It may be the case this is only possible through looping, but who knows.
I’m in a Java coding environment using MS SQL Server, and I have been searching around and can’t seem to find a good way to INSERT records in say a transaction history table that we must keep updated for every action done in other tables upon issuing a single UPDATE command to UPDATE many records. For each UPDATE record updated, I would like to do an insert statement as well Essentially this is what we currently do, one by one, in code not using a stored procedure:
UPDATE table SET column1=null, column2=null, column3=0, column4=getDate() where column5=? and column6=1
...
//Insert transtable
createTransaction(tran, localConn);
...
INSERT INTO transtable ( a, b, c, d, e, f, g, h, i, " +
"j, k, l, m, n, o, p, q, r, s, " +
"t, u, v, w) " +
" VALUES ( ? ,? ,? ,? ,? ,? ,getDate() ,getDate() ,getDate() ,? ,? ,? ,? ,? ,? ,? ,getDate() ,getDate(),? ,? ,? ,?,? )
What I would like to do, is something like this instead for the UPDATE:
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[setFlags]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
UPDATE table1
SET column1 = '2', column2 = NULL
WHERE column1 = '1' or column1 = '2'
--FOR EACH UPDATED ROW IN THIS TABLE, CREATE A TRANSACTION IN TRANSTABLE
UPDATE table2
SET column1 = '0', column2 = NULL
WHERE column1 = '1'
--FOR EACH UPDATED ROW IN THIS TABLE, CREATE A TRANSACTION IN TRANSTABLE
UPDATE table3
SET column1 = '0', column2 = NULL
WHERE column1 = '1'
--FOR EACH UPDATED ROW IN THIS TABLE, CREATE A TRANSACTION IN TRANSTABLE
END
However, for each record updated (usually about ~100 records would be updated using this utility), about 100 INSERTs need to be done as well in our transtable table, which will also contain some of the data from the rows being updated.
Does anyone have any ideas? Resources out there? Etc.? I’m somewhat of a beginner, so sometimes I just don’t know what to search for. Lately I’ve been searching “for each UPDATE INSERT SQL” or “looping insert for each update”
I might need to do WHILE TSQL, but if there is a better way (it also sounds terribly inefficient), that’d be great to know:
http://msdn.microsoft.com/en-us/library/ms178642.aspx
You don’t tell what version of SQL Server you are using, neither what edition. If you are on 2008 Enterprise edition, have a look at Change Data Capture. There is a good introduction article here.