SQL Newb and "'CREATE/ALTER PROCEDURE' must be the first statement in a query batch."

  • Hi,

    I've been passed an SQL script to modify and test. No a great idea as I know no SQL, but needs must.

    I have created a database using the Management Studio Express, and I need to create a table and then run a procedure to populate some data

    When I get to execute my procedure the i get the error message that makes up the title of this thread.

    The code is as below

    "

    use Sample

    --DROP TABLE ALARMS

    CREATE TABLE ALARMS

    (

    ALARM_NUMINT NOT NULL DEFAULT (0) ,

    LOW_BATT_RECEIVED_DATEDATETIME NULL ,

    LAST_UPDATED_DATE DATETIME NOT NULL DEFAULT (GETDATE()),

    CONSTRAINT PK__ALARMS PRIMARY KEY CLUSTERED (ALARM_NUM)

    )

    INSERT INTO ALARMS(ALARM_NUM , LOW_BATT_RECEIVED_DATE, LAST_UPDATED_DATE )

    VALUES (1 , NULL , GETDATE() )

    CREATE PROCEDURE UPDATE_ALARMS_BAT @ALARM_NUM INT

    AS

    UPDATE ALARMS

    SETLOW_BATT_RECEIVED_DATE = GETDATE(),

    LAST_UPDATED_DATE = GETDATE()

    WHERE ALARM_NUM = ISNULL(@ALARM_NUM, -1)

    --CALL STORED PROCEDURE

    EXEC UPDATE_ALARMS_BAT 1

    "

    I am sure that there is a very simple reason why this occurs, but I cant find a definitive reason anywhere on the net.

    Can someone please provide a solution (other than the obvious of pass it to someone who knows SQL - which we dont have here)

  • You need to have a batch separator in there. In default installations of Management Studio, it's "go".

    Add "go" to the line above "CREATE PROCEDURE", on a line all by itself, and it should fix that error.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks,

    If only all problems were as easily soved as that.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply