I have of values (1, 2, 3
, for example), and I want to pass that list to a SQL query:
"select name from tbl where id in" + list
How might I achieve this?
To create a list query from the selected column, right-click on the column and select 'Add as New Query' option as shown in the picture below. NOTE: You can also use Table. ToList() function in Power Query (M) language for creating a list from a data table.
All you have to do is build the comma separated list of items and insert it in the string.
CREATE FUNCTION dbo. SplitInts ( @List VARCHAR(MAX), @Delimiter VARCHAR(255) ) RETURNS TABLE AS RETURN ( SELECT Item = CONVERT(INT, Item) FROM ( SELECT Item = x.i.value('(./text())[1]', 'varchar(max)') FROM ( SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>') + '</i>'). query('.
you have to put your list directly to sql statement
example:
String sql="select name from tbl where id in ("+StringUtils.join(list, ',')+")";
Statement st=connection.createStatement();
st.execute(sql);
I see in your reply to a comment that you're using HQL. If that's the case, the Hibernate folks make this one easy for you, in the query, just specify:
where id in (:ids)
and then use setParameterList("ids", list)
passing your list. Hibernate will do all the expansion for you!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With