主题:一个关于触发器的问题,各位大虾请进!
如下一段代码:
CREATE TRIGGER trigger6 ON [dbo].[MF_WR]
instead of INSERT
AS
insert into MF_WR select * from inserted
DECLARE @ITM integer,@wr char(10)
set @wr=(select WR_NO from inserted)
/*下面的代码被我剪下来到查询分析器里面运行,可以对一下一个字母不差*/
SET @ITM=(SELECT ZC_ITM
FROM MF_TZ
WHERE (TZ_NO = (SELECT TZ_NO FROM TF_WR WHERE WR_NO=@wr))
AND (MO_NO = (SELECT MO_NO FROM TF_WR WHERE WR_NO=@wr)))
IF(@ITM=1)
BEGIN
select * into ribaobiao from TF_WR where WR_NO=@wr
delete from TF_WR where WR_NO=@wr
DECLARE @num integer
set @num=(SELECT top 1 itm
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) order by itm desc)
while(@num>0)
begin
update ribaobiao set itm=@num,qty=(SELECT qty
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),prd_mark=(SELECT prd_mark
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),bat_no=(SELECT bat_no
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
)
insert into tf_wr select * from ribaobiao
set @num=@num-1
end
drop table ribaobiao
END
ELSE
BEGIN
select * into ribaobiao from TF_WR where MO_NO = (SELECT MO_NO FROM TF_WR WHERE WR_NO=@wr)
and TZ_NO = (SELECT LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) - 1 AS varchar) FROM TF_WR
WHERE (WR_NO=@wr))
update ribaobiao set WR_NO=@wr,TZ_NO =(LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) + 1 AS varchar))
delete from TF_WR where WR_NO=@wr
insert into tf_wr select * from ribaobiao
drop table ribaobiao
END
在我向mf_wr中插入记录时没有显示期望的结果,但是我把其中的这段代码剪下来,稍微改一下变成如下所示的代码到查询分析器里面运行就会出现预期的结果
DECLARE @ITM integer,@wr char(10)
set @wr=(select wr_no from mf_wr where wr_no='WR5B240001')
/*以下代码是剪下来的*/
SET @ITM=(SELECT distinct ZC_ITM
FROM MF_TZ
WHERE (TZ_NO = (SELECT TZ_NO FROM TF_WR WHERE WR_NO=@wr))
AND (MO_NO = (SELECT MO_NO FROM TF_WR WHERE WR_NO=@wr)))
IF(@ITM=1)
BEGIN
select * into ribaobiao from TF_WR where WR_NO=@wr
delete from TF_WR where WR_NO=@wr
DECLARE @num integer
set @num=(SELECT top 1 itm
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) order by itm desc)
while(@num>0)
begin
update ribaobiao set itm=@num,qty=(SELECT qty
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),prd_mark=(SELECT prd_mark
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),bat_no=(SELECT bat_no
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
)
insert into tf_wr select * from ribaobiao
set @num=@num-1
end
drop table ribaobiao
END
ELSE
BEGIN
select * into ribaobiao from TF_WR where MO_NO = (SELECT distinct MO_NO FROM TF_WR WHERE WR_NO=@wr)
and TZ_NO = (SELECT LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) - 1 AS varchar) FROM TF_WR
WHERE (WR_NO=@wr))
update ribaobiao set WR_NO=@wr,TZ_NO =(LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) + 1 AS varchar))
delete from TF_WR where WR_NO=@wr
insert into tf_wr select * from ribaobiao
drop table ribaobiao
END
我测试的结果好象不在查询分析器里面运行if语句从来不运行,else里面的select * into ...也没有往ribaobiao插入任何记录(ribaobiao创建了但里面没有任何记录,但应该是有的),我怀疑一开始的两个set赋值没有赋到值,还请高手指点!!
CREATE TRIGGER trigger6 ON [dbo].[MF_WR]
instead of INSERT
AS
insert into MF_WR select * from inserted
DECLARE @ITM integer,@wr char(10)
set @wr=(select WR_NO from inserted)
/*下面的代码被我剪下来到查询分析器里面运行,可以对一下一个字母不差*/
SET @ITM=(SELECT ZC_ITM
FROM MF_TZ
WHERE (TZ_NO = (SELECT TZ_NO FROM TF_WR WHERE WR_NO=@wr))
AND (MO_NO = (SELECT MO_NO FROM TF_WR WHERE WR_NO=@wr)))
IF(@ITM=1)
BEGIN
select * into ribaobiao from TF_WR where WR_NO=@wr
delete from TF_WR where WR_NO=@wr
DECLARE @num integer
set @num=(SELECT top 1 itm
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) order by itm desc)
while(@num>0)
begin
update ribaobiao set itm=@num,qty=(SELECT qty
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),prd_mark=(SELECT prd_mark
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),bat_no=(SELECT bat_no
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
)
insert into tf_wr select * from ribaobiao
set @num=@num-1
end
drop table ribaobiao
END
ELSE
BEGIN
select * into ribaobiao from TF_WR where MO_NO = (SELECT MO_NO FROM TF_WR WHERE WR_NO=@wr)
and TZ_NO = (SELECT LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) - 1 AS varchar) FROM TF_WR
WHERE (WR_NO=@wr))
update ribaobiao set WR_NO=@wr,TZ_NO =(LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) + 1 AS varchar))
delete from TF_WR where WR_NO=@wr
insert into tf_wr select * from ribaobiao
drop table ribaobiao
END
在我向mf_wr中插入记录时没有显示期望的结果,但是我把其中的这段代码剪下来,稍微改一下变成如下所示的代码到查询分析器里面运行就会出现预期的结果
DECLARE @ITM integer,@wr char(10)
set @wr=(select wr_no from mf_wr where wr_no='WR5B240001')
/*以下代码是剪下来的*/
SET @ITM=(SELECT distinct ZC_ITM
FROM MF_TZ
WHERE (TZ_NO = (SELECT TZ_NO FROM TF_WR WHERE WR_NO=@wr))
AND (MO_NO = (SELECT MO_NO FROM TF_WR WHERE WR_NO=@wr)))
IF(@ITM=1)
BEGIN
select * into ribaobiao from TF_WR where WR_NO=@wr
delete from TF_WR where WR_NO=@wr
DECLARE @num integer
set @num=(SELECT top 1 itm
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) order by itm desc)
while(@num>0)
begin
update ribaobiao set itm=@num,qty=(SELECT qty
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),prd_mark=(SELECT prd_mark
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
),bat_no=(SELECT bat_no
FROM TF_ML
WHERE (ML_NO =
(SELECT ml_no
FROM mf_ml
WHERE TZ_no = (SELECT TZ_NO FROM ribaobiao)
AND mo_no = (SELECT MO_NO FROM ribaobiao))) and itm=@num
)
insert into tf_wr select * from ribaobiao
set @num=@num-1
end
drop table ribaobiao
END
ELSE
BEGIN
select * into ribaobiao from TF_WR where MO_NO = (SELECT distinct MO_NO FROM TF_WR WHERE WR_NO=@wr)
and TZ_NO = (SELECT LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) - 1 AS varchar) FROM TF_WR
WHERE (WR_NO=@wr))
update ribaobiao set WR_NO=@wr,TZ_NO =(LEFT(TZ_NO, 5) + CAST(CAST(RIGHT(TZ_NO, 5) AS integer) + 1 AS varchar))
delete from TF_WR where WR_NO=@wr
insert into tf_wr select * from ribaobiao
drop table ribaobiao
END
我测试的结果好象不在查询分析器里面运行if语句从来不运行,else里面的select * into ...也没有往ribaobiao插入任何记录(ribaobiao创建了但里面没有任何记录,但应该是有的),我怀疑一开始的两个set赋值没有赋到值,还请高手指点!!