I am using the Context_Info() variable to keep track of the user that is executing a stored procedure and free-form sql. When troubleshooting issues on this server everyone session comes through. I would like to be able to bring in the value of the context_info() variable and filter based on it.
You can use the UserConfigurable Events along with sp_trace_generateevent (EventId's 82-91) when setting the context_info() to output the values to the trace. Your option is to either do that, or trace the statements setting the context_info(). You won't be able to get the value any other way unless you write a process to dump the output of sys.dm_exec_sessions in a loop while the trace is running:
select session_id, cast(context_info as varchar(128)) as context_info
from sys.dm_exec_sessions
where session_id > 50 -- user sessions
for SQL 2000 you can use sysprocesses:
select spid, cast(context_info as varchar(128)) as context_info
from sysprocesses
where sid > 50 -- user sessions
Related
Why this code gives 1 in new query window of SSMS?
select ##ROWCOUNT
SSMS issues several queries after it creates a connection. You can see this using SQL Server Profiler
on my system,
select ##spid;
select SERVERPROPERTY('ProductLevel');
is the last query executed.
When you create a new connection, your SSMS window automatically sets a number of session variables for you.
Sample trace of a new session being created:
select ##spid;
select SERVERPROPERTY('ProductLevel');
As a test you can execute the following statement, and confirm that the result will always be 1.
select ##spid;
select SERVERPROPERTY('ProductLevel');
select ##ROWCOUNT
When I write pl/sql procedure in Oracle and suspect that it possible will run long enough I usually use DBMS_APPLICATION_INFO.SET_MODULE ('Some calculation', i||' records of '||total_count||' were processed') in order to have ability to monitor calculation process.
Is there something similar in SQL Server to monitor calculation process through system views?
To view progress in a long running SQL job I normally just intersperse PRINT or RAISERROR messages.
RAISERROR ('Some calculation %i records of %i were processed',0,1,50,100) WITH NOWAIT;
These info messages can be retrieved and displayed by the executing application (printed in the messages tab of SSMS for example).
Sounds like the Oracle thing is a bit different. You can stuff arbitrary 128 byte messages in CONTEXT-INFO
DECLARE #Msg BINARY(128) = CAST('Some calculation 50 records of 100 were processed' AS BINARY(128))
SET CONTEXT_INFO #Msg
And then retrieve it as
SELECT CAST(context_info AS CHAR(128))
FROM sys.dm_exec_sessions
WHERE session_id = 55 /*Change as needed*/
Another possibility would be to fire a custom profiler event with EXEC sp_trace_generateevent that you could then capture.
But probably easier to just add a logging table that your steps get inserted into (may need to query this with NOLOCK if your steps are running inside a transaction).
If I understand correctly, with stored procedure result is also returned a message called DONE_IN_PROC
Fragment From MSDN:
... SET NOCOUNT ON eliminates the sending of DONE_IN_PROC messages to the
client for each statement in a stored procedure.
Is there a way to access this message somehow in Management Studio after executing stored procedure and read i.e. number of rows affected.
(I am asking out of curiosity and I know there are other ways to get number of rows affected)
Certain (if not all) Database APIs make the TDS layer's DONE_IN_PROC message accessible. If ODBC is the chosen Database API, see the http://technet.microsoft.com/en-us/library/ms130812.aspx discussion about SQLRowCount, SQLGetStmtAttr, and NOCOUNT.
Late to the party but are you just after the int from the message?
create proc delme as
begin
select * from sys.databases
end
go
set nocount on
go
exec delme --returns "(7 row(s) affected)" on the message channel
select ##ROWCOUNT --returns 7
I Configured a distribution in SQL Server 2008 using both Wizard and T-SQL but after it when I want to remove it Using Wizard (right clicking on Replication and choosing 'Disable Publishing and Distribution...') or executing following command with and without its parameters:
exec sp_dropdistributor #no_checks = 1 -- no new results with #ignore_distributor = 1
this Error would be presented:
Msq 21122, Level 16, State 1, Procedure sp_dropdistributiondb Line 124
Cannot drop the distribution database 'lobloblob' because it is
currently in use.
I didn't publish any thing, didn't configure any subscription but gave this error
what should I do ?
Try this:
SELECT spid FROM sys.sysprocesses WHERE dbid = db_id('distribution')
Kill the spid and try again. Now it should work.
I used the following scripts:
SELECT spid FROM sys.sysprocesses WHERE dbid = db_id('distribution')
and found that the session_id of current session (which contains the distribution configuration script) doesn't allow to disable distribution so i suggest this script to kill running spid to drop distribution:
use [master]
declare #spid varchar(10)
select #spid=spid from sys.sysprocesses where dbid = DB_ID('distribution')
while ##ROWCOUNTS <> 0
exec ('KILL ' + #spid)
exec sp_dropdistributor #no_checks = 1
My guess would be that the distribution cleanup job is causing the problem. But, to check, prepare to execute the sp_dropdistributor in one window in SSMS and note the session_id of the window. In a second, prepare to run select session_id from sys.dm_os_waiting_tasks where blocked_session_id = <spid from window 1>. Back in window 1, run the proc and then switch back to window 2 and run the select. it'll tell you the session_ids of the sessions blocking the drop of the database.
I'm using this query to find some queries that have been running for a long time:
SELECT sqltext.TEXT,
req.session_id,
req.status,
req.command,
req.cpu_time,
req.total_elapsed_time
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
order by req.total_elapsed_time des
Currently, we have some xp_cmdshell things that seem to be stuck (we use it to call bcp.exe for bulk exporting in jobs). However, the output of sys.dm_exec_sql_text() only outputs "xp_cmdshell" and not the parameters -- I really would like to see exactly what commands xp_cmdshell is running so I can track down the issues.
Is there any way to do that in SQL server?
EDIT: The active sessions are calling xp_cmdshell from a stored procedure. E.g.:
EXEC usp_xxx -> calls EXEC usp_yyy -> calls xp_cmdshell.
So, the output of DBCC InputBuffer is the call to usp_xxx which is not what I want.
I'm not sure if the details are available in any of the dynamic management views, but you could take your session_id and use it with DBCC INPUTBUFFER to get the details you seek.
DBCC INPUTBUFFER(83)
would return something like this as an example
EventType Parameters EventInfo
-------------- ---------- ------------------------
Language Event 0 EXEC xp_cmdshell 'sc /h'