| 发表于:2007-06-26 13:57:335楼 得分:0 |
用c#实现?c#还不是操纵数据库实现啊,哈哈 给你一个自动编号的存储过程,在c#里去调用 set ansi_nulls on set quoted_identifier on go create procedure [dbo].[autono] @strtablename varchar(50),--要编号的表名 @strcolumnname varchar(50),--要编号的列名 @strprestring varchar(50),--编号前缀字符 @istartpos int,--编号起始位 @ilength int,--长度 @strwhere varchar(800),--外部强制条件,可为空字符 " " @bdiscontinuity bit--是否补缺号 as declare @returnno int if len(@strwhere) > 0 set @strwhere = @strwhere + ' and ' if @bdiscontinuity = 0 begin EXEC( 'select identity(int,1,1) xh,cast(substring( '+@strcolumnname+ ', '+ @istartpos+ ', '+@ilength+ ') as int) as curcode into ##temp from (select '+@strcolumnname+ ' from '+ @strtablename+ ' where '+@strwhere+@strcolumnname+ ' like ' ' '+@strprestring+ '% ' ') as ls ') set @returnno = (select isnull(min(##temp.xh),0) as returnno from ##temp where ##temp.xh <> ##temp.curcode) drop table ##temp if @returnno = 0 --说明没有检测到断号,此时要返回的是数据表中最大编号 begin EXEC( 'select cast(substring( '+@strcolumnname+ ', '+@istartpos+ ', '+@ilength+ ') as int) as maxno into ##temp1 from (select '+@strcolumnname+ ' from '+@strtablename+ ' where '+@strwhere+@strcolumnname+ ' like ' ' '+@strprestring+ '% ' ') as ls ') select isnull(max(##temp1.maxno),0)+1 as returnno from ##temp1 drop table ##temp1 end else select @returnno as returnno end else begin EXEC( 'select cast(substring( '+@strcolumnname+ ', '+@istartpos+ ', '+@ilength+ ') as int) as maxno into ##temp from (select '+@strcolumnname+ ' from '+@strtablename+ ' where '+@strwhere+@strcolumnname+ ' like ' ' '+@strprestring+ '% ' ') as ls ') EXEC( 'select isnull(max(##temp.maxno),0)+1 as returnno from ##temp ') drop table ##temp end | | |
|