这篇文章比其他帖子长得多,我为此道歉。所以,如果您对此感兴趣,请帮助我。
CREATE TABLE software_manual(
item_no varchar(50),
title varchar(50) NOT NULL,
description varchar(500),
manufacturer varchar(50),
version varchar(50),
CONSTRAINT software_manual_pk PRIMARY KEY(item_no),
CONSTRAINT check_sw_item_no CHECK (title LIKE '[S]%')
);
CREATE TABLE cpy_item(
access_no int,
status varchar(50),
CONSTRAINT cpy_item_pk PRIMARY KEY(access_no),
CONSTRAINT check_status CHECK(status IN ('Borrowed','Lost','Stolen','Available'))
);
CREATE TABLE cpy_software_manual(
access_no int,
item_no varchar(50),
CONSTRAINT cpy_software_manual_pk PRIMARY KEY(access_no),
CONSTRAINT cpy_software_manual_fk FOREIGN KEY(item_no) REFERENCES software_manual(item_no),
CONSTRAINT cpy_software_manual_fk2 FOREIGN KEY(access_no) REFERENCES cpy_item(access_no)
);
我为上面的表创建一个视图,如下所示
CREATE VIEW vw_sw_manual (title,manufacturer,version,access_no)
AS
SELECT sm.title,sm.manufacturer,sm.version,csm.access_no
FROM software_manual sm, cpy_software_manual csm
WHERE sm.item_no=csm.item_no;
我想写一个INSTEAD OF触发器到vw_sw_manual视图来做以下提到
我。如果插入的行包含表中存在的访问权限,则使用插入的数据更新software_manual表的版本,标题和制造商列。
II。否则,如果sofware_manual表中存在标题,制造商和版本,则在cpy_software_manual表中插入一个新行,其中的项目信息引用software_manual表的现有行
III。否则,将行插入software_manual和cpy_software_manual表以反映新信息。
我写了一个触发器,如下所示,它包含一些逻辑错误。 在第二部分。我的问题@item_no在我的触发器中获取空值。任何人都可以帮我解决这个问题吗?
alter TRIGGER tr_sw_vw_insert
ON vw_sw_manual INSTEAD OF INSERT
AS
BEGIN
DECLARE @insertAccessNo int
DECLARE @title varchar(50)
DECLARE @manufacturer varchar(5)
DECLARE @version varchar(50)
DECLARE @itemNo varchar(50)
DECLARE @maxAccessNo int
SELECT @maxAccessNo=MAX(access_no) FROM cpy_software_manual;
SELECT @insertAccessNo=access_no,@title=title,@manufacturer=manufacturer,@version=version FROM inserted;
IF(@insertAccessNo IS NOT NULL)
BEGIN -- if @insertAccessNo IS NOT NULL do this
SELECT @itemNo=item_no FROM cpy_software_manual WHERE access_no=@insertAccessNo;
UPDATE software_manual SET title=@title,version=@version,manufacturer=@manufacturer
WHERE item_no=@insertAccessNo;
END
ELSE IF(@title IS NOT NULL AND @manufacturer IS NOT NULL AND @version IS NOT NULL)
BEGIN
INSERT INTO cpy_item(access_no,status) VALUES(@maxAccessNo+1,'Available');
SELECT @itemNo=item_no FROM software_manual
WHERE title=@title AND manufacturer=@manufacturer AND version=@version;
INSERT INTO cpy_software_manual(access_no,item_no) VALUES(@maxAccessNo+1,@itemNo);
END
ELSE
BEGIN-- else do this
DECLARE @maxItemNo int
SELECT @maxItemNo=MAX(item_no) FROM software_manual;
INSERT INTO software_manual(item_no,title,version,manufacturer)
VALUES(@maxItemNo+1,@title,@version,@manufacturer);
INSERT INTO cpy_item(access_no,status) VALUES(@maxAccessNo+1,'Available');
INSERT INTO cpy_software_manual(access_no,item_no) VALUES(@maxAccessNo+1,@maxItemNo+1);
END
END
答案 0 :(得分:0)
alter TRIGGER tr_sw_vw_insert
ON vw_sw_manual INSTEAD OF INSERT
AS
BEGIN
DECLARE @insertAccessNo int
DECLARE @title varchar(50)
DECLARE @manufacturer varchar(5)
DECLARE @version varchar(50)
DECLARE @itemNo varchar(50)
DECLARE @maxAccessNo int
SELECT @maxAccessNo=MAX(access_no) FROM cpy_software_manual;
SELECT @insertAccessNo=access_no,@title=title,@manufacturer=manufacturer,@version=version FROM inserted;
IF(@insertAccessNo IS NOT NULL)
BEGIN -- if @insertAccessNo IS NOT NULL do this
SELECT @itemNo=item_no FROM cpy_software_manual WHERE access_no=@insertAccessNo;
UPDATE software_manual SET title=@title,version=@version,manufacturer=@manufacturer
WHERE item_no=@insertAccessNo;
END
ELSE IF(@title IS NOT NULL AND @manufacturer IS NOT NULL AND @version IS NOT NULL)
BEGIN
INSERT INTO cpy_item(access_no,status) VALUES(@maxAccessNo+1,'Available');
SELECT @itemNo=item_no FROM software_manual
WHERE title=@title AND manufacturer=@manufacturer AND version=@version;
INSERT INTO cpy_software_manual(access_no,item_no) VALUES(@maxAccessNo+1,@itemNo);
END
ELSE
BEGIN-- else do this
DECLARE @maxItemNo int
SELECT @maxItemNo=MAX(item_no) FROM software_manual;
INSERT INTO software_manual(item_no,title,version,manufacturer)
VALUES(@maxItemNo+1,@title,@version,@manufacturer);
INSERT INTO cpy_item(access_no,status) VALUES(@maxAccessNo+1,'Available');
INSERT INTO cpy_software_manual(access_no,item_no) VALUES(@maxAccessNo+1,@maxItemNo+1);
END
END