create procedure TestA as
declare @Col1 varchar(10),@bi int
select @bi = 0
declare cur_select cursor
for select  GoodstypeID from Goodstype order by GoodstypeID
OPen cur_select
fetch next from cur_select into @Col1
while @@fetch_status = 0
begin
if(@bi = 0)
begin
select IDENTITY(int,   1,1)   AS   ID_Num,GoodsID into TestAtable  from allgoods where left(Middle_Item_ID,2) = @Col1
select @bi = @bi + 1
end
else
begin
select IDENTITY(int,   1,1)   AS   ID_Num,GoodsID into TestAtable1  from allgoods where left(Middle_Item_ID,2) = @Col1
SET  IDENTITY_INSERT TestAtable ON --注意這裡
INSERT INTO TestAtable   SELECT * from TestAtable1
drop table TestAtable1
end
FETCH NEXT FROM cur_select INTO @Col1end
select * from TestAtable
drop table TestAtable
close cur_select
deallocate cur_select
go--錯誤信息
位於資料表 'TestAtable' 的識別資料行其外顯值只有當使用了資料行清單且 IDENTITY_INSERT 為 ON 時才能指定。
請問各位這個存儲過程應該怎麼修改。

解决方案 »

  1.   

    select IDENTITY(int,   1,1)   AS   ID_Num,GoodsID into TestAtable1  from allgoods where left(Middle_Item_ID,2) = @Col1
    SET  IDENTITY_INSERT TestAtable ON --注意這裡
    INSERT INTO TestAtable   SELECT * from TestAtable1
    drop table TestAtable1這段改為select IDENTITY(int,   1,1)   AS   ID_Num,GoodsID into TestAtable1  from allgoods where left(Middle_Item_ID,2) = @Col1
    SET  IDENTITY_INSERT TestAtable ON --注意這裡
    INSERT INTO TestAtable(ID_Num,GoodsID)  SELECT ID_Num,GoodsID from TestAtable1 --修改的地方
    SET  IDENTITY_INSERT TestAtable OFF --加上的地方
    drop table TestAtable1
      

  2.   

    如果將IDENTITY_INSERT 設為ON,插入的時候,必須將列名指定出來,不能直接這麼寫。“只有當使用了資料行清單且 IDENTITY_INSERT 為 ON 時才能指定。”,注意這句話的前半部分,“只有當使用了資料行清單”。
      

  3.   

    不说语法,paoluo已经说得比较清楚,问题是这种存储过程不需要这么写
      

  4.   

    為了彌補http://community.csdn.net/Expert/topic/4915/4915916.xml?temp=.1423609帖子給予
    paoluo(一天到晚游泳的鱼) 的分數的不公特此將本帖所有的分數給paoluo(一天到晚游泳的鱼)
      

  5.   

    其中 排行是查詢的時候自動產生的,(按照部門來產生的這樣的sql如何實習)
    ----------------
    Select 
    [排 行]=(Select Count(*) From TableName Where 部門=A.部門 And 商品編號<=A.商品編號
    ),
    *
    From TableName A給我的啟發不小,謝謝