By executing below SQL Query, you will get the list of connected users with the last running SQL Statement by each user...
Who is connected
---------------------
CREATE TABLE #WHO_TEST(
[spid] [int] NULL,
[ecid] [int] NULL,
[status] [nvarchar](50) NULL,
[loginame] [nvarchar](50) NULL,
[hostname] [nvarchar](500) NULL,
[blk] [nvarchar](50) NULL,
[dbname] [nvarchar](500) NULL,
[cmd] [nvarchar](500) NULL,
[request_id] [int] NULL
)
insert into #WHO_TEST
EXEC sp_who;
SELECT *,(SELECT TEXT FROM sys.dm_exec_sql_text((select sql_handle FROM sys.sysprocesses WHERE spid = a.spid))) SQL_CMD
from #WHO_TEST a
Drop Table #WHO_TEST
Share reusable code, script, component. Reusable Database scripts. Different kinds of automation tools like SSIS, Batch ... please write a Comment to do batter ...
Showing posts with label sql. Show all posts
Showing posts with label sql. Show all posts
Friday, November 16, 2018
Thursday, January 7, 2016
SQL Script to split string using SQL Server
NOTE: In 2016 version of SQL Server, a new function STRING_SPLIT (GivenString, Separator) has been implemented but for below 2016 version has no any string function like this one.
So that, I am sharing a user defined table valued function, named SplitString(GivenString, Separator, ReturnSeperator), by which you can Split any string by using any separator. Also, this function has another parameter by which you can return the separator also.
See below parameter description
SplitString(givenString , separator, returnSeperator) -- Download the sql script or see below for the coding
Returns a Table with two columns – OrdinalPosition & Value
Param Name
|
Type
|
Direction
|
Description
|
givenString
|
VARCHAR (8000)
|
IN
|
A String, which one you want to parse/split
|
separator
|
VARCHAR (8000)
|
IN
|
A String, which is used as a separator for concatenated strings.
|
returnSeperator
|
BIT
|
IN
|
Default is False.
For True/1 value, return String will have the separator at the end
For False/0, No separator will be returned.
|
Return Column
|
Type
|
Description
|
OrdinalPosition
|
INT
|
This column will represent the position of the value string in GivenString
|
Value
|
VARCHAR
|
String value as varchar(max length of value) which has been split from GivenString
|
Uses:
Select * from SplitString (‘abc,xyz,pqr’ , ‘,’, False)
Select * from SplitString (‘abc,xyz,pqr’ , ‘,’, default)
|
| ||||||||
Select * from SplitString (‘abc,xyz,pqr’ , ‘,’, true)
|
|
Here is the Table Valued Function script
SplitString: download as file
-----------------------------------------------------------------------
CREATE FUNCTION [dbo].[SplitString] (
@givenString varchar(8000) , @separator varchar(100) , @returnSeparator bit = 0) RETURNS TABLE AS RETURN ( WITH data ([start], [end]) AS ( SELECT 0 AS [start], CHARINDEX(@separator, @givenString) AS [end] UNION ALL SELECT [end] + 1, CHARINDEX(@separator, @givenString, [end] + 1) FROM data WHERE [end] > 0 ) SELECT ROW_NUMBER() OVER ( ORDER BY OrdinalPosition ) OrdinalPosition, RTRIM(LTRIM(Value)) + ( CASE WHEN @returnSeparator = 1 THEN @separator ELSE '' END ) Value FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY [start] ) OrdinalPosition, SUBSTRING(@givenString, [start], COALESCE(NULLIF([end], 0), LEN(@givenString) + 1) - [start]) Value FROM data ) r WHERE RTRIM(Value) <> '' AND Value IS NOT NULL ) |
Subscribe to:
Posts (Atom)