Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UPDATE table2 with value from table1 on INSERT into table1


UPDATE table2 with value from table1 on INSERT into table1

Author
Message
kabaari
kabaari
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 365
Good morning all,

And thank i advance. I need to "UPDATE table2.ID with value from table1.ID on INSERT into table1"

Table1.ID is Identity Insert but table2.ID is Nullable. I want when I insert values into table1 that table2 get the same ID from table for future queries.
UPDATE    tbl_Assembly_Delay1.ID
SET tbl_Assembly_Production.ID = tbl_Assembly_Delay1.ID
FROM tbl_Assembly_Production
WHERE (tbl_Assembly_Production.ID = tbl_Assembly_Delay1.ID)



Thanks again,

DJ Khalif
matt6288
matt6288
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1230 Visits: 1351
It sounds like you need an insert trigger on table 1. I can't rattle off the syntax for this off the top of my head but you can look up "triggers" in books online and see if that will do what you are needing.
SQLZ
SQLZ
SSC Eights!
SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)

Group: General Forum Members
Points: 930 Visits: 940
kabaari (8/26/2009)
Good morning all,

And thank i advance. I need to "UPDATE table2.ID with value from table1.ID on INSERT into table1"

Table1.ID is Identity Insert but table2.ID is Nullable. I want when I insert values into table1 that table2 get the same ID from table for future queries.
UPDATE    tbl_Assembly_Delay1.ID
SET tbl_Assembly_Production.ID = tbl_Assembly_Delay1.ID
FROM tbl_Assembly_Production
WHERE (tbl_Assembly_Production.ID = tbl_Assembly_Delay1.ID)



Thanks again,

DJ Khalif


Something doesn't quite seem right here.....

If the ID value in table2 is nullable how will you ever be able to link up table2 with table1?

Let's follow this through the logical sequence....

1. You insert a record into table1.
2. You can use scope_identity() to get the identity value of the record you just inserted.
3. So now you want to update table2's ID column with this ID you just inserted. But which row are you going to update? In other words, what conditions are you using to determine which row you want to update in table2?

Hope that makes sense.

Karl
source control for SQL Server
kabaari
kabaari
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 365
Thanks in advance:

I realized that logically I was incorrect. Below is where I attempted to correct my mistake. Now I get: "Incorrect syntax near 'Code'".

INSERT into tbl_Assembly_Delay(ID, Code, DelayTime) 
( '#Trim(FORM.Code)#',
'#Trim(FORM.DelayTime)#', SELECT ID
FROM tbl_Assembly_Production
WHERE tbl_Assembly_Production.WorkOrder = '#FORM.WorkOrder#');


kabaari
kabaari
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 365
I made another change and I get this: Incorrect syntax near ','.
INSERT into tbl_Assembly_Delay(ID, Code, DelayTime) 
SELECT ID
FROM tbl_Assembly_Production
WHERE tbl_Assembly_Production.WorkOrder = '#FORM.WorkOrder#' AND ('#Trim(FORM.Code)#' ,
'#Trim(FORM.DelayTime)#');


SQLZ
SQLZ
SSC Eights!
SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)SSC Eights! (930 reputation)

Group: General Forum Members
Points: 930 Visits: 940
Try this:



INSERT into tbl_Assembly_Delay(ID, Code, DelayTime)
SELECT ID, '#Trim(FORM.Code)#', '#Trim(FORM.DelayTime)#'
FROM tbl_Assembly_Production
WHERE tbl_Assembly_Production.WorkOrder = '#FORM.WorkOrder#' ;




Karl
source control for SQL Server
kabaari
kabaari
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 365
Thanks. That was the same thing I came up with:
INSERT INTO tbl_Assembly_Delay1(ID, Code, DelayTime) 
SELECT ID , '#Trim(FORM.Code)#', '#Trim(FORM.DelayTime)#'
FROM tbl_Assembly_Production
WHERE tbl_Assembly_Production.WorkOrder = '#FORM.WorkOrder#'




I also ran into some other issues but figured those too. Thanks again.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search