0
 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

  • A CTE is an **expression** (Common Table Expression) not an object. It can only be referenced *in the statement it is declare in*. Assign all of your values to your variables in one statement, not 4. This `TRIGGER` looks like an XY problem though. – Thom A Apr 20 '20 at 18:09
  • @Larnu I have tried doing that however each assignment needs to make use of a specific where statement, do you perhaps know a work around where id meet the same logic with one SELECT statement ? – Matthew Leslie Apr 20 '20 at 18:23
  • @Larnu It is most definitely an XY problem however its not up to me :( – Matthew Leslie Apr 20 '20 at 18:30
  • If its impossible please still let me know. Thanks again – Matthew Leslie Apr 20 '20 at 18:35
  • If it is an XY Problem, you really should be telling us what the *real* problem you're tryimg to solve is. You can achieve what you want with a single statement, but that trigger is very likely going to be slow, and slow triggers kill performance. – Thom A Apr 20 '20 at 19:04
  • @Larnu When I say its an XY problem we have a legacy database that the company still uses for its front end system however Im doing addtional app development and the legacy database is not sufficient so everytime that database is used which is not often i need the actions to be replicated in the new database (this specific trigger is pushing too) The company refuses to leave the front end system they've been using since 1998 – Matthew Leslie Apr 20 '20 at 19:14
  • Would creating a #temp table from the CLE expression 'final' work if i dropped the table at the end of the table ? and then obviously preforming all the inserts from the #temp table ? – Matthew Leslie Apr 20 '20 at 19:15
  • @Larnu could you please expand on how I could achieve the same logic through a single statement. – Matthew Leslie Apr 20 '20 at 19:16
  • I see that it's pushing, but the CTE, which is recursive, seems unlikely to be needed. That is why I am asking what you are *really* fry Ng to achieve here. I don't want to give a bad answer when I can likely push you to a *far better* solution. – Thom A Apr 20 '20 at 19:16
  • @Larnu Please refer to this question which was my additional problem, really appreciate you being so helpful and trying to help me with the best solution. https://stackoverflow.com/questions/61233575/sql-server-trigger-i-need-to-move-through-a-hierarchical-tree-structure-from-an – Matthew Leslie Apr 20 '20 at 19:18
  • @Larnu please tell me if i need to explain more, I have no problem doing that – Matthew Leslie Apr 20 '20 at 19:20
  • That doesnt really explain anything I'm afraid. Perhaps some changes dimmable sample data and expected results will help me help you, along with a description to f your *real* goal. – Thom A Apr 20 '20 at 19:33
  • @Larnu could you please help me understand why the sample data and expected results on the other post (referenced in comments) are not sufficient as im very new to stack overflow. When you say my real goal are you referring to the reason why i want to create the trigger ? – Matthew Leslie Apr 20 '20 at 19:43
  • The sample data there are in rows, do you really have such denormalised data? If so, that's the real problem. If not, it's not representative of your actual data. – Thom A Apr 20 '20 at 19:53
  • I don't think the real data is in rows like that. I think the OP is just representing it in that format – Nick.Mc Apr 21 '20 at 12:48
  • Does this answer your question? [SQL Server trigger (I need to move through a hierarchical tree structure from any given node)](https://stackoverflow.com/questions/61233575/sql-server-trigger-i-need-to-move-through-a-hierarchical-tree-structure-from-an) – Nick.Mc Apr 22 '20 at 01:45
  • Yes Nick, thanks once again. – Matthew Leslie Apr 22 '20 at 13:12

0 Answers0