我遇到一些麻煩,下面的存儲過程SQL服務器如果語句愁楚
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
我很奇怪爲什麼下面,如果,如果我傳遞0作爲ID
IF ISNULL(@id, '') <> ''
SELECT @q = @q + ' AND Id = ' + Cast(@id as varchar)
聲明不被認爲是真正的
感謝您的幫助
我真的希望它連接字符串,如果傳入的ID是0. 我不希望它進入該塊的唯一時間是如果該ID根本沒有傳入。 – AlteredConcept
然後將上面的0改爲-1(即:ISNULL(@ Id,-1)<> -1 –