SSMS-EE: Creating and Using Stored Procedures - T-SQL Error Messages 111, 156, & 102

Hi all,

I tried to use the SQL Server Management Studio-Express Edition to execute the following code statements:

--- SQLQuery.sql---

USE testDB

DECLARE @procID int

DECLARE @procName varchar(20)

DECLARE @procType varchar(20)

CREATE PROC sp_getRecords AS SELECT * FROM Inventory

CREATE PROC sp_insertRecord @procID int, @procName varchar(20), @procType

varchar(20) AS INSERT INTO Inventory VALUES (@procID, @procName, @procType)

CREATE PROC sp_deleteRecord @procID int AS DELETE FROM Inventory WHERE ID=@procID

CREATE PROC sp_updateRecord @procID int, @procName varchar(20), @procType

varchar(20) AS UPDATE Inventory SET name=@procName, type=@procType where ID=@procID

--- Insert records into the Inventory table

EXE sp_insertRecord 4, 'ER Vol 1', 'DVD'

EXE sp_insertRecord 5, 'ER', 'VHS'

EXE sp_insertRecord 6, 'Sixth Sense', 'DVD'

--- Delete record with ID=3

EXEC sp_deleteRecord 3

--- Update record with ID=5

EXEC sp-updateRecord 5 'ERVol1', 'VHS'

--- View the updated table

EXEC sp_getRecords

GO

//////////////////////////////////////////////////////////////////////////////////////////////////

I got the following T-SQL error messages:

Msg 111, Level 15, State 1, Procedure sp_getRecords, Line 8

'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.

Msg 156, Level 15, State 1, Procedure sp_getRecords, Line 10

Incorrect syntax near the keyword 'PROC'.

Msg 156, Level 15, State 1, Procedure sp_getRecords, Line 11

Incorrect syntax near the keyword 'PROC'.

Msg 102, Level 15, State 1, Procedure sp_getRecords, Line 14

Incorrect syntax near 'EXE'.

Msg 102, Level 15, State 1, Procedure sp_getRecords, Line 20

Incorrect syntax near 'updateRecord'.

/////////////////////////////////////////////////////////////////////////////////////////////////////

Please help, tell me what is wrong in my execution of the code and advise me how I can correct this problem.

Thanks,

Scott Chang




Answer this question

SSMS-EE: Creating and Using Stored Procedures - T-SQL Error Messages 111, 156, & 102

  • Suneet

    The first error message indicates the problem. Certain DDL statements like CREATE PROCEDURE/TRIGGER/FUNCTION/VIEW etc can be the only statement in a batch. So you need to put a GO after the first set of DECLARE statements (I am not sure why they are there) and after every CREATE PROC statement. Also, please don't use the sp_ prefix for user stored procedures. They are meant for system stored procedures and have different name resolution rules so it will incur some performance overhead. Check the Books Online topics on CREATE PROCEDURE, Batches and Statements for more details.

  • SSMS-EE: Creating and Using Stored Procedures - T-SQL Error Messages 111, 156, & 102