触发器/程序帮助

发布于 2024-10-03 13:08:30 字数 1485 浏览 2 评论 0原文

create table  student
(spnr   VARCHAR(5)  PRIMARY KEY,
sname   VARCHAR(10),
saddress    VARCHAR(10),
stel    VARCHAR(10) )

create table course
    (ccode  VARCHAR(5)  PRIMARY KEY,
    cname   VARCHAR(10),
    caddress    VARCHAR(10),
    points  int )

create table studies
    (id     int     PRIMARY KEY,
     spnr   VARCHAR(5)  NOT NULL,
     ccode  VARCHAR(5)  NOT NULL,
     result int,    
    CONSTRAINT STUDIES_SPNR_FK FOREIGN KEY(spnr) REFERENCES student(spnr),
    CONSTRAINT STUDIES_CCODE_FK FOREIGN KEY(ccode) REFERENCES course(ccode) )

create trigger t1  on  course
after update,insert
as
    print 'inside trigger';
    select i.ccode from inserted i, deleted d
    where i. ccode = d. ccode

create procedure what (@tableName varchar(10))
as 
begin

    declare @name varchar(20);

    declare c cursor for 
    select column_name 
    from information_schema.columns
    where table_name = @tableName;
    open c;
    fetch c into @name;
    while @@fetch_status = 0 begin
        print 'name:' + @name;
        fetch c into @name;
    end

    close c;
    deallocate c;
end;

运行时:

begin transaction
insert into course values('K1','data1','lund',5);
update course
set cname ='informatik';
commit

显示如下信息:

   inside trigger

(0 row(s) affected)

(1 row(s) affected)
inside trigger

(1 row(s) affected)

(1 row(s) affected)

最后,为什么1行受影响的最后显示了2次?当它只在一个位置将 cname 更改为“信息”时?

create table  student
(spnr   VARCHAR(5)  PRIMARY KEY,
sname   VARCHAR(10),
saddress    VARCHAR(10),
stel    VARCHAR(10) )

create table course
    (ccode  VARCHAR(5)  PRIMARY KEY,
    cname   VARCHAR(10),
    caddress    VARCHAR(10),
    points  int )

create table studies
    (id     int     PRIMARY KEY,
     spnr   VARCHAR(5)  NOT NULL,
     ccode  VARCHAR(5)  NOT NULL,
     result int,    
    CONSTRAINT STUDIES_SPNR_FK FOREIGN KEY(spnr) REFERENCES student(spnr),
    CONSTRAINT STUDIES_CCODE_FK FOREIGN KEY(ccode) REFERENCES course(ccode) )

create trigger t1  on  course
after update,insert
as
    print 'inside trigger';
    select i.ccode from inserted i, deleted d
    where i. ccode = d. ccode

create procedure what (@tableName varchar(10))
as 
begin

    declare @name varchar(20);

    declare c cursor for 
    select column_name 
    from information_schema.columns
    where table_name = @tableName;
    open c;
    fetch c into @name;
    while @@fetch_status = 0 begin
        print 'name:' + @name;
        fetch c into @name;
    end

    close c;
    deallocate c;
end;

When running:

begin transaction
insert into course values('K1','data1','lund',5);
update course
set cname ='informatik';
commit

The following message is displayed:

   inside trigger

(0 row(s) affected)

(1 row(s) affected)
inside trigger

(1 row(s) affected)

(1 row(s) affected)

In the end, why does 1 row affected show 2 times in the end?? When it only changes cname to "informatic" at one position?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷鸟归林 2024-10-10 13:08:30

触发器中的 UPDATE 会产生一个“1 row受影响”,而 SELECT 会产生一个“1 row受影响”。尝试从触发器中删除 SELECT,您应该只会看到“1 行受影响”。

One "1 row affected" results from the UPDATE and one from the SELECT within the trigger. Try removing the SELECT from the trigger and you should only see one "1 row affected."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文