2
\$\begingroup\$

I'm using sql server and I'm trying to update certain columns in a table but before it updates the data, I want to insert the data into another table.

This is my code

declare @table1 table (Id int, Name varchar(100))
declare @temp table (Id int, Name varchar(100), CreatedOn datetime default getdate())
insert into @table1 values (1, 'Albert')
insert into @table1 values (2, 'Alex')
insert into @table1 values (3, 'Alas')
insert into @table1 values (4, 'Bob')
update @table1
set Name = 'B' + Name
output Deleted.Id, Deleted.Name, getdate() into @temp
where Name like 'A%'
select * from @table1
select * from @temp

Above code seems to be working correctly. Please let me know if I have to change anything.

asked May 20, 2013 at 12:28
\$\endgroup\$
2
  • \$\begingroup\$ Is this happening on a set schedule, or every time the data is being updated? \$\endgroup\$ Commented May 21, 2013 at 19:19
  • \$\begingroup\$ Not actually a schedule nor every time when data is updated... its a custom script. its executed when something bulk has to be deactivated. \$\endgroup\$ Commented May 24, 2013 at 16:33

1 Answer 1

1
\$\begingroup\$

You don't need to change anything as long as the code performs what you need. If you want to insert records to another table on every update (and not only in this place) you should probably take a look at triggers

answered May 20, 2013 at 12:44
\$\endgroup\$
1
  • \$\begingroup\$ actually I need this data only for this script... I so might not be going for triggers. thank you for the suggestion. \$\endgroup\$ Commented May 20, 2013 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.