日期:2014-05-18  浏览次数:20425 次

有没有办法使identity的始量,可以用变量来替代?
declare   @beginnum   as   int
set   @beginnum   =   88
select   identity(int,@beginnum,1)   as   iden   into   #t1   from   sysobjects
select   *   from   #t1
drop   table   #t1

上面的是错误的,那么有没有办法使identity的始量,可以用变量来替代?

进一步说,有没有办法,可以使identity的始量和增量,可以用变量来替代?


------解决方案--------------------
那只有用动态SQL实现。

declare @beginnum as int, @sql varchar(1000)

set @beginnum = 88

set @sql = 'select identity(int, ' + cast(@beginnum as varchar(100)) + ',1) as iden into #t1 from sysobjects select * from #t1 drop table #t1 '

print @sql

exec (@sql)