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
Related
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
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
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
I'm having a bit of difficulty with this one in that I'm not sure how to do this in SQL Server.
Basically, I want to insert a new row into the database, get the PK value that was just inserted (same query) and output it back to whatever called the stored procedure:
CREATE PROCEDURE Users_Insert
-- Add the parameters for the stored procedure here
#userid int output,
#name varchar(50),
#surname varchar(50),
#email varchar(200),
#password varchar(50),
#location varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into Users(FirstName, LastName, Email, Password, Location)
values(#name, #surname, #email, #password, #location);
GO
#userid = ##IDENTITY;
END
I've done this in MySQL as follows:
CREATE PROCEDURE Users_Insert(#userid int output, #name varchar(50), #surname varchar(50), #email varchar(200), #password varchar(50), #location varchar(50)
BEGIN
insert into Users(FirstName, LastName, Email, Password, Location)
values(#name, #surname, #email, #password, #location);
set #userid = last_insert_id();
END
SQL Server gives me an error:
Msg 102, Level 15, State 1, Procedure Users_Insert, Line 18
Incorrect syntax near '#userid'.
Frankly, I'm not sure I declared the output parameter correctly, can anyone offer suggestions?
You need to assign the value to #userid ! Also, I would recommend using SCOPE_IDENTITY() and not ##IDENTITY :
CREATE PROCEDURE Users_Insert
-- Add the parameters for the stored procedure here
#userid int output,
#name varchar(50),
#surname varchar(50),
#email varchar(200),
#password varchar(50),
#location varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into Users(FirstName, LastName, Email, Password, Location)
values(#name, #surname, #email, #password, #location);
-- make an actual **assignment** here...
SELECT #userid = SCOPE_IDENTITY();
END
See this blog post for an explanation as to WHY you should use SCOPE_IDENTITY over ##IDENTITY
Can I return UNIQUEIDENTIFIER from a stored procedure using the RETURN statement or is it only by using the OUTPUT statement?
i.e to return the PersonID UNIQUEIDENTIFIER:
CREATE PROCEDURE CreatePerson
#Name NVARCHAR(255),
#Desc TEXT
AS
DECLARE #Count INT
DECLARE #JobFileGUID UNIQUEIDENTIFIER
-- Check if job exists?
SET #Count = (SELECT COUNT(Name) AS Name FROM Person WHERE Name=#Name)
IF #Count < 1
BEGIN
SET #PersonGUID = NEWID();
INSERT INTO Person
(PersonID, Name, [Desc])
VALUES (#PersonGUID, #Name, #Desc)
END
SELECT #PersonGUID = Person.PersonID
FROM Person
WHERE Name = #Name
RETURN #PersonGUID
GO
Thanks
In stored procedure - only using the OUTPUT statement. In function - return.
Use:
CREATE PROCEDURE CreatePerson
#Name NVARCHAR(255),
#Desc TEXT,
#PersonGUID UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET #PersonGUID = ...
END
How to call:
DECLARE
#name NVARCHAR(255),
#desc TEXT,
#personGUID UNIQUEIDENTIFIER
SET #name = 'Bob'
SET #desc = 'One handsome man.'
EXEC [Database].[schema].CreatePerson #name, #desc, #personGUID OUTPUT
From the documentation you can actually see that a return in a stored procedure is actually used as a response code, hence you get the exception when trying to return a uniqueidentifier.
https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/return-data-from-a-stored-procedure?view=sql-server-ver16#return-data-using-a-return-code
How I solved it, is by just performing a SELECT after the insert of the generated unique identifier.
DECLARE #ReportId UNIQUEIDENTIFIER;
SET #ReportId = NEWID();
INSERT INTO [dbo].[Report]
([ReportId]
,[ReportName])
VALUES
(#ReportId
,#ReportName)
SELECT #ReportId as ReportIdInternal
You'll have to see how to perform that with multiple selects though.
CREATE TABLE [dbo].[tbl_Clients]( [ClientID] [uniqueidentifier] NULL, [ClientName] varchar NULL, [ClientEnabled] [bit] NULL ) ON [PRIMARY]
GO
CREATE PROCEDURE [dbo].[sp_ClientCreate] #in_ClientName varchar(250) = "New Client 123", #in_ClientEnabled bit, #out_ClientId uniqueidentifier OUTPUT AS
SET #out_ClientId = NEWID();
INSERT INTO tbl_Clients(ClientId, ClientName, ClientEnabled) VALUES( #out_ClientId, #in_ClientName, #in_ClientEnabled)
DECLARE #return_value int, #out_ClientId uniqueidentifier
EXEC #return_value = [dbo].[sp_ClientCreate] #in_ClientName = N'111', #in_ClientEnabled = 1, #out_ClientId = #out_ClientId OUTPUT
SELECT #out_ClientId as N'#out_ClientId'
SELECT 'Return Value' = #return_value
GO
Result:-59A6D7FE-8C9A-4ED3-8FC6-31A989CCC8DB