Need help with a trigger

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 3
Reputation: ferretwoman is an unknown quantity at this point 
Solved Threads: 0
ferretwoman ferretwoman is offline Offline
Newbie Poster

Need help with a trigger

 
0
  #1
Oct 27th, 2008
New to triggers and SQL programming but I'm trying to figure it out by looking at code examples. I have a table, called child. My program inserts a new child into the child table and stores the parentid as a FK. What I need to do is when a new child is inserted into the table, I need to take the childid, parentid, and childname as well as the parentname , and put it in this new table called parentchild. I can get the childid, childname, and parentid from the inserted items to the child table, but I need to use the parentid to do a lookup in the parent table to get the actual string name of parent. Finally, I need to also insert in the parentchild table the name of parent/child separated by a slash.

I've attempted code below to do so. Could you tell me if I'm going in the right direction, and what I would need to do to actually get it to work as I've explained above. Here is my code:

  1. CREATE TRIGGER parent_child ON parentchild
  2. FOR INSERT
  3. AS
  4.  
  5. DECLARE @parent AS nvarchar(20);
  6. DECLARE @child AS nvarchar(20);
  7. DECLARE @parentid AS int;
  8. DECLARE @childid AS int;
  9.  
  10. BEGIN
  11.  
  12. SELECT * FROM INSERTED
  13. BEGIN
  14. SELECT @parentid AS parent FROM INSERTED
  15. SELECT @childid AS Child FROM INSERTED
  16. SELECT @parent AS parent.name FROM PARENT WHERE @parentid = parent.parentid;
  17. SELECT @child AS child FROM INSERTED;
  18.  
  19. INSERT INTO parentchild
  20. SET parentchild.child = @childid
  21. SET parentchild.parent = @parentid
  22. SET parentchild.parentchild = @parent + " / " + @child;
  23.  
  24. END
  25. GO

Thanks!

Linda
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Need help with a trigger

 
0
  #2
Oct 27th, 2008
you need to correct the insert statement portion

  1. INSERT INTO parentchild(child, parent, parentchild)
  2. VALUES(@child, @parentid, @parent + " / " + @child)
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: ferretwoman is an unknown quantity at this point 
Solved Threads: 0
ferretwoman ferretwoman is offline Offline
Newbie Poster

Re: Need help with a trigger

 
0
  #3
Oct 27th, 2008
Thanks for the help so far.

I've gotten this far with it now:
  1. CREATE TRIGGER parent_child ON parentchild
  2. after INSERT
  3.  
  4. AS
  5.  
  6. DECLARE @parent AS nvarchar(20)
  7. DECLARE @child AS nvarchar(20)
  8. DECLARE @parentid AS numeric
  9. DECLARE @childid AS numeric
  10. DECLARE @parentchild AS nvarchar(40)
  11.  
  12.  
  13. BEGIN
  14.  
  15. SELECT @parentid = i.parent, @childid = i.child
  16. @parent = p.parent, @child=i.child,
  17. FROM inserted i, parent p
  18. WHERE p.parentid = i.parent
  19. @parentchild = (@parent + " / " + @child)
  20.  
  21.  
  22. INSERT INTO parentchild(child, parent, parentchild)
  23. VALUES(@childid, @parentid, @parentchild)
  24.  
  25.  
  26. END
  27. GO

I'm still getting a syntax error:
Incorrect syntax near '@parent'.
which I guess is referring to my @parentchild = (@parent + "/" + @child) line. Can anyone point out the problem with that for me?

Thanks!

Linda
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Need help with a trigger

 
0
  #4
Oct 27th, 2008
try adding the word AND in your where statement
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: ferretwoman is an unknown quantity at this point 
Solved Threads: 0
ferretwoman ferretwoman is offline Offline
Newbie Poster

Re: Need help with a trigger

 
0
  #5
Oct 27th, 2008
Thanks! Finally figured it out - a few things missing, a few things changed. Final code that compiles (haven't tested to make sure it runs properly yet though) in case it would help others.

  1. CREATE TRIGGER parent_child ON parentchild
  2. after INSERT
  3.  
  4. AS
  5.  
  6. DECLARE @parent AS nvarchar(20)
  7. DECLARE @child AS nvarchar(20)
  8. DECLARE @parentid AS numeric
  9. DECLARE @childid AS numeric
  10. DECLARE @parentchild AS nvarchar(40)
  11.  
  12.  
  13. BEGIN
  14.  
  15. SELECT @parentid = i.parent, @childid = i.child,
  16. @parent = p.name, @child=i.child
  17. FROM inserted i, parent p
  18. WHERE p.parentid = i.parent
  19.  
  20. SET @parentchild = (@parent + ' / ' + @child)
  21.  
  22.  
  23. INSERT INTO parentchild(child, parent, parentchild)
  24. VALUES(@childid, @parentid, @parentchild)
  25.  
  26.  
  27. END
  28. GO

Thanks!

Linda
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Need help with a trigger

 
0
  #6
Oct 27th, 2008
A suggestion would be not to use numeric unless you needed to, i'm assuming parentid and childid could be int

also nvarchar is used for multilingual data, if you have that keep it, otherwise use varchar
Last edited by dickersonka; Oct 27th, 2008 at 4:37 pm.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC