Felipe Calderon Felipe Calderon - 5 months ago 56
SQL Question

Must declare the scalar variable? Microsoft SQL Server

This code is giving me the "Must declare scalar variable @MANDATORY" error:

CREATE PROCEDURE [dbo].[CRE_STATION_PR]
@P_CODE VARCHAR(10),
@P_NAME VARCHAR(50),
@P_COORDINATE_X FLOAT,
@P_COORDINATE_Y FLOAT,
@P_LOCATION VARCHAR(50),
@P_STATE_PURPOSE VARCHAR(200),
@P_MANDATORY BIT,
@P_ID_STATE INT
AS
INSERT INTO TBL_STATIONS (CODE,NAME,COORDINATE_X,COORDINATE_Y,LOCATION,STATE_PURPOSE,MANDATORY,ID_STATE) VALUES (@P_CODE,@P_NAME,@P_COORDINATE_X,@P_COORDINATE_Y,@P_LOCATION,@P_STATE_PURPOSE,@MANDATORY,@P_ID_STATE)
RETURN 0
GO


What should I do?

Answer

There is a typo in your insert query

INSERT INTO TBL_STATIONS 
(CODE,NAME,COORDINATE_X,COORDINATE_Y,LOCATION,STATE_PURPOSE,MANDATORY,ID_STATE) 
VALUES 
(@P_CODE,@P_NAME,@P_COORDINATE_X,@P_COORDINATE_Y,@P_LOCATION,@P_STATE_PURPOSE,
@P_MANDATORY, --Here P_ missing 
@P_ID_STATE)