Solved

Stored Procedure Variable Problem

Posted on 2007-11-27
1
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Following an example - removing duplicate strings 4 70
Selection from table2 where criteria for table1 10 69
Sql query 107 102
Database maintenance 36 139
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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