DBCC INPUTBUFFER(@@SPID)

Hi, experts -
I needed to find out what statement updated a column value by adding  below trigger:

create TRIGGER LogMySQL
ON sample_table
AFTER   UPDATE  
AS
 
 INSERT INTO SQLLog_sample (language_event, parameters, event_info)
 EXEC('DBCC INPUTBUFFER(@@SPID);');

Problem is it works when a single row is updated; in case of multiple row updates this trigger throws an error as:

Subquery returned more than 1 value. This is not permitted when the subquery follows....

Any ideas ? How can my trigger be modifed ? Need at earliest, thanks.
ivan_belalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
You can put the results from DBCC INPUTBUFFER (@@SPID) into a #temp table then populate your table from that temp table not do it directly. This should work for single and batch sample_table update
0
Aaron ShiloChief Database ArchitectCommented:
hi

first of all this is to be expected since sql server trigger are per sentance and not FOR EACH ROW like in oracle for example.

try using lcohan's advice

0
cyberkiwiCommented:
Wouldn't the result of that just log

EventType      Parameters      EventInfo
Language Event      0      EXEC('DBCC INPUTBUFFER(@@SPID);');

?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

cyberkiwiCommented:
Okay, I set up a test table and the update trigger.

The query you use is fine, contrary to what I believed.  There is NO subquery here at all, so the error is somewhere else in your code.
0
ivan_belalAuthor Commented:
There is no sub-query in trigger, but when > 1 row is updated on base table - trigger fails with error. Single row update on base table is ok. Temp table solution behaves same way. Can someone test out modified trigger code and send me some suggestion ?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I checked with this code sample that the error does not come from your trigger itself:

create table sample_table ( id int )
go
create table SQLLog_sample ( language_event varchar(100), parameters varchar(100), event_info varchar(1000))
go
create TRIGGER LogMySQL
ON sample_table
AFTER   UPDATE  
AS
 
 INSERT INTO SQLLog_sample (language_event, parameters, event_info)
 EXEC('DBCC INPUTBUFFER(@@SPID);'); 
go
insert into sample_table values(1)
insert into sample_table values(2)
go
update sample_table  set id = id + 1
go
select * from SQLLog_sample
go

drop table  SQLLog_sample
go 
drop table sample_table

Open in new window


so, I have to presume that you have (another) trigger on the SQLLog_sample table?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ivan_belalAuthor Commented:
This is a nice test. Issue I need to find out with this trigger turned on in my environment (on a real table) there is an update stmt fails on sub-query returning more than one row message; w/o trigger on - update stmt works fine. Team member suggests to this trigger for failure !!!
0
lcohanDatabase AnalystCommented:
If the server was never re-started since trigger got created you should be able to see when and who created it by checking "Schema change history" on that particular database - right click it in SSMS, select Reports - > Standard Reports - >  "Schema change history"

Or get it from the system table by executing script below against your database where replage trigger_name with the actual triger name:

select * from sys.triggers tr where name = 'triger_name'

To test the trigger with temp table please see code below:


create table SQLLog_sample ( language_event sysname, [parameters] sysname, event_info varchar(max))
go

create table test_trigger ( id int )
go

create TRIGGER test_dbcc
ON test_trigger
AFTER   UPDATE  
AS
 
      create table #temp ([EventType] sysname, [Parameters] sysname, [EventInfo] varchar(max))
      insert #temp execute ('DBCC INPUTBUFFER(@@SPID)')
      INSERT INTO SQLLog_sample (language_event, parameters, event_info) select * from #temp;
      drop table #temp;
go


insert into test_trigger values(1)
insert into test_trigger values(2)
insert into test_trigger values(3)
go

update test_trigger set id = id + 1 where id in (1,2,3);
go
select * from test_trigger;
go

select * from SQLLog_sample;
go

drop table SQLLog_sample;
drop table test_trigger;
go

 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.