读书人

这样写的identity为什么会有语法异常呢

发布时间: 2011-12-30 23:30:45 作者: rapoo

这样写的identity为什么会有语法错误呢?
declare @f1 as int

set @f1=20 /* @f1的值是从另一个表中取得的,我这里写了一个固定值 */
select identity(int,@f1,1)
into #temp1
from tblInfo

系统提示语法错误,如何来写?

[解决办法]
declare @f1 as int

set @f1=20 /* @f1的值是从另一个表中取得的,我这里写了一个固定值 */

select identity(int,0,1) as id--identity 的参数只能是常量
into #temp1
from tblInfo

update #temp1
set id = id + @f1

[解决办法]
也可以如下:
declare @f1 as int
set @f1=20 /* @f1的值是从另一个表中取得的,我这里写了一个固定值 */
declare @sql as varchar(8000)
set @sql = 'select identity(int, '+ str(@f1) + ',1) as id into #temp1 from t_test select * from #temp1 '
exec(@sql)

读书人网 >SQL Server

热点推荐