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