Search code examples
sqlstored-proceduresdynamicexecutedeclare

how to excute a Sp with dynamic parameter


I have created a stored procedure to find name and designation by supplying income of an employee

Create  PROCEDURE GetEmployeessalaryInOutputVariable
(
    @Income INT,                 -- Input parameter,  Income of the Employee
    @FirstName VARCHAR (30) OUT, -- Output parameter to collect the Employee name
    @Title VARCHAR (30)OUT       -- Output Parameter to collect the Employee designation
)
AS
BEGIN
SELECT FirstName=@FirstName, Title=@Title 
    FROM Salaries WHERE @Income=Income
END

After this I tried to execute the Sp as follows

Declare @FirstName as varchar(30) -- Declaring the variable to collect the Employeename
Declare @Title as varchar(30)     -- Declaring the variable to collect the Designation
Execute GetEmployeessalaryInOutputVariable 500 , @FirstName output, @Title output
select @FirstName,@Title as Designation   

As soon as I write the above statement, it displays an error displaying

Invalid object name GetEmployeessalaryInOutputVariable

Why is it behaving like that although the procedure has been created and exists?

Also, how can I run the query to get proper results ?


Solution

  • Create  PROCEDURE GetEmployeessalaryInOutputVariable
    (
        @Income INT,                 -- Input parameter,  Income of the Employee
        @FirstName VARCHAR (30) OUT, -- Output parameter to collect the Employee name
        @Title VARCHAR (30)OUT       -- Output Parameter to collect the Employee designation
    )
    AS
    BEGIN
    SELECT @FirstName = FirstName, @Title=Title 
        FROM Salaries WHERE @Income=Income
    END
    

    Your SP should be like this