SQL “SELECT IN (Value1, Value2…)” with passing variable of values into GridView

血红的双手。 提交于 2019-12-21 17:51:48

问题


I have a strange encounter when creating a GridView using SELECT..WHERE..<field> IN (value1, val2...).

In the "Configure datasource" tab, if i hard code the values SELECT .... WHERE field1 in ('AAA', 'BBB', 'CCC'), the system works well.

However, if I define a new parameter and pass in a concatenated string of values using a variable; be it a @session, Control or querystring; e.g. SELECT .... WHERE field1 in @SESSION the result is always empty.

I did another experiment by reducing the parameter content to only one single value, it works well.

in short, if I hardcode a string of values, it works, if I pass a variable with single value only, it works, but if i pass a varialbe with two values; it failed.

Pls advise if I have make any mistake or it is a known bug.

BR SDIGI


回答1:


This works. Not sure how efficient it is though.

CREATE PROCEDURE [dbo].[get_bars_in_foo]
    @bars varchar(255)
AS
BEGIN
    DECLARE @query AS varchar(MAX)
    SET @query = 'SELECT * FROM [foo] WHERE bar IN (' + @bars + ')'
    exec(@query)
END

-- exec [get_bars_in_foo] '1,2,3,4'



回答2:


Take a look at the answer to this question (which is very similar to yours)

Parameterize an SQL IN clause

Which ultimately links (via a convoluted route) to this definitive answer:

http://www.sommarskog.se/arrays-in-sql.html




回答3:


If you go to using a stored procedure, you can use this method, which I discussed in regards to how to do it in SQL.



来源:https://stackoverflow.com/questions/342655/sql-select-in-value1-value2-with-passing-variable-of-values-into-gridvie

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