Hi all,
I'm having some trouble with the following sproc
Create PROCEDURE GetMatchingUsers
@id int = NULL,
@lastName varchar(50) = NULL,
@firstName varchar(50) = NULL
AS
BEGIN
SET NOCOUNT ON
DECLARE @q nvarchar(4000),
@paramlist nvarchar(4000)
SELECT @q = 'SELECT Id
, LastName
, FirstName '
SELECT @q = @q + 'FROM Users WHERE 1 = 1'
IF ISNULL(@id, '') <> ''
SELECT @q = @q + ' AND Id = ' + Cast(@id as varchar)
IF ISNULL(@lastName, '') <> ''
SELECT @q = @q + ' AND LastName like ''' + @lastName + '%'''
IF ISNULL(@firstName, '') <> ''
SELECT @q = @q + ' AND FirstName like ''' + @firstName + '%'''
SELECT @q = @q + ' ORDER BY LastName, FirstName '
--PRINT @q
SELECT @paramlist = '
@id int = NULL,
@lastName varchar(50) = NULL,
@firstName varchar(50) = NULL'
EXEC sp_executesql @q, @paramlist,
@id,
@lastName,
@firstName
I was wonder why the following if statement is not considered true if i pass 0 as an id
IF ISNULL(@id, '') <> ''
SELECT @q = @q + ' AND Id = ' + Cast(@id as varchar)
Thanks for any help