forked from FilipDeVos/sp_select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp_select.sql
57 lines (44 loc) · 1.62 KB
/
sp_select.sql
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
IF OBJECT_ID('sp_select') is null
BEGIN
PRINT 'Creating procedure sp_select...'
EXEC ('CREATE PROCEDURE sp_select AS RETURN(-1)')
END
GO
/*
Created by: Filip De Vos
https://foxtricks.com
Based on the post by Jonathan Kehayias
http://sqlblog.com/blogs/jonathan_kehayias/archive/2009/09/29/what-session-created-that-object-in-tempdb.aspx
Usage:
create table #myTempTable (id int, value varchar(100))
insert into #myTempTable values (10, 'hihi'), (11, 'haha')
Keep the connection open where the temptable is created and run the following query from any connection you want.
exec sp_select 'tempdb..#myTempTable'
Also "normal" tables can be inspected.
exec sp_select 'msdb.dbo.MSdbms'
*/
PRINT 'Altering procedure sp_select...'
GO
ALTER PROCEDURE dbo.sp_select(@table_name sysname, @spid int = NULL, @max_pages int = 1000)
AS
SET NOCOUNT ON
DECLARE @status int
, @object_id int
, @db_id int
EXEC @status = sp_select_get_object_id @table_name = @table_name
, @spid = @spid
, @object_id = @object_id output
IF @object_id IS NULL
BEGIN
RAISERROR('The table [%s] does not exist', 16, 1, @table_name)
RETURN (-1)
END
SET @db_id = DB_ID(PARSENAME(@table_name, 3))
EXEC @status = sp_selectpages @object_id = @object_id, @db_id = @db_id, @max_pages = @max_pages
RETURN (@status)
GO
if OBJECT_ID('sp_select') is null
PRINT 'Failed to create procedure sp_select...'
ELSE
PRINT 'Correctly created procedure sp_select...'
GO