首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

有没有办法使identity的始量,可以用变量来替代?解决办法

2012-01-16 
有没有办法使identity的始量,可以用变量来替代?declare@beginnumasintset@beginnum88selectidentity(int,

有没有办法使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)


热点排行