tsql IN not working properly if passed as parameter

。_饼干妹妹 提交于 2019-12-23 05:27:38

问题


I have a sitution where I need to pass a string in order to do an IN

    declare @searchString varchar(50)
    set @searchString = ' ''Nestle'',''UFI'' '

    select * from tbl1 where CompanyName IN (@SearchString) 

does't work.

But if I do:

    select * from tbl1 where CompanyName IN ('Nestle','UFI')

It works fine. I cannot understand why one work and the other doesn't


回答1:


When you use IN it looks at a set and not a single string expression. Because of this you need to implement a function such as CsvToInt to return a table, like so:

CREATE Function [dbo].[CsvToInt] ( @Array varchar(1000)) 
returns @IntTable table 
    (IntValue int)
--Parse comma seperated value parameters
--To be used in SELECT blah WHERE blah IN (...)
--This function returns int, but may be modified to return any datatype
AS
begin

    declare @separator char(1)
    set @separator = ','

    declare @separator_position int 
    declare @array_value varchar(1000) 

    set @array = @array + ','

    while patindex('%,%' , @array) <> 0 
    begin

      select @separator_position =  patindex('%,%' , @array)
      select @array_value = left(@array, @separator_position - 1)

        Insert @IntTable
        Values (Cast(@array_value as int))

      select @array = stuff(@array, 1, @separator_position, '')
    end

    return
end

And then you can use this function from say within a stored procedure like so:

SELECT Blah
FROM MyTable
WHERE Foo IN (SELECT * FROM dbo.CsvToInt(@Parameter))

Where @Parameter contains a comma seperated string of values like:

Nestle, UFI, Test




回答2:


IN spects a set, not an string as parameter.

See this:

http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/33108337-b7b4-4ada-a480-60673e175f4d/




回答3:


Your example that works is not the same as your example that does not work. Your example that works has a list of strings. Your example that does not work is just a single string. This means that you are trying to retrieve rows where the CompanyName field is equal to the whole string, not the comma delimited portions of that string.

Your example that does not work is equal to:

select * from tbl1 where CompanyName IN (' ''Nestle'',''UFI'' ')

not the following:

select * from tbl1 where CompanyName IN ('Nestle','UFI')



回答4:


declare @searchString varchar(50)
set @searchString = ' ''Nestle'',''UFI'' '

exec ('select * from tbl1 where CompanyName IN (' + @SearchString + ');')

Note that this is dynamic SQL which is sometimes frowned upon and does not optimize well, but it's simple and will get what you want.




回答5:


declare @searchString varchar(50)
set @searchString = 'Nestle,UFI'
set @searchString = ',' + @searchString + ','

select * from tbl1 where charindex(',' + CompanyName + ',',@SearchString) > 0 


来源:https://stackoverflow.com/questions/12803231/tsql-in-not-working-properly-if-passed-as-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!