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

一人触发器的问题?
表结构:
CREATE   TABLE   [dbo].[tClass]   (
[ClassID]   [int]   IDENTITY   (1,   1)   NOT   NULL   ,
[ClassName]   [varchar]   (50)   COLLATE   Chinese_PRC_CI_AS   NOT   NULL   ,
[ClassMemo]   [varchar]   (50)   COLLATE   Chinese_PRC_CI_AS   NULL  
)   ON   [PRIMARY]
GO

/******     /
CREATE   TABLE   [dbo].[tTest]   (
[TestID]   [int]   IDENTITY   (1,   1)   NOT   NULL   ,
[TestName]   [varchar]   (50)   COLLATE   Chinese_PRC_CI_AS   NOT   NULL   ,
[TestMemo]   [varchar]   (50)   COLLATE   Chinese_PRC_CI_AS   NULL  
)   ON   [PRIMARY]
GO
我想提取tClass里面字段内容到tTest中去,以后每次入的时候也提取到Ttest表中去,触发器如下,但出错,怎么会事?提示游标CC已经存在,可我Deallcoate了啊?
Create   trigger   Insert_tClass
on   tclass
for   insert
as
declare   cc   cursor
for   select   classname,classmemo   from   tclass
open   cc
declare   @classname   varchar(50),
                @classmemo   varchar(50)
fetch   next   from   cc   into   @classname,@classmemo
while   @@fetch_status=0  
begin
    insert   ttest(testname,testmemo)values(@classname,@classmemo)
    fetch   next   from   cc   into   @classname,@classmemo
end
close   cc
deallocate   cc
go


------解决方案--------------------
Create trigger Insert_tClass
on tclass
for insert
as
declare @classname varchar(50),
@classmemo varchar(50)

declare cc cursor
for select classname,classmemo from tclass
open cc

fetch next from cc into @classname,@classmemo
while @@fetch_status=0
begin
insert ttest(testname,testmemo)values(@classname,@classmemo)
fetch next from cc into @classname,@classmemo
end
close cc
deallocate cc
go