-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbo.spExecute.sql
More file actions
66 lines (56 loc) · 1.1 KB
/
dbo.spExecute.sql
File metadata and controls
66 lines (56 loc) · 1.1 KB
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
58
59
60
61
62
63
64
65
66
DROP TABLE IF EXISTS dbo.RunningCode
GO
CREATE TABLE dbo.RunningCode
(
SPID int DEFAULT @@SPID,
LineNumber int NOT NULL,
SQL nvarchar(200) NOT NULL,
Assembler nvarchar(200) NOT NULL,
CONSTRAINT PK_RunningCode PRIMARY KEY CLUSTERED (SPID, LineNUmber)
)
GO
CREATE OR ALTER VIEW vwRunningCode
AS
SELECT *
FROM dbo.RunningCode
WHERE SPID = @@SPID
GO
CREATE OR ALTER PROCEDURE spExecute
@StartLineNumber int = 1
AS
SET NOCOUNT ON
DECLARE @Break bit = 0,
@Sql nvarchar(200),
@StackTop int
EXEC spContextInitialize
@StackPointer = DEFAULT,
@ProgramCounter = @StartLineNumber
WHILE(1 = 1)
BEGIN
SELECT @Sql = dbo.fnNextLine(),
@StackTop = dbo.fnTop()
IF @Sql IS NULL
BREAK
EXEC spPrintStack
RAISERROR (N'Executing line %d: %s', 0, 1, @StackTop, @Sql) WITH NOWAIT
EXEC sp_executesql
@Sql,
N'@ST int',
@St = @StackTop
UPDATE dbo.ProgramCounter
SET Number = Number + 1
END
SELECT dbo.fnTop()
GO
CREATE OR ALTER FUNCTION dbo.fnNextLine ()
RETURNS nvarchar(200)
AS
BEGIN
RETURN
(
SELECT TOP (1) SQL
FROM dbo.vwRunningCode
WHERE LineNumber = dbo.fnProgramCounterGet()
)
END
GO