| | |
Need help with a trigger
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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:
Thanks!
Linda
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:
MS SQL Syntax (Toggle Plain Text)
CREATE TRIGGER parent_child ON parentchild FOR INSERT AS DECLARE @parent AS nvarchar(20); DECLARE @child AS nvarchar(20); DECLARE @parentid AS int; DECLARE @childid AS int; BEGIN SELECT * FROM INSERTED BEGIN SELECT @parentid AS parent FROM INSERTED SELECT @childid AS Child FROM INSERTED SELECT @parent AS parent.name FROM PARENT WHERE @parentid = parent.parentid; SELECT @child AS child FROM INSERTED; INSERT INTO parentchild SET parentchild.child = @childid SET parentchild.parent = @parentid SET parentchild.parentchild = @parent + " / " + @child; END GO
Thanks!
Linda
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
you need to correct the insert statement portion
MS SQL Syntax (Toggle Plain Text)
INSERT INTO parentchild(child, parent, parentchild) VALUES(@child, @parentid, @parent + " / " + @child)
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
Thanks for the help so far.
I've gotten this far with it now:
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
I've gotten this far with it now:
MS SQL Syntax (Toggle Plain Text)
CREATE TRIGGER parent_child ON parentchild after INSERT AS DECLARE @parent AS nvarchar(20) DECLARE @child AS nvarchar(20) DECLARE @parentid AS numeric DECLARE @childid AS numeric DECLARE @parentchild AS nvarchar(40) BEGIN SELECT @parentid = i.parent, @childid = i.child @parent = p.parent, @child=i.child, FROM inserted i, parent p WHERE p.parentid = i.parent @parentchild = (@parent + " / " + @child) INSERT INTO parentchild(child, parent, parentchild) VALUES(@childid, @parentid, @parentchild) END 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
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
try adding the word AND in your where statement
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
Thanks!
Linda
MS SQL Syntax (Toggle Plain Text)
CREATE TRIGGER parent_child ON parentchild after INSERT AS DECLARE @parent AS nvarchar(20) DECLARE @child AS nvarchar(20) DECLARE @parentid AS numeric DECLARE @childid AS numeric DECLARE @parentchild AS nvarchar(40) BEGIN SELECT @parentid = i.parent, @childid = i.child, @parent = p.name, @child=i.child FROM inserted i, parent p WHERE p.parentid = i.parent SET @parentchild = (@parent + ' / ' + @child) INSERT INTO parentchild(child, parent, parentchild) VALUES(@childid, @parentid, @parentchild) END GO
Thanks!
Linda
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
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
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
www.houseshark.net
![]() |
Similar Threads
- Trigger help (MS SQL)
- problem with trigger (MS SQL)
- SQL trigger based on a true/false field value (MS SQL)
- Computed column in view VS trigger on update (MS SQL)
- Hi - how do you trigger a macro to run when a cell is updated in Excel? (Windows Software)
- Trigger onChange manually (HTML and CSS)
Other Threads in the MS SQL Forum
- Previous Thread: How do I get ONLY DATATYPE and NAME of all the columns available in given table_name
- Next Thread: Group by with aggregate functions
| Thread Tools | Search this Thread |
connectingtodatabaseinuse count cursor data database datepart deadlock delete_trigger highperformancecomputing hpc hpcserver2008 ibm iis loop maximum microsoft ms mssql multithreading news number permission query reporting result server services sets source sql sqlserver sqlserver2005 supercomputing uniqueid update view






