Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get the actual query text being fired for a particular operation. I'm trying to implement a trigger on a table where i need to store the query also. Expand / Collapse
Author
Message
Posted Monday, January 5, 2009 4:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 4, 2012 3:09 AM
Points: 7, Visits: 81
Hi All,

Can some one help me in getting the actual query text being used to perform an operation.
I'm doing a Outer Apply with sys.dm_exec_sql_text(deqs.sql_handle),
though this is giving correct info but not in the desired manner.
I'm doing this implementation inside a trigger.
In case of a stored proc call which has some internal DML operations in it, i want the query text as "Exec Procname 'xxxx', 'xxxxx'" which is the actual one which is fired. But the DMV which i've specfied above is giving me the entire content of the SP being fired.

Request help to address my issue.

Thanks in advance.

Srinivas

Post #629682
Posted Monday, January 5, 2009 3:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
Is there a reason to do this in a trigger, as opposed to working from a trace? A trace will give you exactly what you're looking for, the proc name and parameter values. What are you doing with the data in the trigger?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #630164
Posted Monday, January 5, 2009 3:18 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, July 22, 2013 11:59 AM
Points: 477, Visits: 719
DBCC InputBuffer (SPID) will give you the text sent to the SQL Server, if that helps.

Atlantis Interactive - SQL Server Tools
My blog
Why I wrote a sql query analyzer clone
Post #630171
Posted Monday, January 5, 2009 5:07 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 3:14 PM
Points: 4,363, Visits: 9,545
You can get the object using the objectid column - but, if you want the actual stored procedure call you need to get it from a trace.

Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #630228
Posted Tuesday, January 6, 2009 3:25 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 4, 2012 3:09 AM
Points: 7, Visits: 81
Thank you for the reply.
But, can DBCC Inputbuffer(SPID) in an inline query?

I'm actually trying to monitor the various DML opertaions being performed on a table.
In the update trigger, i'm capturing all the old values of the columns being updated from the table and along with, host name from which query is fired, sql statement used, updated columns (XML format) etc... into a log table.

My query is as below:

INSERT INTO Tablename_Log
SELECT D.Column1 , D.Column2 , D.Column3 , D.Column4 , D.Column5 ,
@FldsUpdated, B.Text
, GetDate(),Host_Name(),A.Session_Id
FROM DELETED D , (select Session_Id, sql_handle from sys.dm_exec_requests where session_id=@@spid) A
OUTER APPLY sys.dm_exec_sql_text(A.sql_handle) B

Thanks And Regards,
Srinivas
Post #630402
Posted Tuesday, January 6, 2009 4:02 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, July 22, 2013 11:59 AM
Points: 477, Visits: 719
sinureddi (1/6/2009)

But, can DBCC Inputbuffer(SPID) in an inline query?

No - it just gives you what was sent to the server - I wasn't 100% sure where you were coming from so thought I would mention it just in case! :D


Atlantis Interactive - SQL Server Tools
My blog
Why I wrote a sql query analyzer clone
Post #630425
Posted Tuesday, January 6, 2009 9:55 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
I don't know a way to get exactly what you're looking for in one solution.

I'd log the update (as you're doing), and run a trace. From time-stamps, SPIDs, etc., you should then be able to join the two together after the fact. Trace will give you exact command run, log will give you the rest of it.

It's not ideal, but it will work.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #630737
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse