My stored procedure has a IF statement where it passes a value if @LastName has a string, but if its empty, @LastName is NULL and SELECT statement executes without the condition.
I want it so that the user enters nothing, it passes NULL, but if the user enters text, the text passes to the @LastName parameter.
When I enter something in the textbox it works, but if I leave it blank it does not execute the SELECT statement without the conidition.
I tried using the code below....
Any ideas?
Stored Procedure:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_StoredProcedure]
@LastName varchar(50) = NULL
AS
BEGIN
SET NOCOUNT ON;
Why not the below? This way you don't need a dynamic query.
IF @LastName IS NOT NULLBEGIN SET @LastName = @LastName + '%' SELECT e.FirstName, e.LastName FROM Employees e WHERE e.LastName LIKE @LastNameENDELSE SELECT e.FirstName, e.LastName FROM Employees eEND
Open in new window