VBdotnet2005
asked on
create sp and drop temp table
Is this the correct way of create a store procedure? I want to drop a temp tables everytime
when demo procedure is called.
IF OBJECT_ID('tempdb..#TEMP_1 ') IS NOT NULL
DROP TABLE #TEMP_1
go
IF OBJECT_ID('tempdb..#TEMP_2 ') IS NOT NULL
DROP TABLE #TEMP_2
go
IF OBJECT_ID('tempdb..#TEMP_3 ') IS NOT NULL
DROP TABLE #TEMP_3
go
GO
CREATE PROCEDURE dbo.Demo
@StartsWith nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT
p.Name,
OrderCount = COUNT_BIG(DISTINCT th.ReferenceOrderID)
FROM Production.Product AS p
JOIN Production.TransactionHist ory AS th ON
th.ProductID = p.ProductID
WHERE
p.Name LIKE @StartsWith + N'%'
GROUP BY
p.Name;
END;
when demo procedure is called.
IF OBJECT_ID('tempdb..#TEMP_1
DROP TABLE #TEMP_1
go
IF OBJECT_ID('tempdb..#TEMP_2
DROP TABLE #TEMP_2
go
IF OBJECT_ID('tempdb..#TEMP_3
DROP TABLE #TEMP_3
go
GO
CREATE PROCEDURE dbo.Demo
@StartsWith nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT
p.Name,
OrderCount = COUNT_BIG(DISTINCT th.ReferenceOrderID)
FROM Production.Product AS p
JOIN Production.TransactionHist
th.ProductID = p.ProductID
WHERE
p.Name LIKE @StartsWith + N'%'
GROUP BY
p.Name;
END;
ASKER
I want to drop temp tables everytime when procedure demo is called. Not dropping demo.
DROP TABLE #TEMP_1
DROP TABLE #TEMP_2
DROP TABLE #TEMP_13
DROP TABLE #TEMP_1
DROP TABLE #TEMP_2
DROP TABLE #TEMP_13
ASKER
Like this?
CREATE PROCEDURE dbo.Demo (@StartsWith nvarchar(50)) AS
IF OBJECT_ID('tempdb..#TEMP_1 ') IS NOT NULL
DROP TABLE #TEMP_1
go
IF OBJECT_ID('tempdb..#TEMP_2 ') IS NOT NULL
DROP TABLE #TEMP_2
go
IF OBJECT_ID('tempdb..#TEMP_3 ') IS NOT NULL
DROP TABLE #TEMP_3
go
GO
SELECT
p.Name,
COUNT_BIG(DISTINCT th.ReferenceOrderID) as OrderCount
FROM Production.Product AS p
JOIN Production.TransactionHist ory AS th ON th.ProductID = p.ProductID
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO
CREATE PROCEDURE dbo.Demo (@StartsWith nvarchar(50)) AS
IF OBJECT_ID('tempdb..#TEMP_1
DROP TABLE #TEMP_1
go
IF OBJECT_ID('tempdb..#TEMP_2
DROP TABLE #TEMP_2
go
IF OBJECT_ID('tempdb..#TEMP_3
DROP TABLE #TEMP_3
go
GO
SELECT
p.Name,
COUNT_BIG(DISTINCT th.ReferenceOrderID) as OrderCount
FROM Production.Product AS p
JOIN Production.TransactionHist
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So when sp demo is called, it will drop #temp_1, 2 & 3 first? It wouldn't keep droping Demo everytime sp Demo is called, right?
>So when sp demo is called, it will drop #temp_1, 2 & 3 first?
correct
>It wouldn't keep droping Demo everytime sp Demo is called, right?
correct
executing the script does the IF EXISTS..DROP SP and CREATE SP
After that, executing the SP does not alter the SP, it just runs it
correct
>It wouldn't keep droping Demo everytime sp Demo is called, right?
correct
executing the script does the IF EXISTS..DROP SP and CREATE SP
After that, executing the SP does not alter the SP, it just runs it
ASKER
Sorry, I am still new. Do I need to copy and paste the whole sample when creating sp?
IF EXISTS(SELECT name FROM sys.procedures WHERE name='Demo')
DROP PROC demo
GO
CREATE PROCEDURE dbo.Demo (@StartsWith nvarchar(50)) AS
DROP TABLE #TEMP_1
DROP TABLE #TEMP_2
DROP TABLE #TEMP_3
SELECT
p.Name,
COUNT_BIG(DISTINCT th.ReferenceOrderID) as OrderCount
FROM Production.Product AS p
JOIN Production.TransactionHist ory AS th ON th.ProductID = p.ProductID
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO
IF EXISTS(SELECT name FROM sys.procedures WHERE name='Demo')
DROP PROC demo
GO
CREATE PROCEDURE dbo.Demo (@StartsWith nvarchar(50)) AS
DROP TABLE #TEMP_1
DROP TABLE #TEMP_2
DROP TABLE #TEMP_3
SELECT
p.Name,
COUNT_BIG(DISTINCT th.ReferenceOrderID) as OrderCount
FROM Production.Product AS p
JOIN Production.TransactionHist
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO
yes
ASKER
Thank you Jim. I appreciate your help.
Anytime.
Open in new window
1.
Significantly cleaned up the code2.
Can lose the DROP TABLE #tmp's, as those are not used in the SP, so it's not necessary3.
BEGIN and END is not necessary, as the SP is only one query4.
You'll want to test the OrderCount column, as I can't test it on my end.5.
If by 'Do I need to put Drop table first?' you mean drop procedure, then you can put the below block before the CREATE PROCOpen in new window