Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8253781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:59:33+00:00 2026-06-08T00:59:33+00:00

I have the following SQL Server stored procedure: CREATE PROCEDURE ispsDcOhcAgg @TmpTableName NVARCHAR(50), @ListItem

  • 0

I have the following SQL Server stored procedure:

CREATE PROCEDURE ispsDcOhcAgg @TmpTableName NVARCHAR(50), @ListItem NVARCHAR(50) 
AS
IF EXISTS (SELECT name 
           FROM sys.tables 
           WHERE name = @TmpTableName) 
DROP TABLE @TmpTableName; -- This will not work.
GO

This will clearly not work (see the comment in the above snippit). The only (and very ugly) way I have found to get around this problem is to do the following

CREATE PROCEDURE ispsDcOhcAgg @TmpTableName NVARCHAR(50), @ListItem NVARCHAR(50) 
AS
DECLARE @SQL NVARCHAR(4000) 
SET @SQL = N'IF EXISTS (SELECT name ' +
           N'FROM sys.tables ' + 
           N'WHERE name = N' + N'''' + @TmpTableName + N''') ' + 
           N'DROP TABLE ' + @TmpTableName + N';'
EXEC sp_executesql @SQL;
GO

which truly stinks and for large stored procedures, it’s horrendous!

Is there another way of doing this that I don’t know about?

Thanks for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:59:35+00:00Added an answer on June 8, 2026 at 12:59 am

    No, if you want to use a table name dynamically like this, you need to use dynamic SQL.

    So you should make sure you don’t open yourself up to nasty SQL injection risks!

    Try something like this:

    SET @SQL = 'IF EXISTS (SELECT name ' +
               N'FROM sys.tables ' + 
               N'WHERE name = @TableName) ' + 
               N'DROP TABLE ' + QUOTENAME(@TmpTableName) + ';'
    
    EXEC sp_executesql @SQL, N'@TableName sysname', @TmpTableName;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sniplet of a stored procedure in SQL Server Create PROCEDURE
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
i have the following stored procedure in sql server and i am trying to
Hi i have the following stored proc in SQL Server 2005: ALTER PROCEDURE [dbo].[slot_sp_EngineerTimeslots_Group]
Have a Sql Script file name spCreation.sql with the following contents. CREATE PROCEDURE dbo.usp_select_customers
Compare the following stored procedures: CREATE PROCEDURE testProc1 AS SELECT * INTO #temp FROM
We have the following simple Stored Procedure that runs as an overnight SQL server
I have created a stored procedure in SQL Server, which runs immediately from Management
I have a stored procedure in SQL Server CREATE PROCEDURE ParseXML (@InputXML xml) The
I have the following requirements I have relational content stored in a SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.