SQL Clone
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
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 367
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
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2513 Visits: 1376
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
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5274 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
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 367
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
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 367
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
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5274 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
Say Hey Kid
Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)Say Hey Kid (704 reputation)

Group: General Forum Members
Points: 704 Visits: 367
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