Solved

Stored Procedure Variable Problem

Posted on 2007-11-27
1
234 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 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

20 Experts available now in Live!

Get 1:1 Help Now