0

I'm trying to create a stored procedure in SQL Server, but I keep getting an error whenever I try to execute it:

Msg 137, Level 15, State 2, Line 48
Must declare the scalar variable "@FirstName"

I thought I declared FirstName in WHERE condition, but it doesn't seem right.

CREATE PROCEDURE Example6
    @FirstName VARCHAR(50),
    @LastName VARCHAR(50),
    @MiddleName VARCHAR(50)
AS 
    SELECT  
        [employee_id], [employee_type_id], 
        [HR_status_id], [employer_id],
        [fName], [mName], [lName],
        [address], [city], [state_id], [zip],
        [hireDate], [currDate],
        [ssn], [ext_emp_id],
        [terminationDate], [dob],
        [initialMeasurmentEnd], 
        [plan_year_id], [limbo_plan_year_id], [meas_plan_year_id],
        [modOn], [modBy],
        [plan_year_avg_hours], [limbo_plan_year_avg_hours],
        [meas_plan_year_avg_hours], [imp_plan_year_avg_hours],
        [classification_id], [aca_status_id], [ResourceId]
    FROM 
        dbo.employee
    WHERE 
       [fName] = @FirstName 
       AND [lName] = @LastName 
       AND [mName] = @MiddleName
       AND @FirstName LIKE '%son%' OR @LastName LIKE '%son%';
4
  • dbfiddle.uk/… -- Please post how you call your SP Commented May 29, 2018 at 14:14
  • 1
    Why is this tagged with C#? Commented May 29, 2018 at 14:14
  • 1
    I'm guessing the issue is that you didn't define that parameter when calling it. Please include that code as well. Commented May 29, 2018 at 14:16
  • The code is working properly. use EXEC Example6 'a','b','c' Commented May 29, 2018 at 14:23

3 Answers 3

2

You should add parentheses in the parameters clause.
Try that one:

CREATE PROCEDURE Example6

(@FirstName varchar(50),
@LastName varchar(50),
@MiddleName varchar(50))
AS 
SELECT [employee_id]
      ,[employee_type_id]
      ,[HR_status_id]
      ,[employer_id]
      ,[fName]
      ,[mName]
      ,[lName]
      ,[address]
      ,[city]
      ,[state_id]
      ,[zip]
      ,[hireDate]
      ,[currDate]
      ,[ssn]
      ,[ext_emp_id]
      ,[terminationDate]
      ,[dob]
      ,[initialMeasurmentEnd]
      ,[plan_year_id]
      ,[limbo_plan_year_id]
      ,[meas_plan_year_id]
      ,[modOn]
      ,[modBy]
      ,[plan_year_avg_hours]
      ,[limbo_plan_year_avg_hours]
      ,[meas_plan_year_avg_hours]
      ,[imp_plan_year_avg_hours]
      ,[classification_id]
      ,[aca_status_id]
      ,[ResourceId]
     FROM dbo.employee
    WHERE  @FirstName LIKE '%son%' OR @LastName LIKE '%son%';
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely not necessary.
2

You need to wrap the parameters for the stored procedure in (), then call the stored procedure such as:

EXEC Procedure 'Son','Son','Son'

       CREATE PROCEDURE Example6
        (
        @FirstName varchar(50),
        @LastName varchar(50),
        @MiddleName varchar(50)
        )
        AS 
        SELECT [employee_id]
              ,[employee_type_id]
              ,[HR_status_id]
              ,[employer_id]
              ,[fName]
              ,[mName]
              ,[lName]
              ,[address]
              ,[city]
              ,[state_id]
              ,[zip]
              ,[hireDate]
              ,[currDate]
              ,[ssn]
              ,[ext_emp_id]
              ,[terminationDate]
              ,[dob]
              ,[initialMeasurmentEnd]
              ,[plan_year_id]
              ,[limbo_plan_year_id]
              ,[meas_plan_year_id]
              ,[modOn]
              ,[modBy]
              ,[plan_year_avg_hours]
              ,[limbo_plan_year_avg_hours]
              ,[meas_plan_year_avg_hours]
              ,[imp_plan_year_avg_hours]
              ,[classification_id]
              ,[aca_status_id]
              ,[ResourceId]
             FROM dbo.employee
            WHERE 
            [FirstName] LIKE @FirstName OR [LastName] LIKE @LastName

2 Comments

I tried that already, but it doesn't give me what I need. I need to list all First names and Last Names from employee table that contains 'son' somewhere in the name.
If you want to use the parameters passed to your stored proc, I think you will want the last line, however the like '%son%' will return people with 'son' in their names
0

In SSMS, if you right-click Stored Procedures and select New Stored Procedure, you'll get this template generated for you. Notice the BEGIN and END that are missing from your SP:

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
    -- Add the parameters for the stored procedure here
    <@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    <@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.