当前位置: 代码迷 >> Sql Server >> 这样写的identity为什么会有语法异常呢
  详细解决方案

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

热度:77   发布时间:2016-04-27 21:36:12.0
这样写的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)
  相关解决方案