Repository navigation
Expand file tree
/
Copy pathdbo.spDropLocalTemporaryTableIfExists
More file actions
66 lines (55 loc) · 1.79 KB
/
Copy pathdbo.spDropLocalTemporaryTableIfExists
File metadata and controls
66 lines (55 loc) · 1.79 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
USE <DatabaseName>
GO
CREATE PROCEDURE dbo.spDropLocalTemporaryTableIfExists(
@TableName nvarchar(128)
)
AS
/**************************************************************************************************
DATE AUTHOR
7/31/18 Alejandro
PURPOSE
Drops local temporary table (single #) if it exists.
PARAMETERS
@TableName Name of the temporary table without the #
TICKETS
Date Ticket Description
7/31/18 NHITAP-36 Review General Ledger Tool (put the existing version of this procedure
in my template so I could upload it to Git)
TEST CODE
EXEC dbo.spDropLocalTemporaryTableIfExists 'TestTable'
**************************************************************************************************/
BEGIN
SET XACT_ABORT, NOCOUNT ON;
BEGIN TRY
--Begin a transaction if there is not already an open one
DECLARE @ExistsLocalTransaction bit = 0;
IF @@TRANCOUNT = 0
BEGIN
BEGIN TRANSACTION;
SELECT @ExistsLocalTransaction = 1;
END;
--Error checks
IF @TableName LIKE '%#%'
BEGIN
RAISERROR('Invalid @TableName parameter passed to dbo.DropLocalTemporaryTableIfExists.',16,1);
END;
--Save the actual name of the table by adding the # to it
SELECT @TableName = '#' + @TableName
--If it exists, drop it using dynamic SQL
IF OBJECT_ID('tempdb..' + @TableName) IS NOT NULL
BEGIN
DECLARE @SQL nvarchar(149) = 'DROP TABLE ' + @TableName;
EXECUTE sp_executesql @SQL;
END;
--Only commit the transaction if this procedure opened it
IF @ExistsLocalTransaction = 1 COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
DECLARE
@ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE(),
@ErrorSeverity INT = ERROR_SEVERITY(),
@ErrorState INT = ERROR_STATE();
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
END CATCH
END