Thursday, 9 May 2013

Finding Unique Customers Who have AccountNumber Null

SELECT customerid FROM T_TableName
GROUP BY customerid HAVING
SUM   (CASE
WHEN AccountNumber IS NULL THEN 0
ELSE 1
END
)=0
GO

The above query return unique customerid from table T_Tablename if customerid is more than one.

Also it only return if the customerid is having NULL value for all the records. If they have proper account number instead of NULL, the query would not return the customerid.

FETCH API_CURSOR and sp_cursorfetch - SQL Text Finder

DBCC INPUTBUFFER (53)

This returns:
FETCH API_CURSOR0000000000000004
-------------------------------
SELECT t.text
FROM sys.dm_exec_connections c
CROSS APPLY sys.dm_exec_sql_text (c.most_recent_sql_handle) t
WHERE session_id = 53

This returns:

FETCH API_CURSOR0000000000000004
-------------------------------
SELECT c.session_id, c.properties, c.creation_time, c.is_open, t.text
FROM sys.dm_exec_cursors (53) c
CROSS APPLY sys.dm_exec_sql_text (c.sql_handle) t

The above query will produce appropriate text.

Reference:


Wednesday, 8 May 2013

SQL Server Activity Monitor Permission


USE [master]
GO
GRANT VIEW SERVER STATE TO [SQLTest]
GO

The above sql script grant the permissions to view the server activity data.