Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

foreach loop in sql server trigger

Posted on 2014-03-21
4
Medium Priority
?
4,839 Views
Last Modified: 2014-04-03
Hi ,

I have a table with 50 columns . I need to store insert ,update log in audit table.

for that i am writing triggers.

insert into HC_Audit
           (User_ID,System_Time,Affected_Table,Affected_Field,Old_Data,New_Data)
      values(@user_id,@InsertDate,'Care','Emp_ID','',@Emp_id);


can anyone help me to run for each loop to get column name and column value so that i can insert data into hc_Audit table from insert , update , delete trigger .
0
Comment
Question by:Vikash p
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 39945927
For INSERT you just:

SELECT * FROM INSERTED or even better:

insert into HC_Audit
           (User_ID,System_Time,Affected_Table,Affected_Field,Old_Data,New_Data)
SELECT User_ID,System_Time,Affected_Table,Affected_Field,Old_Data,New_Data FROM INSERTED

assuming the tables have identical structure.


For DELETE is the same just replace INSERTED with DELETED - these are internal SQL tables used during the standard DML operations.

For UPDATE is a bit more tricky as an UPDATE is done in SQL like a INSERT and DELETE so the INSERTED table in a update trigger will have all NEW values going in that row(s) and the DELETED table will have all the OLD values that were updated by the new values coming from INSERTED.

Based on this you can chose to log both operations  and keep old/new values for an update.

I also suggest in your Audit table to add a TriggerAction column for instance to be able to tell what operation was logged - Insert/Delete/Update or...you can have one table for each of them but that would mean three audit tables for 1 parent.

"Using the inserted and deleted Tables"
http://technet.microsoft.com/en-us/library/ms191300(v=sql.100).aspx

http://www.mssqltips.com/sqlservertip/2342/understanding-sql-server-inserted-and-deleted-tables-for-dml-triggers/
0
 

Author Comment

by:Vikash p
ID: 39946211
I have 50 columns in a table can any one give me code to use foreach loop in sql server in which i can get coulumn name and value ..
0
 
LVL 35

Accepted Solution

by:
David Todd earned 2000 total points
ID: 39947028
Hi,

You don't need to loop.

All the values you need are available in the pseudo tables inserted and deleted.

That is, the new or inserted data is available as
select * from inserted

See the suggested code from icohan above.

BTW there is NO way to guarantee that the trigger will have one row. If you do a bulk update and touch each row in the table, there will be that many rows in inserted ...

HTH
  David
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 2000 total points
ID: 39949229
Hi,

This appears to be a very similar question to
http://www.experts-exchange.com/Database/MS-SQL-Server/SQL-Server-2005/Q_28394418.html

... where you have received much the same advice as you are getting here.

Regards
  David
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question