how to excute a Sp with dynamic parameter - sql

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 ?

Execute GetEmployeessalaryInOutputVariable 500 , 'FirstName', 'Title'
OR
Declare #FirstName as varchar(30)
set #FirstName = 'FirstName'
Declare #Title as varchar(30)
set #Title = 'title'
Execute GetEmployeessalaryInOutputVariable 500 , #FirstName, #Title

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

Related

Creating only one table in a stored procedure

Is it possible to create a table only once while executing the same procedure several times eg:
create procedure grg
(#firstname varchar(100),
#lastname varchar(100),
#dob date,
#loginID varchar(100),
#password varchar(100),
#email varchar(100),
#SSN varchar(11)
)
as
begin
create table............ <- only once
select
#firstname, #lastname,
#dob, #loginID, #password, #email,
#SSN
If(.....)
you can use CREATE TABLE IF NOT EXISTS
Keep in mind the if the table already exists it will show a warning not an error.
You can ignore the warning. If you want to temporarily disable the warning you can do it as follows.
SET sql_notes = 0;
CREATE TABLE IF NOT EXISTS
SET sql_notes = 1;
Yeah with sql you can use temp tables just prefix them with a hash.
#
These tables would only exist on the current session
If you really want the stored proc to handle the creation of the table then you can create the table on the stored procedure's definition. Just make sure to validate if table already exists so you don't get errors.
CREATE PROCEDURE XX
AS
BEGIN
IF EXISTS (SELECT * FROM sysobjects WHERE name = <table_name>)
BEGIN
<---INSERT CREATE TABLE SCRIPT HERE---->
END
<---POPULATE HERE--->
END
Try following
CREATE PROCEDURE grg
(#firstname VARCHAR(100),
#lastname VARCHAR(100),
#dob DATE,
#loginID VARCHAR(100),
#password VARCHAR(100),
#email VARCHAR(100),
#SSN VARCHAR(11)
)
AS
BEGIN
IF NOT EXISTS
(
SELECT * FROM sysobjects WHERE name = 'myTable' AND xtype = 'U'
)
BEGIN
CREATE TABLE myTable(Name VARCHAR(64) NOT NULL);
END;
SELECT * FROM myTable;
END;

create procedure

please hep, I am trying to create a procedure that will display 4 parameters whenever I enter a parameter. This is my code below
CREATE PROCEDURE sp_emp_info
( #EmployeeID INT,
#EmployeeID INT OUTPUT,
#LastName VARCHAR(30) OUTPUT,
#FirstName VARCHAR(15) OUTPUT,
#Phone VARCHAR(10) OUTPUT )
AS
SELECT #EmployeeID,
#LastName,
#FirstName,
#Phone
FROM Employees
WHERE EmployeeID = #EmployeeID
GO
This is bringing this error
The variable name '#EmployeeID' has already been declared. Variable names must be unique within a query batch or stored procedure.
Must declare the scalar variable "#LastName"
I have EmployeeID as both input and output. When I tried to remove the output by running the below :
CREATE PROCEDURE sp_emp_info
( #EmployeeID int,
#LastName VARCHAR(30) OUTPUT,
#FirstName VARCHAR(15) OUTPUT,
#Phone VARCHAR(10) OUTPUT )
AS
SELECT #EmployeeID,
#LastName,
#FirstName,
#Phone
FROM Employees
WHERE EmployeeID = #EmployeeID
GO
that worked, but the procedure wont run . eg I ran
EXECUTE sp_emp_info 7
It brough this error:
Procedure or function 'sp_emp_info' expects parameter '#LastName', which was not supplied.
What do you think I am doing wrong? Thank you .
Okay, I was going about this the complex way. this simple coding:
CREATE PROCEDURE sp_emp_info
( #EmployeeID int )
AS
SELECT EmployeeID,
LastName,
FirstName,
Phone
FROM Employees
WHERE EmployeeID = #EmployeeID
GO
solved my problem.
Thank you everyone.Much appreciated
You need to set a default value for the optional parameters. In this case I set them to NULL.
CREATE PROCEDURE sp_emp_info
( #EmployeeID int )
AS
SELECT EmployeeID
LastName,
FirstName,
Phone
FROM Employees
WHERE EmployeeID = #EmployeeID
GO

Getting Error On Stored Procedure while getting employee names from table

I have this SQL Server stored procedure:
use PracticeDatabase
Go
alter procedure spGetEmpByGender
#gender nvarchar(50),
#name nvarchar(50) output
as
begin
select #name = Emp_Name
from tblPracticeEmpTable
where gender = #gender
End
declare #EmpNames nvarchar(50)
exec spGetEmpByGender 'male', #EmpNames output
select #EmpNames
While executing the above procedure, I get this error:
Msg 217, Level 16, State 1, Procedure spGetEmpByGender, Line 10
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
Execute the following and see what happens
use PracticeDatabase
Go
IF OBJECT_ID('spGetEmpByGender') IS NOT NULL
DROP PROCEDURE spGetEmpByGender
GO
CREATE procedure spGetEmpByGender
#gender nvarchar(50),
#name nvarchar(50) output
as
begin
select Top 1 #name = Emp_Name
from tblPracticeEmpTable
where gender = #gender
End
GO
declare #EmpNames nvarchar(50)
exec spGetEmpByGender 'male', #EmpNames output
select #EmpNames
GO

Search ID, first name and last name in SQL using Like

Hi I have this stored procedure for Searching employee information. I want the user to have the choice to enter ID, or First Name or Last Name. but when I execute this stored procedure, it requires the other parameters.
create proc searchtry
#empID int,
#firstname varchar(20),
#lastname varchar(20)
as
begin
select fld_EmployeeId,fld_EmployeeFirstName,fld_EmployeeLastName
from Reference.tbl_EmployeeDetails
where fld_EmployeeId like #empID
OR fld_EmployeeFirstName like #firstname
OR fld_EmployeeLastName like #lastname
end
You should give default values to the parameters
create proc searchtry
#empID int = NULL,
#firstname varchar(20) = '',
#lastname varchar(20) = ''
as
begin
select fld_EmployeeId,fld_EmployeeFirstName,fld_EmployeeLastName
from Reference.tbl_EmployeeDetails
where fld_EmployeeId like #empID
OR fld_EmployeeFirstName like #firstname
OR fld_EmployeeLastName like #lastname
end

Store values in Stored Procedure

I created a SP as "SP_SELECT"
CREATE PROCEDURE SP_SELECT #userid varchar(50)
AS
BEGIN
SELECT Dept_Id from Master where User_Id=#userid
END
GO
Now the task is I have to store the Result (i.e) Dept_Id in a variable named as "dept" in the same stored procedure itself. Help me with the query.
Do you mean this?
CREATE PROCEDURE SP_SELECT #userid varchar(50)
AS
BEGIN
DECLARE #deptid int;
SET #deptid = SELECT Dept_Id from Master where User_Id=#userid
END
RETURN #deptid
GO
You could also pass #deptid as an output parameter in sql.
But this var will only exist each time the stored proc is run on the calling function or code you will still need to store the value.
i.e.
Declare #masterdeptid int;
SET #masterdeptid = EXEC SP_Select '123';
I ain't test the sql but you get the idea
try this one
set #dept= (select Dept_Id from Master where User_Id=#userid)
and if you want return this value to front end , write below code
set #dept= (select Dept_Id from Master where User_Id=#userid)
select #dept
This could be helpful to you
CREATE PROCEDURE USP_SELECT #userid varchar(50), #Dept int output
AS
BEGIN
SELECT #Dept = Dept_Id from Master where User_Id=#userid
END
above code can be executed as
Declare #Dept int;
EXEC USP_Select '123',#Dept output
Print #Dept