-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsql_sessions.sh
executable file
·34 lines (34 loc) · 1.16 KB
/
sql_sessions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
# This script requires Outsourcer to be installed. It creates an external
# table which connects to a SQL Server database to get basic session
# information.
#
# Prerequisites:
# 1. Outsourcer is installed
#
# 2. dba schema is created
# CREATE SCHEMA dba;
#
# 3. A valid connection defined in os.ext_connection for SQL Server.
# INSERT INTO os.ext_connection
# (type, server_name, instance_name, port, database_name, user_name, pass)
# VALUES
# ('sqlserver', 'jonnywin', null, null, null, 'os_test', 'os_password');
#
# Note: you can also use the Web UI to create this connection.
#
# --get the ID you just inserted
# SELECT id
# FROM os.ext_connection
# WHERE type = 'sqlserver'
# AND server_name = 'jonnywin'
#
# --in my example, the value is 2.
#
psql -c "SELECT os.fn_create_ext_table('dba.sql_sessions',
ARRAY['sql_time timestamp','start_time timestamp','status varchar(30)', 'session_id smallint','sqltext text'],
2, --os.ext_connection.id
'SELECT getdate() as sql_time, req.start_time, req.status, req.session_id, sqltext.TEXT as sqltext
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
order by req.start_time');"