Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stored Procedure Variable Problem

Posted on 2007-11-27
1
Medium Priority
?
291 Views
Last Modified: 2010-03-19
Hello, I have the following stored procedure that is giving me the "Must declare the scalar variable @createquery" error supposedly at line 2:

--Deletes the table if it already exists and re-creates it using the users SQL
CREATE PROCEDURE dbo.usp_CreateTable
      @tablename nvarchar(128) = NULL,  --The name of the table to create
      @createquery nvarchar(500) = NULL --The sql statement to create the table
AS
      DECLARE @msg AS NVARCHAR(500);

      --Input Validation
      IF @tablename IS NULL
            BEGIN
                  SET @msg = N'A value must be supplied for parameter @tablename.';
                  RAISERROR(@msg, 16, 1);
                  RETURN;
            END

      IF @createquery IS NULL
            BEGIN
                  SET @msg = N'A value must be supplied for parameter @createquery.';
                  RAISERROR(@msg, 16, 1);
                  RETURN;
            END

      IF OBJECT_ID(@tablename) IS NOT NULL
            DECLARE @DropSQL nvarchar(255)
            SET @DropSQL='DROP TABLE [dbo].' + @tablename
            EXEC(@DropSQL);
      GO

      EXEC(@createquery);
GO

What do I need to change to fix the proc?
0
Comment
Question by:Torrwin
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 20361086
you have a GO too early... apart from the that you have another "problem" with the last IF ... the begin/end are missing:
--Deletes the table if it already exists and re-creates it using the users SQL
CREATE PROCEDURE dbo.usp_CreateTable
      @tablename nvarchar(128) = NULL,  --The name of the table to create
      @createquery nvarchar(500) = NULL --The sql statement to create the table
AS
BEGIN
      DECLARE @msg AS NVARCHAR(500); 
      --Input Validation
      IF @tablename IS NULL
            BEGIN
                  SET @msg = N'A value must be supplied for parameter @tablename.';
                  RAISERROR(@msg, 16, 1);
                  RETURN;
            END 
      IF @createquery IS NULL
            BEGIN
                  SET @msg = N'A value must be supplied for parameter @createquery.';
                  RAISERROR(@msg, 16, 1);
                  RETURN;
            END 
      IF OBJECT_ID(@tablename) IS NOT NULL
      BEGIN
            DECLARE @DropSQL nvarchar(255)
            SET @DropSQL='DROP TABLE [dbo].' + @tablename
            EXEC(@DropSQL);
      END 
      EXEC(@createquery);
END

Open in new window

0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

926 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