Recursive triggers and error : subquery returned more than 1 value
Recursive triggers and error : subquery returned more than 1 value
我需要你的帮助!
我正在 sql 服务器上工作
1-- 我创建了这个触发器,但它似乎是错误的...
CREATE TRIGGER [dbo].[chargeAZero]
ON [dbo].[situations_final]
after INSERT
AS
BEGIN
SET nocount ON
UPDATE sfinal
SET charge = 00
FROM inserted i
INNER JOIN situations_final sfinal
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie
/*and i.Datecadence=sfinal.Datecadence*/
WHERE (SELECT sfinal.nouveauposte
FROM situations_final sfinal
INNER JOIN inserted i
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie) IS
NULL
END
错误消息始终相同:子查询返回了多个值...我想我正确地编写了触发器,就像我对其他工作正常的触发器所做的那样。
2-- 我的第二个问题是:是否可以只使一个触发器递归?
3-- 正如您在我的 table "Nomenclatures" 数据库中注意到的那样(英文材料清单)我有 3 个元素:
*codepiecemere:组件母
*codepiecefille:组件 child
* 数量。
我给你一个我需要的例子:
母亲= A Child= B 数量= 2
妈妈= B Child= C 数量= 3
我想要一个触发器给我这样的结果:
A 1 B 2 C 6=2*3(C做1 B所需的数量)。
非常感谢
is null
通常不与子查询一起使用。试试这个:
where not exists (select 1
from SITUATIONS_Final sfinal inner join inserted i
on i.ReferencePiece=sfinal.ReferencePiece
and i.AncienPoste=sfinal.AncienPoste
and i.numerophase=sfinal.numerophase
and i.datestrategie=sfinal.datestrategie
)
这是假设 is null
正在测试没有返回值,而不是 sfinal.nouveauposte
中的 NULL
值。如果是后者:
where exists (select 1
from SITUATIONS_Final sfinal inner join inserted i
on i.ReferencePiece=sfinal.ReferencePiece
and i.AncienPoste=sfinal.AncienPoste
and i.numerophase=sfinal.numerophase
and i.datestrategie=sfinal.datestrategie
where sfinal.nouveauposte is null
)
编辑:
你真的需要子查询吗?
UPDATE sfinal
SET charge = 00
FROM inserted i
INNER JOIN situations_final sfinal
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie
WHERE sfinal.nouveauposte IS NULL;
我认为问题在于您在单个命令中插入了多行,因此插入的 table 包含多行。因此子查询
SELECT sfinal.nouveauposte
FROM situations_final sfinal
INNER JOIN inserted i
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie
也包含不止一行,无法与作为标量值的 NULL 进行比较。
这是解决 material 聚合问题的递归查询。
Table定义
CREATE TABLE [dbo].[Material](
[Mother] [varchar](100) NOT NULL,
[Child] [varchar](100) NOT NULL,
[Quantity] [int] NOT NULL,
)
和查询:
WITH Result(mother, child, quantity)
AS
(
select * from material
union all
select M.mother, R.Child, M.quantity * R.Quantity as Quantity
from Result R INNER JOIN Material M ON M.Child = R.Mother
)
select * from result
你可以在这里看到一个例子:http://sqlfiddle.com/#!6/6dc64/1
更新:
Sql fiddle 不工作,我不知道为什么
更新 2
Sql Fiddle 回来了! :-)
我雄心勃勃:D 我试图改进脚本:
WITH RESULT (MOTHER, CHILD, QUANTITY)
as
(
select Mother, Child, CONVERT(Numeric(10,0), Quantity) as Quantity from bilangammestest
union all select M.mother, R.Child, CONVERT(Numeric(10,0), M.quantity * R.Quantity) as Quantity from Result R
INNER JOIN bilangammestest M ON M.Child = R.Mother
)
select * from result
where mother not in (select child from bilangammestest )
这是我在 table "Bilangammestest" 上的数据:
Z A 1
Z Y 1
A B 2
Y B 2
B C 3
这是我得到的结果:
Z A 1
Z Y 1
Z C 6
Z C 6
Z B 2
Z B 2
这是我想要的最终结果:
Z A 1
Z Y 1
Z C 12
Z B 4
我试着做 'sum' 但我做错了:(
我需要你的帮助!
我正在 sql 服务器上工作 1-- 我创建了这个触发器,但它似乎是错误的...
CREATE TRIGGER [dbo].[chargeAZero]
ON [dbo].[situations_final]
after INSERT
AS
BEGIN
SET nocount ON
UPDATE sfinal
SET charge = 00
FROM inserted i
INNER JOIN situations_final sfinal
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie
/*and i.Datecadence=sfinal.Datecadence*/
WHERE (SELECT sfinal.nouveauposte
FROM situations_final sfinal
INNER JOIN inserted i
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie) IS
NULL
END
错误消息始终相同:子查询返回了多个值...我想我正确地编写了触发器,就像我对其他工作正常的触发器所做的那样。
2-- 我的第二个问题是:是否可以只使一个触发器递归?
3-- 正如您在我的 table "Nomenclatures" 数据库中注意到的那样(英文材料清单)我有 3 个元素: *codepiecemere:组件母 *codepiecefille:组件 child * 数量。 我给你一个我需要的例子: 母亲= A Child= B 数量= 2 妈妈= B Child= C 数量= 3
我想要一个触发器给我这样的结果: A 1 B 2 C 6=2*3(C做1 B所需的数量)。
非常感谢
is null
通常不与子查询一起使用。试试这个:
where not exists (select 1
from SITUATIONS_Final sfinal inner join inserted i
on i.ReferencePiece=sfinal.ReferencePiece
and i.AncienPoste=sfinal.AncienPoste
and i.numerophase=sfinal.numerophase
and i.datestrategie=sfinal.datestrategie
)
这是假设 is null
正在测试没有返回值,而不是 sfinal.nouveauposte
中的 NULL
值。如果是后者:
where exists (select 1
from SITUATIONS_Final sfinal inner join inserted i
on i.ReferencePiece=sfinal.ReferencePiece
and i.AncienPoste=sfinal.AncienPoste
and i.numerophase=sfinal.numerophase
and i.datestrategie=sfinal.datestrategie
where sfinal.nouveauposte is null
)
编辑:
你真的需要子查询吗?
UPDATE sfinal
SET charge = 00
FROM inserted i
INNER JOIN situations_final sfinal
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie
WHERE sfinal.nouveauposte IS NULL;
我认为问题在于您在单个命令中插入了多行,因此插入的 table 包含多行。因此子查询
SELECT sfinal.nouveauposte
FROM situations_final sfinal
INNER JOIN inserted i
ON i.referencepiece = sfinal.referencepiece
AND i.ancienposte = sfinal.ancienposte
AND i.numerophase = sfinal.numerophase
AND i.datestrategie = sfinal.datestrategie
也包含不止一行,无法与作为标量值的 NULL 进行比较。
这是解决 material 聚合问题的递归查询。
Table定义
CREATE TABLE [dbo].[Material](
[Mother] [varchar](100) NOT NULL,
[Child] [varchar](100) NOT NULL,
[Quantity] [int] NOT NULL,
)
和查询:
WITH Result(mother, child, quantity)
AS
(
select * from material
union all
select M.mother, R.Child, M.quantity * R.Quantity as Quantity
from Result R INNER JOIN Material M ON M.Child = R.Mother
)
select * from result
你可以在这里看到一个例子:http://sqlfiddle.com/#!6/6dc64/1
更新:
Sql fiddle 不工作,我不知道为什么
更新 2
Sql Fiddle 回来了! :-)
我雄心勃勃:D 我试图改进脚本:
WITH RESULT (MOTHER, CHILD, QUANTITY)
as
(
select Mother, Child, CONVERT(Numeric(10,0), Quantity) as Quantity from bilangammestest
union all select M.mother, R.Child, CONVERT(Numeric(10,0), M.quantity * R.Quantity) as Quantity from Result R
INNER JOIN bilangammestest M ON M.Child = R.Mother
)
select * from result
where mother not in (select child from bilangammestest )
这是我在 table "Bilangammestest" 上的数据:
Z A 1
Z Y 1
A B 2
Y B 2
B C 3
这是我得到的结果:
Z A 1
Z Y 1
Z C 6
Z C 6
Z B 2
Z B 2
这是我想要的最终结果:
Z A 1
Z Y 1
Z C 12
Z B 4
我试着做 'sum' 但我做错了:(