Click here to Skip to main content
16,020,080 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi, I have this query that is updating a table1.Table1 has another column called OldReference with this query it is being set to NULL, now I need to set this column as the value of the NewReference where then the NewRefernce will be set based on the select query.




INSERT INTO [Table1] (
    Class, DateAdded, User, [Type], NewReference
) (
    SELECT Class_ID, GetDate(), @User_ID, table2_id, @Current_Lot
    FROM Table2
        CROSS JOIN @StripList
    WHERE MType = 'Add New'
)


What I have tried:

INSERT INTO [Table1] (
    Class, DateAdded, User, [Type], OldReference, NewReference
) (
    SELECT Class_ID, GetDate(), @User_ID, table2_id, T1.OldReference, @Current_Lot
    FROM Table1 T1, Table2
        CROSS JOIN @StripList
    WHERE MType = 'Add New'
)
Posted
Updated 30-Jan-18 2:43am
v3
Comments
MayurDighe 26-Jan-18 4:22am    
You said, "...updating a table1...". Then try UPDATE query instead of INSERT query. Refer this link https://goo.gl/ta8NNi
ZurdoDev 26-Jan-18 9:58am    
I do not understand what you are asking.
RedDk 26-Jan-18 14:42pm    
Which is it? In the Object Explorer panel if you expand the [Table1] "Columns" the list of structural fields available will provide a clue as to what's what ... for use in a SELECT clause.
[no name] 30-Jan-18 9:32am    
How can you expect a insert query when you want to update?

1 solution

SQL
UPDATE T1
   SET DateAdded=GETDATE() --I believe you need a field for UpdateDate
    , User=@USER_ID
    ,Type=table2_id
    ,OldReference =t1.newReference
    ,NewReference=@Current_Lot
FROM Table1 T1, Table2
        CROSS JOIN @StripList
    WHERE MType = 'Add New'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900