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
Related
If i need to just update one column do i have to give other column previous value or i can just give give the column i need to change and not null values to update in database .. here is procedure code what can i do to this procedure so that i can just need to insert the value i want to update ...
ALTER procedure [dbo].[Customer_update]
( #Id int,
#Firstname nvarchar(40)=,
#Lastname nvarchar(40)=,
#City nvarchar(40)=null,
#Country nvarchar(40)=null,
#Phone nvarchar(20)=null
)
as
begin
update Customer set FirstName=#Firstname,LastName=#Lastname,City=#City,Country=#Country,Phone=#Phone where Id=#Id
end
You almost have it and as Alex K pointed out in the comment above, the most elegant way to handle optional updates is to allow null parameters and update what is sent over.
ALTER PROCEDURE [dbo].[Customer_update]
(#Id INT,
#Firstname NVARCHAR(40) = NULL,
#Lastname NVARCHAR(40) = NULL,
#City NVARCHAR(40) = NULL,
#Country NVARCHAR(40) = NULL,
#Phone NVARCHAR(20) = NULL)
AS
BEGIN
UPDATE Customer
SET FirstName = ISNULL(#Firstname, FirstName)...
WHERE Id = #Id
END
In order to use this in client code with nullable fields, simply omit the parameter altogether or let nulls pass through (one caveat to this is when you really need to set the field to null in which case the field should probably not be nullable. Then you can implicitly send a null over and the field will be set to the proper value)
Nullable fields allow you to omit parameters from a stored procedure call and still perform the operation. This is useful when you need make changes and do not want to impact existing applications, as long as the nullable fields are not required.
Are you asking about Insert/Update in one operation?
Some people do "upserts". I personally do not like having one operation for insert/updates. I feel the client should already know what operation should be performed, also, having one update and one insert procedure makes the procedures more transparent and easier to auto generate.
However, if that is what you were asking then you would create a procedure similar to the one below:
ALTER PROCEDURE [dbo].[Customer_update]
(#Id INT = NULL,
#Firstname NVARCHAR(40) = NULL,
#Lastname NVARCHAR(40) = NULL
#City NVARCHAR(40) = NULL
#Country NVARCHAR(40) = NULL
#Phone NVARCHAR(20) = NULL)
AS
BEGIN
IF (#Id IS NULL)
BEGIN
INSERT INTO Customer...
SET #ID = ##SCOPE_IDENTITY
END
ELSE BEGIN
UPDATE Customer
SET FirtName = ISNULL(#FirstName, FirstName)
WHERE Id = #Id
END
SELECT *
FROM Customer
WHERE Id = #Id
END
You would need to use ISNULL on each column:
ALTER procedure [dbo].[Customer_update]
( #Id int,
#Firstname nvarchar(40)=,
#Lastname nvarchar(40)=,
#City nvarchar(40)=null,
#Country nvarchar(40)=null,
#Phone nvarchar(20)=null
)
as
begin
update Customer
set FirstName=ISNULL(#Firstname,FirstName),
LastName=ISNULL(#Lastname,LastName),
City=ISNULL(#City,City),
Country=ISNULL(#Country,Country),
Phone=ISNULL(#Phone,Phone)
where Id=#Id
end
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
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
I am working on a project that accepts 3 different parameters, the date is required and the First and Last name are optional. We setup the query below, but even if I change the parameters on the report (SSRS) it still looks at #LetterCreated as being '1/1/1950', any ideas on how I can get this to just accept the parameters? We set the date this way because we want the report to show with all of the reports when it is initially opened.
Alter Proc
AS
BEGIN
SET NOCOUNT ON;
DECLARE #LetterCreated DATETIME,
#FirstName VARCHAR(20),
#LastName VARCHAR(20)
SELECT #LetterCreated = '1/1/1950'
SELECT #FirstName = ''
SELECT #LastName = ''
SELECT
LETTERCREATETIME,
Firstname,
LastName,
From RedFlagAddress
WHERE
CASE WHEN #LetterCreated='1/1/1950'
THEN '1/1/1950'
ELSE ISNULL(LETTERCREATETIME, '07/05/81')
END = #LetterCreated
AND (LastName LIKE #LASTNAME + '%' AND FirstName LIKE #FirstNAME + '%')
END
Any suggestions would be greatly appreciated.
You are setting the #lettercreated date in the procedure. Variables defined within the procedure are not visible outside it.
You should declare the parameters as parameters, and set the default in the declaration
ALTER PROC yourproc
(
#LetterCreated DATETIME = '1950-1-1',
#FirstName VARCHAR(20) = '',
#LastName VARCHAR(20) = ''
)
as
begin
select
LETTERCREATETIME,
Firstname,
LastName,
From
RedFlagAddress
where
(
ISNULL(LETTERCREATETIME, '07/05/81') = #LetterCreated
or
#LetterCreated = '1950-1-1'
)
AND LastName LIKE #LASTNAME + '%'
AND FirstName LIKE #FirstNAME + '%'
end
I'm guessing here but what you may want is
IF parameter #LetterCreated is null then it should not be used as a filter at all
IF the data in RedFlagData.LETTERCREATETIME is Null then it should be matched to a filter date of '07/05/81' FWR
Assuming that you have the 'allow nulls' check on the RDL/RDLC set for the #LetterCreated parameter, the where needs to be changed, the optional filter can be set like so:
ISNULL(#LetterCreated, LETTERCREATETIME) = ISNULL(LETTERCREATETIME, '07/05/81')
If you get rid of the magic date, then you can guarantee no date filter applied even if LETTERCREATETIME is null, by the filter:
ISNULL(#LetterCreated, LETTERCREATETIME) = ISNULL(LETTERCREATETIME)
OR
(#LetterCreated IS NULL AND LETTERCREATETIME IS NULL)
Thus:
ALTER PROC XYZ
(
#LetterCreated DATETIME,
#FirstName VARCHAR(20) = NULL,
#LastName VARCHAR(20) = NULL
)
as
begin
select
LETTERCREATETIME,
Firstname,
LastName,
From
RedFlagAddress
where
(
ISNULL(#LetterCreated, LETTERCREATETIME) = LETTERCREATETIME
OR
(#LetterCreated IS NULL AND LETTERCREATETIME IS NULL)
)
AND LastName LIKE ISNULL(#LastName, '') + '%'
AND FirstName LIKE ISNULL(#FirstName, '') + '%'
end
One caveat : The performance of this query will be terrible, given the amount of functional manipulation in the where clause, and the LIKEs and ORs - Hopefully RedFlagAddress is a relatively small table? If not, you may need to rethink your approach
Similar to podiluska's answer, you should set the parameters as part of the SP, as podiluska indicates, but then you should set the default values in SSRS, in the Parameters there.
This will let the users see the defaults and use those for subscriptions, or override them as needed.
Alter Proc Proc_Name
(
#LetterCreated DATETIME,
#FirstName VARCHAR(20) = null,
#LastName VARCHAR(20) = null
)
AS
BEGIN
SET NOCOUNT ON;
This will pass null values as Default values to you Proc now you code should be able to handle null values if no values are provided for FirstName and LastName
Can I please have some help with the syntax of a SP in SQL.
Here is my code:
CREATE PROCEDURE usp_GetValue
(
#ID VARCHAR(10),
#Description VARCHAR(10)
)
AS
BEGIN
return #ID + #Description
END
CREATE PROCEDURE usp_InsertValue
(
#ID VARCHAR(10),
#FirstName VARCHAR(50),
#LastName VARCHAR(50),
#Description VARCHAR(10),
#Comment VARCHAR(max)
)
AS
BEGIN
Declare #v_Value VARCHAR(15)
Set #v_Value = usp_GetValue(#ID, #Description)
END
In the usp_InsertValue SP, I am wanting to declare and set a variable. Once the variable has been declared, I then wish to call another SP with parameters to set the value of the declared variable.
I am not sure of the syntax. May I please have some help?
UPDATE
I have updated my above code using your function. How do I Set the #v_Value from the usp_GetValue function.
I am getting this error:
'usp_GetValue' is not a recognized built-in function name.
UPDATE2
Here is my full code:
CREATE PROCEDURE usp_PersonCategoryLookupTesting
(
#ID VARCHAR(10),
#Description VARCHAR(10),
#res VARCHAR(10) OUTPUT
)
AS
BEGIN
return #ID + #Description
END
CREATE PROCEDURE usp_InsertPersonTesting
(
#IDTest VARCHAR(10),
#FirstName VARCHAR(50),
#LastName VARCHAR(50),
#AddressLine1 VARCHAR(50),
#AddressLine2 VARCHAR(50),
#AddressLine3 VARCHAR(50),
#MobilePhone VARCHAR(20),
#HomePhone VARCHAR(20),
#Description VARCHAR(10),
#Comment VARCHAR(max)
)
AS
BEGIN
Declare #PersonCategory VARCHAR(15)
EXEC usp_PersonCategoryLookupTest #ID, #Description, #PersonCategory
INSERT INTO Person(FirstName, LastName, AddressLine1, AddressLine2, AddressLine3, MobilePhone, HomePhone, DateModified, PersonCategory, Comment)
VALUES (#FirstName, #LastName, #AddressLine1, #AddressLine2, #AddressLine3, #MobilePhone, #HomePhone, GETDATE (), #PersonCategory, #Comment)
END
I am getting this error in my application that is calling the SQL code:
Conversion failed when converting the varchar value '123Client' to data type int.
I am using the values "123" and "Client" for #IDTest and #Description.
I think output parameters should be the proper way:
See this post:
stored procedure returns varchar
CREATE PROCEDURE usp_GetValue
(
#ID VARCHAR(10),
#Description VARCHAR(10),
#res VARCHAR(10) OUTPUT
)
AS
BEGIN
return #ID + #Description
END
CREATE PROCEDURE usp_InsertValue
(
#ID VARCHAR(10),
#FirstName VARCHAR(50),
#LastName VARCHAR(50),
#Description VARCHAR(10),
#Comment VARCHAR(max)
)
AS
BEGIN
Declare #v_Value VARCHAR(15)
EXEC usp_GetValue #ID, #Description, #v_Value
-- use #v_Value here...
END
You can only use a function like that, not stored procedure. Stored Procedures only return integer values.
For SP, you need to create out parameters to retrieve the values. For this scenario, it is better to create a function
CREATE FUNCTION usp_GetValue
(
#ID VARCHAR(10),
#Description VARCHAR(10),
)
RETURNS VARCHAR(50)
AS
BEGIN
return #ID + #Description
END
Then call it:
SET #v_value = dbo.usp_GetValue('John','Doe') --output: John Doe
If you insist you want a SP, which is not proper for this, there are 2 ways
1) An out parameter
CREATE PROCEDURE usp_GetValue
(
#ID VARCHAR(10),
#Description VARCHAR(10),
#Result varchar(20) OUTPUT
)
AS
BEGIN
SET #Result = #ID + #Description
END
Then call it:
Declare #v_Value VARCHAR(15)
Set #v_Value = EXEC usp_GetValue #ID, #Description, #v_Value OUTPUT
2) Select from it
CREATE PROCEDURE usp_GetValue
(
#ID VARCHAR(10),
#Description VARCHAR(10)
)
AS
BEGIN
SELECT #ID + #Description
END
Use it like this:
declare #tempresult as table(v_value as varchar(15))
INSERT INTO #tempresult
EXEC usp_GetValue #ID, #Description
Select Top 1 #v_value = v_value From #tempresult
My advise is use the function approach.