Solved

create sp and drop temp table

Posted on 2014-10-15
10
228 Views
Last Modified: 2014-10-15
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.TransactionHistory AS th ON

        th.ProductID = p.ProductID

    WHERE

        p.Name LIKE @StartsWith + N'%'

    GROUP BY

        p.Name;

END;
0
Comment
Question by:VBdotnet2005
  • 5
  • 5
10 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40382640
CREATE PROCEDURE dbo.Demo (@StartsWith nvarchar(50)) AS

SELECT
    p.Name,
    COUNT_BIG(DISTINCT th.ReferenceOrderID) as OrderCount
FROM Production.Product AS p
   JOIN Production.TransactionHistory AS th ON th.ProductID = p.ProductID
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO

Open in new window

1.

Significantly cleaned up the code

2.

Can lose the DROP TABLE #tmp's, as those are not used in the SP, so it's not necessary

3.

BEGIN and END is not necessary, as the SP is only one query

4.

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 PROC
IF EXISTS(SELECT name FROM sys.procedures WHERE name='Demo') 
   DROP PROC demo
GO

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 40382664
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
0
 

Author Comment

by:VBdotnet2005
ID: 40382668
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.TransactionHistory AS th ON th.ProductID = p.ProductID
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40382671
In that case you can include the IF EXISTS..DROP right after the CREATE PROC, like in the below code block.

Also, the keyword GO executes all statements as a batch, which means any parameter declare before the GO does not exist after it, so notice that there's only a GO right after DROP PROC and the end of the CREATE PROC.

The IF EXISTS .. DROP PROC is the same concept as with the temp tables, if you CREATE PROC and it already exists it'll throw an error, so the best way to handle it is to either DROP..CREATE.  There's also an ALTER PROC,  but again if the proc doesn't exist it'll throw an error.

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.TransactionHistory AS th ON th.ProductID = p.ProductID
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO

Open in new window

0
 

Author Comment

by:VBdotnet2005
ID: 40382689
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?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 40382705
>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
0
 

Author Comment

by:VBdotnet2005
ID: 40382739
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.TransactionHistory AS th ON th.ProductID = p.ProductID
WHERE p.Name LIKE @StartsWith + '%'
GROUP BY p.Name
GO
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40382748
yes
0
 

Author Comment

by:VBdotnet2005
ID: 40382766
Thank you Jim. I appreciate your help.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40382852
Anytime.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now