日期:2014-05-19  浏览次数:20679 次

表变量,IDENTITY_INSERT,order by 的问题?
declare   @tt   table   (sid   tinyint   identity(1,1),v   decimal(18,2),st   tinyint)

insert   into   @tt  
select   #1.vs,
case
when   ...   then   0
when   ...   then   1
when   ...   then   2
end   si
from   fd()#1
inner   join
fs( ' ')#3  
on   #1.id=#3.id
order   by     vs,si

就会出错:
服务器:   消息   8101,级别   16,状态   1,行   3
仅当使用了列的列表,并且   IDENTITY_INSERT   为   ON   时,才能在表   '@tt '   中为标识列指定显式值。

如果去掉order   by     vs,si,就不会出错!

vs是decimal数据。

------解决方案--------------------
自动增量的列是不是插入值的 ,你去掉自动增量就OK了
------解决方案--------------------
表变量不用设置(这样用的)
declare @ta table(id int identity(1,1),name varchar(10))
insert @ta
select 'a ' union all select 'b '
select * from @ta
针对表才用
SET IDENTITY_INSERT 表名 off

SET IDENTITY_INSERT 表名 ON