1
我有這個存儲過程SQL Server存儲過程sp_executesql
USE [all_things]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_getThingsByType]
@thingid int,
@typeid int
AS
DECLARE @Sql NVARCHAR(MAX) = 'SELECT * from items'
IF @thingid IS NOT NULL
BEGIN
SET @Sql += ' where thingid = @thingid'
END
IF @typeid IS NOT NULL
BEGIN
SET @Sql += ' and TypeID = @typeid'
END
EXEC sp_executesql @sql, N'@thingid int,@typeid int',@[email protected],@[email protected];
測試用例:
當我運行它
thingid
是null
和typeid
是null
,我得到的所有結果是完善。當
thingid
供應和typeid
是null
,結果是OK這裏是哪裏,結果並不好:
thingid
是null
和typeid
供應。一切都被退回。
我錯過了什麼?
謝謝
imho,如果thingid爲空而不是typeid,則表示有一個格式不正確的查詢,它們看起來像是select * from items和TypeID = @ typeid'。 – tschmit007 2013-04-04 15:31:28
我沒有看到。謝謝 – mpora 2013-04-04 15:34:29