Passing SQL stored procedure entirety of WHERE clause

后端 未结 5 1941
隐瞒了意图╮
隐瞒了意图╮ 2020-12-11 19:01

I have a SQL stored procedure of the form

SELECT [fields] FROM [table] WHERE @whereSql

I want to pass the procedure an argument (@whereSql)

相关标签:
5条回答
  • 2020-12-11 19:22

    The short answer is that you can't do it like this -- SQL Server looks at the contents of a variable as a VALUE. It doesn't dynamically build up the string to execute (which is why this is the correct way to avoid SQL injection attacks).

    You should make every effort to avoid a dynamic WHERE as you're trying to do, largely for this reason, but also for the sake of efficiency. Instead, try to build up the WHERE clause so that it short-circuits pieces with lots of ORs, depending on the situation.

    If there's no way around it, you can still build a string of your own assembled from the pieces of the command, and then EXEC it.

    So you could do this:

    DECLARE @mywhere VARCHAR(500)
    DECLARE @mystmt VARCHAR(1000)
    SET @mywhere = ' WHERE MfgPartNumber LIKE ''a%'' '
    SELECT @mystmt = 'SELECT TOP 100 * FROM Products.Product AS p ' + @mywhere + ';'
    EXEC( @mystmt )
    

    But I recommend instead that you do this:

    SELECT TOP 100 * 
        FROM Products.Product AS p 
        WHERE 
            ( MfgPartNumber LIKE 'a%' AND ModeMfrPartNumStartsWith=1)
        OR  ( CategoryID = 123 AND ModeCategory=1 )
    
    0 讨论(0)
  • 2020-12-11 19:31

    http://sqlmag.com/t-sql/passing-multivalued-variables-stored-procedure

    try this it works!!

    CHARINDEX (',' + ColumnName + ',', ',' +
    REPLACE(@Parameter, ' ', '') + ',') > 0
    

    execute syntax set @Parameter= 'nc1,nc2'

    0 讨论(0)
  • 2020-12-11 19:36

    Make sure you read this fully

    www.sommarskog.se/dynamic_sql.html

    0 讨论(0)
  • 2020-12-11 19:37

    Dynamic SQL listed in some of the Answers is definitely a solution. However, if Dynamic SQL needs to be avoided, one of the solutions that I prefer is to make use of table variables (or temp tables) to store the parameter value that is used for comparison in WHERE clause.

    Here is an example Stored Procedure implementation.

    CREATE PROCEDURE [dbo].[myStoredProc]
    @parameter1 varchar(50)
    AS
    
    declare  @myTempTableVar Table(param1 varchar(50))
    insert into @myTempTableVar values(@parameter1)
    
    select * from MyTable where MyColumn in (select param1 from @myTempTableVar)
    
    GO
    

    In case you want to pass in multiple values, then the comma separated values can be stored as rows in the table variable and used in the same way for comparison.

    CREATE PROCEDURE [dbo].[myStoredProc]
    @parameter1 varchar(50)
    AS
    
    --Code Block to Convert Comma Seperated Parameter into Values of a Temporary Table Variable
    declare  @myTempTableVar Table(param1 varchar(50))
    declare @index int =0, @tempString varchar(10)
    
    if charindex(',',@parameter1) > 0
    begin
     set @index = charindex(',',@parameter1)
     while @index > 0
      begin
        set @tempString = SubString(@parameter1,1,@index-1)
        insert into @myTempTableVar values (@tempString)
        set @parameter1 = SubString(@parameter1,@index+1,len(@parameter1)-@index)
        set @index = charindex(',',@parameter1)
      end
    
      set @tempString = @parameter1
      insert into @myTempTableVar values (@tempString)
    end
    else
    insert into @myTempTableVar values (@parameter1)
    
    select * from MyTable where MyColumn in (select param1 from @myTempTableVar)
    
    GO
    
    0 讨论(0)
  • 2020-12-11 19:39

    I believe this can be done using Dynamic SQL. See below:

    CREATE PROCEDURE [dbo].[myProc]
    @whereSql nvarchar(256)
    
    AS
        EXEC('SELECT [fields] FROM [table] WHERE ' + @whereSql)
    GO
    

    That said, you should do some serious research on dynamic SQL before you actually use it. Here are a few links that I came across after a quick search:

    • http://www.sommarskog.se/dynamic_sql.html

    • http://msdn.microsoft.com/en-us/library/aa224806%28SQL.80%29.aspx

    • http://www.itjungle.com/fhg/fhg100505-story02.html

    0 讨论(0)
提交回复
热议问题