sql server - Capture single SQL Select statement -


hello sql coders , gurus

i’ve been coding sql few years , i’ve wondered how can grab first sql statement user executes against db each day say. user might have number of select statements highlighted when hit f5 (to execute 1 after another) i’m trying first one.

i’ve got couple of efforts below return please see.

example . ..


select 'blah first select'  select 'blah second select'   declare @sqltext varbinary(128) select @sqltext = sql_handle sys.sysprocesses spid = @@spid select  text  sys.dm_exec_sql_text(@sqltext) 

----------------------------------------------

which when executed returns of . ..

‘select 'blah first select'   select 'blah second select'   declare @sqltext varbinary(128) select @sqltext = sql_handle sys.sysprocesses spid = @@spid select  text  sys.dm_exec_sql_text(@sqltext)’ 

but result want first select statement executed db.. .. ‘select 'blah first select'‘

there may many long , complex select statements run (in batch) need first full sql statement string executed.

--================================================

example b . ..


select 'blah first select'  select 'blah second select'   select execquery.last_execution_time [date time], execsql.text [script] sys.dm_exec_query_stats execquery cross apply sys.dm_exec_sql_text(execquery.sql_handle) execsql --  execsql.text '%mhd%' order execquery.last_execution_time desc 

------------------------------------------------

this statement returns number of lines taking first still . ..

‘select 'blah first select'   select 'blah second select'   select execquery.last_execution_time [date time], execsql.text [script] sys.dm_exec_query_stats execquery cross apply sys.dm_exec_sql_text(execquery.sql_handle) execsql --  execsql.text '%mhd%' order execquery.last_execution_time desc’ 

and again result want . .. ‘select 'blah first select'

can please ?? hope i’ve explained myself , may , have fun little puzzle.

cheers nosinet

below example of xe trace:

create event session [firststatementtrace] on server  add event sqlserver.sp_statement_starting(     action(package0.collect_system_time,package0.event_sequence,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.session_id,sqlserver.sql_text,sqlserver.username)), add event sqlserver.sql_batch_starting(     action(package0.collect_system_time,package0.event_sequence,sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.session_id,sqlserver.sql_text,sqlserver.username))  add target package0.event_file(set filename=n'firststatementtrace') (max_memory=4096 kb,event_retention_mode=allow_single_event_loss,max_dispatch_latency=30 seconds,max_event_size=0 kb,memory_partition_mode=none,track_causality=off,startup_state=off) go  alter event session [firststatementtrace] on server state = start; go 

and here's example of how extract information trace data. there room improvement performance perspective don't have time tweak right now. should started, though.

if object_id(n'tempdb..#shredded_event_data', 'u') not null     drop table #shredded_event_data; go       event_data (         select             object_name, cast(event_data xml) event_data_xml         sys.fn_xe_file_target_read_file ( n'c:\program files\microsoft sql server\mssql11.sqlexpress\mssql\log\firststatementtrace*.xel', null, default, default )     )     , shredded_event_data (         select                object_name             , event_data_xml.value('/event[1]/action[@name="collect_system_time"][1]/value[1]', 'datetime2(3)') collect_system_time             , event_data_xml.value('/event[1]/action[@name="event_sequence"][1]/value[1]', 'bigint') event_sequence             , event_data_xml.value('/event[1]/action[@name="client_app_name"][1]/value[1]', 'sysname') client_app_name             , event_data_xml.value('/event[1]/action[@name="client_hostname"][1]/value[1]', 'sysname') client_hostname             , event_data_xml.value('/event[1]/action[@name="session_id"][1]/value[1]', 'smallint') session_id             , event_data_xml.value('/event[1]/action[@name="sql_text"][1]/value[1]', 'nvarchar(max)') sql_text         event_data     ) select       collect_system_time     , event_sequence     , client_app_name     , client_hostname     , session_id     , sql_text     , row_number() on (partition session_id order event_sequence) row_num #shredded_event_data shredded_event_data; 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -