views:

445

answers:

2

I have a table that has a Trigger associated with it for Update, Insert and Delete. The Trigger works fine and executes. Here is the code for my trigger:

CREATE trigger [dbo].[trg_audit_TableName]
ON [dbo].viewLayers
FOR INSERT, UPDATE, DELETE
AS
SET NOCOUNT ON
declare @inputbuffer table (EventType nvarchar(30),Parameters int,EventInfo nvarchar(4000))
insert into @inputbuffer exec('dbcc inputbuffer('+@@Spid+')')
declare @ins int
declare @del int
select @ins = count(*) from inserted
select @del = count(*) from deleted

insert into audit_TableName (eventtime, CurrentMachine, appName,  CurrentUser, eventtype, tsql)
select getdate(), host_name(), APP_NAME(), suser_sname(), 
 case 
  when isnull(@ins, 0) > 0 and isnull(@del, 0) > 0 then 'update' 
  when isnull(@ins, 0) > 0 and isnull(@del, 0) = 0 then 'insert' 
  else 'delete' 
 end, 
 EventInfo
from @inputbuffer

Now, if I run a stored procedure from the Management Studio, the TSWL data inserted into the table looks like this:

sp_TestInsert 'paramdata 1', 'paramdata 2'

But, if I run the procedure from my .NET appliation using an SQLCommand object, the TSQL field doesn't show the parameters:

sp_TestInsert;1

If I open up the profiler for this stored procedure when it is executed from .NET this is what I see:

exec sp_TestInsert @val1='paramdata 1',@val2='paramdata 2'

Yet, the audit table still shows

sp_TestInsert;1

Is there a way that I can retrieve the parameters passed with the Stored Procedure?

EDIT: Depending on the stored procedure, it will insert / update / delete data in a table that has the trigger

EDIT 2: Here's a very brief example of the .NET Execution ('conn' is my SQLConnection object):

Using cmd As SqlCommand = conn.CreateCommand 
     cmd.CommandType = CommandType.StoredProcedure 
     cmd.CommandText = "sp_TestInsert" 
     cmd.Parameters.AddWithValue("@val1", "paramdata 1")
     cmd.Parameters.AddWithValue("@val2", "paramdata 2") 
     cmd.ExecuteNonQuery() 
 End Using
+1  A: 

This is executing the stored procedure with no parameters. The semi-colon signifies the end of a statement in sql.

sp_TestInsert;1

I think this is what you are looking for

sp_TestInsert 1

This is assuming that "sp_TestInsert;1" is what you are passing from your code to sql server.

Kevin
This is how the data is provided by the EventInfo. If I open up the profiler for this stored procedure when it is executed from .NET this is what I see: exec sp_TestInsert @val1='paramdata 1',@val2='paramdata 2'Yet, the audit table still shows sp_TestInsert;1For the SQLCommand object, I set the CommandText to "sp_TestInsert" and then add Parameters (cmd.Parameters.AddWithValue). Not sure if that matters.....
hacker
What are you setting the command type to?
Kevin
A: 

I ended up modifying the trigger to also include the inserted / deleted data like so:

 declare @insData varchar(max)  
 set @insData = (select * from inserted for xml auto)  
 declare @delData varchar(max)  
 set @delData = (select * from deleted for xml auto)

And then inserted @insData and @delData into the audit table. This now shows what was changed.

hacker