Skip to content

Commit 9ce53c8

Browse files
authored
Add Code_Reuse_With_Dynamic_SQL_and_Temp_Procedures_Example script
1 parent 105f256 commit 9ce53c8

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
<documentation>
3+
<author>Konstantin Taranov</author>
4+
<summary>List All Unicode Code Points.</summary>
5+
<returns>PRINT variables values getted from temporary stored procedures.</returns>
6+
<created>2019-04-18</created>
7+
<modified>2019-04-18</modified>
8+
<version>1.0</version>
9+
<sourceLink>https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Code_Reuse_With_Dynamic_SQL_and_Temp_Procedures_Example.sql</sourceLink>
10+
</documentation>
11+
*/
12+
13+
14+
BEGIN /* CREATE temporary procedures for reusing TSQL code */
15+
EXEC sp_executesql N'IF OBJECT_ID(''tempdb..#usp_GetTime'', ''P'') IS NOT NULL DROP PROCEDURE #usp_GetTime;'
16+
EXEC sp_executesql N'
17+
CREATE PROCEDURE #usp_GetTime(
18+
@i VARCHAR(16)
19+
, @o DATETIME2(7) OUTPUT
20+
)
21+
AS
22+
BEGIN
23+
SET @o = CASE WHEN @i = ''SYSDATETIME'' THEN SYSDATETIME()
24+
WHEN @i = ''SYSUTCDATETIME'' THEN SYSUTCDATETIME()
25+
END;
26+
END;';
27+
28+
EXEC sp_executesql N'IF OBJECT_ID(''tempdb..#usp_GetDuration'', ''P'') IS NOT NULL DROP PROCEDURE #usp_GetDuration;'
29+
EXEC sp_executesql N'
30+
CREATE PROCEDURE #usp_GetDuration(
31+
@datepart VARCHAR(5)
32+
, @startdate DATETIME2(7)
33+
, @enddate DATETIME2(7)
34+
, @o INT OUTPUT
35+
)
36+
AS
37+
BEGIN TRY
38+
SET @o = CASE WHEN @datepart = ''ns'' THEN DATEDIFF(ns, @startdate, @enddate)
39+
WHEN @datepart = ''mcs'' THEN DATEDIFF(mcs, @startdate, @enddate)
40+
WHEN @datepart = ''ms'' THEN DATEDIFF(ms, @startdate, @enddate)
41+
WHEN @datepart = ''ss'' THEN DATEDIFF(ss, @startdate, @enddate)
42+
WHEN @datepart = ''mi'' THEN DATEDIFF(mi, @startdate, @enddate)
43+
WHEN @datepart = ''hh'' THEN DATEDIFF(hh, @startdate, @enddate)
44+
WHEN @datepart = ''dd'' THEN DATEDIFF(dd, @startdate, @enddate)
45+
WHEN @datepart = ''wk'' THEN DATEDIFF(wk, @startdate, @enddate)
46+
ELSE 0
47+
END;
48+
END TRY
49+
50+
BEGIN CATCH
51+
PRINT ''Error: '' + CONVERT(varchar(50), ERROR_NUMBER()) +
52+
'', Severity: '' + CONVERT(varchar(5), ERROR_SEVERITY()) +
53+
'', State: '' + CONVERT(varchar(5), ERROR_STATE()) +
54+
'', Procedure: '' + ISNULL(ERROR_PROCEDURE(), ''-'') +
55+
'', Line: '' + CONVERT(varchar(5), ERROR_LINE()) +
56+
'', User name: '' + CONVERT(sysname, ORIGINAL_LOGIN())
57+
PRINT(ERROR_MESSAGE());
58+
END CATCH;
59+
';
60+
END;
61+
62+
DECLARE @startTime datetime2(7);
63+
DECLARE @dateTimeFunction varchar(16) = 'SYSDATETIME';
64+
EXEC #usp_GetTime @i = @dateTimeFunction, @o = @startTime OUTPUT;
65+
66+
DECLARE @runTimeStamp datetime2(7) = SYSDATETIME();
67+
DECLARE @finishTime datetime2(7) = DATEADD(hour, -1, @runTimeStamp);
68+
DECLARE @duration int;
69+
DECLARE @durationAccuracy varchar(5) = 'ss';
70+
71+
EXEC #usp_GetDuration @datepart = @durationAccuracy, @startdate = @runTimeStamp, @enddate = @finishTime, @o = @duration OUTPUT;
72+
73+
PRINT('@startTime = ' + ISNULL(CAST(@startTime AS varchar(27)), 'IS NULL'));
74+
PRINT('@duration = ' + ISNULL(CAST(@duration AS varchar(27)), 'IS NULL'));

0 commit comments

Comments
 (0)