Setting multiple variables from a CTE

自闭症网瘾萝莉.ら 提交于 2020-04-30 07:14:46

问题


 SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER dbo.transferVehicle
ON dbo.Vehicles
AFTER INSERT 
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @Level0Key INT, @Level1Key INT, @Level2Key INT, @Level3Key INT, @Level4Key INT, @Level5Key INT,@Level6Key INT,@Level7Key INT, @LocKey INT;

    SELECT @LocKey = [LocKey] FROM Inserted ;

        with tbParent as
(
    select * from Canepro.dbo.locations where LocKey= @LocKey
    union all
    select locations.* from Canepro.dbo.locations  join tbParent  on locations.LocKey = tbParent.ParentKey
),

    tbsons as
(
    select * from Canepro.dbo.locations where LocKey= @LocKey
    union all
    select locations.* from Canepro.dbo.locations  join tbsons  on locations.ParentKey= tbsons.LocKey
),
    tball as
(
    select * from  tbParent as p
    union 
    select * from tbsons as s

),
final as
(
select number = ROW_NUMBER() OVER (ORDER BY t.LocKey), t.LocKey,t.LocName ,  t.ParentKey 
from tball as t 
)

    SELECT @Level1Key = LocKey from final where number = 1 
    SELECT @Level2Key = LocKey from final where number = 2 -- wont pick up 'final' from this select 
    SELECT @Level3Key = LocKey from final where number = 3
    SELECT @Level4Key = LocKey from final where number = 4


    INSERT INTO [NewDatabase].dbo.Vehicles (VehCode, VehicleNumber, RegistrationNumber, Description, FuelKey, CatKey, Active, ExpectedConsumption, IsPetrol, LicenseExpiryDate, FuelTankCapacity, OdometerReading, Level0LocKey, Level1LocKey, Level2LocKey,Level3LocKey, Level4LocKey, Level5LocKey, Level6LocKey, Level7Key)

        SELECT 
            VehCode, VehicleNumber, RegistrationNumber, Description, FuelType, CatKey, Active, ExpectedConsumption, IsPetrol, LicenseExpiryDate, FuelTankCapacity, OdometerReading, LocKey, @Level0Key, @Level1Key, @Level2Key, @Level3Key, @Level4Key, @Level5Key, @Level6Key, @Level7Key -- then all the other nodes that relate to the lockey, above and below is level from level0 (The top of the tree) to level 6 of the tree
        FROM   
            inserted;
END
GO

I have created this trigger which takes any given node in a tree structure then adds itself with all child & parents nodes to a CTE table named final.

You will see I have multiple variable declarations eg. "Level1key"

I want to SET/SELECT each of the rows in 'final' into its corresponding variable EG.

"SELECT @Level1Key = LocKey from final where number = 1"  
"SELECT @Level2Key = LocKey from final where number = 2"  
"SELECT @Level3Key = LocKey from final where number = 3"  

However it does not pick up my CTE table 'final' after the first select.

Below is a photo of the error Problem problem

The reason I've tried to use multiple Select statements on a CTE is to try portray that i need to make use of multiple specific WHERE statements, i'm mainly looking for a way to achieve the same logic however, in one SELECT statement (Which the CTE is accessible in)

Thanks in advance

来源:https://stackoverflow.com/questions/61329064/setting-multiple-variables-from-a-cte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!