怎么写触发器和存储过程/函数??????

飞火流星02027 2009-09-24 01:44:24

怎么写触发器和存储过程/函数??????
有两个表,一个存储位置参数表:
CREATE TABLE T_PUB_CCWZCS
(
N_GH tinyint PRIMARY KEY,//柜号
N_CS tinyint,//层数
N_HS tinyint,//盒数
N_PS tinyint,//容量
)

存储位置表:
CREATE TABLE T_GPGL_CCWZ
(
ID int IDENTITY (1, 1) NOT NULL,
N_GH tinyint, //柜号
N_CH tinyint, //层号
N_HH tinyint, //盒号
N_YS tinyint, //余数 (默认值是盒子子的容量)
)

CREATE TRIGGER MyfirstTrigger ON _PUB_CCWZCS
AFTER INSERT
AS
.........
怎么创建一个触发器(或都别的方法也行),做到如下功能:
当:
insert into T_PUB_CCWZCS //存储位置参数表
values(1,3,2,120);
之后马上:
insert into T_PUB_CCWZ //存储位置表
values(1,1,1,120);
insert into T_PUB_CCWZ
values(1,1,2,120);
insert into T_PUB_CCWZ
values(1,2,1,120);
insert into T_PUB_CCWZ
values(1,2,2,120);
insert into T_PUB_CCWZ
values(1,3,1,120);
insert into T_PUB_CCWZ
values(1,3,2,120);

每当在into T_PUB_CCWZCS表中增加一条记录都会在存储位置表中增加该柜子的所有存储位置记录。
怎么写触发器和存储过程/函数??????
...全文
72 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞火流星02027 2009-09-24
  • 打赏
  • 举报
回复

怎么写触发器和存储过程/函数??????
有两个表,一个存储位置参数表:
CREATE TABLE T_PUB_CCWZCS
(
N_GH tinyint PRIMARY KEY,//柜号(2位)
N_CS tinyint,//层数(2位)
N_HS tinyint,//盒数(2位)
N_PS tinyint,//容量(3位)
)

存储位置表:
CREATE TABLE T_GPGL_CCWZ
(
C_CCWZID varchar(8) 主键,形式:(01-01-01)柜号(2位)+层号(2位)+盒号(2位)
N_GH tinyint, //柜号(2位)
N_CH tinyint, //层号(2位)
N_HH tinyint, //盒号(2位)
N_YS tinyint, //余数 (默认值是盒子子的容量)
)

CREATE TRIGGER MyfirstTrigger ON _PUB_CCWZCS
AFTER INSERT
AS
declare @i int,@j int,@t int;
select @i=N_ZGCS,@t=N_KFHS from inserted;
while @i>0
begin
set @j=@t;
while @j>0
begin
insert into T_PUB_CCWZ(N_GH+'-'+N_CH+'-'+N_HH,N_GH,N_CH,N_HH,N_YS) select N_GH,@i,@j,N_KFPS from inserted;
set @j=@j-1
end
set @i=@i-1
end
为什么会报语法错:‘+’附近有错误??
fwacky 2009-09-24
  • 打赏
  • 举报
回复

create trigger MyfirstTrigger on T_PUB_CCWZCS
AFTER insert
as
declare @n int,@m int,@cs int,@hs int
set @cs=(select N_CS from inserted)
set @hs=(select N_HS from inserted)

set @n=1
set @m=1
while @n<=@cs
begin
while @m<=@hs
begin
insert T_PUB_CCWZ select N_GH,@n,@m,N_PS FROM inserted
set @m=@m+1
end
set @n=@n+1
end
go

飞火流星02027 2009-09-24
  • 打赏
  • 举报
回复
有人顶,真的好感动。。。》》!!!!等我研究好了我就结贴哈!!
fwacky 2009-09-24
  • 打赏
  • 举报
回复

CREATE TABLE T_PUB_CCWZCS
(
N_GH tinyint PRIMARY KEY,--柜号
N_CS tinyint,--层数
N_HS tinyint,--盒数
N_PS tinyint,--容量
)


CREATE TABLE T_GPGL_CCWZ
(
ID int IDENTITY (1, 1) NOT NULL,
N_GH tinyint, --柜号
N_CH tinyint, --层号
N_HH tinyint, --盒号
N_YS tinyint, --余数 (默认值是盒子子的容量)
)

alter TRIGGER MyfirstTrigger ON T_PUB_CCWZCS
AFTER INSERT
AS
insert into T_GPGL_CCWZ values(1,1,1,120);
insert into T_GPGL_CCWZ values(1,1,2,120);
insert into T_GPGL_CCWZ values(1,2,1,120);
insert into T_GPGL_CCWZ values(1,2,2,120);
insert into T_GPGL_CCWZ values(1,3,1,120);
insert into T_GPGL_CCWZ values(1,3,2,120);


insert into T_PUB_CCWZCS values(1,3,2,120)

select * from T_PUB_CCWZCS
select *from T_GPGL_CCWZ

T_GPGL_CCWZ

1 1 1 1 120
2 1 1 2 120
3 1 2 1 120
4 1 2 2 120
5 1 3 1 120
6 1 3 2 120

华夏小卒 2009-09-24
  • 打赏
  • 举报
回复

--触发器

create trigger gg on T_PUB_CCWZCS
for insert
as
declare @n int,@m int,@cs int,@hs int
set @cs=(select N_CS from inserted)
set @hs=(select N_HS from inserted)

set @n=1
set @m=1
while @n<=@cs
begin
while @m<=@hs
begin
insert T_PUB_CCWZ select N_GH,@n,@m,N_PS FROM inserted
set @m=@m+1
end
set @n=@n+1
end
go

---try
sgtzzc 2009-09-24
  • 打赏
  • 举报
回复
CREATE TRIGGER MyfirstTrigger ON _PUB_CCWZCS
AFTER INSERT
AS
declare @i int,@j int,@t int;
select @i=N_CS,@t=N_HS from inserted;
while @i>0
begin
set @j=@t;
while @j>0
begin
insert into T_PUB_CCWZ(N_GH,N_CH,N_HH,N_YS) select N_GH,@i,@j,N_PS from inserted;
set @j=@j-1
end
set @i=@i-1
end

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧