Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server sql server存储过程sp_executesql_Sql Server_Stored Procedures - Fatal编程技术网

Sql server sql server存储过程sp_executesql

Sql server sql server存储过程sp_executesql,sql-server,stored-procedures,Sql Server,Stored Procedures,我有这个存储过程 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 @S

我有这个存储过程

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',@thingid=@thingid,@typeid=@typeid;
测试用例:

  • 当我用
    thingid
    null
    typeid
    null
    运行它时,我得到了所有完美的结果

  • 当提供了
    thingid
    并且
    typeid
    null
    时,结果正常

  • 这里是结果不好的地方:
    thingid
    null
    并提供了
    typeid
    。所有的东西都被归还了

  • 我错过了什么


    谢谢

    谢谢@tschmit007,我把它修好了

    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 AND @thingid IS NOT NULL
    BEGIN
      SET @Sql += ' and TypeID = @typeid'
    END
    
    IF @typeid IS NOT NULL AND @thingid IS NULL
    BEGIN
      SET @Sql += ' where TypeID = @typeid'
    END
    
    EXEC sp_executesql @sql, N'@thingid int,@typeid int',@thingid=@thingid,@typeid=@typeid;
    

    多亏了@tschmit007,我把它修好了

    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 AND @thingid IS NOT NULL
    BEGIN
      SET @Sql += ' and TypeID = @typeid'
    END
    
    IF @typeid IS NOT NULL AND @thingid IS NULL
    BEGIN
      SET @Sql += ' where TypeID = @typeid'
    END
    
    EXEC sp_executesql @sql, N'@thingid int,@typeid int',@thingid=@thingid,@typeid=@typeid;
    

    嗯,如果thingid为null,而不是typeid,那么您就有一个格式不正确的查询,看起来是
    select*from items和typeid=@typeid
    。我没有看到。Thanksimho,如果thingid为null,而不是typeid,则会出现一个格式不正确的查询,看起来是
    select*from items和typeid=@typeid
    。我没有看到这一点。谢谢