Home > Enterprise >  Pass unknown number of values to select statement
Pass unknown number of values to select statement

Time:01-09

I have a department table and I want to select multiple checkboxes under department, then show it in a datagrid. When I select one option it works, but how to pass multi values? As shown in queryBuilder image WHERE userGroup=?.

Here I want to pass multi values depending on the checkboxes:

enter image description here

enter image description here

enter image description here

here where i pass checkbox groupId

CodePudding user response:

Using STRING_SPLIT

@values = 'v1,v2,...'

SELECT column_name(s)
FROM table_name
WHERE userGroup IN (SELECT value FROM STRING_SPLIT(@values, ','))

CodePudding user response:

Try the in clause: https://www.w3schools.com/sql/sql_in.asp

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

In your case

WHERE userGroup in (?,?, ... ,?)

It's a bit more tricky as you'll need to create the sql statement every time by code, depending on the items in user group.

Don't forget to keep on using parameters to keep all that sweet sanitanization.

Check here on how to do it with code: Building SQL "where in" statement from list of strings in one line?

  •  Tags:  
  • Related