Skip to content

How to Parameterize an SQL IN Clause #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM Program
WHERE department_id IN (1, 2, 3);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM Student WHERE FIND_IN_SET(id, '1001,1003,1007');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SET @StudentIds = '1001,1003,1007';
SET @sql = CONCAT('SELECT * FROM Student WHERE id IN (', @StudentIds, ')');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION find_programs_by_departments(dept_ids int[])
RETURNS TABLE(id int, name varchar, description varchar, start_date date, end_date date, type varchar, department_id int) AS $$
BEGIN
RETURN QUERY
SELECT p.*
FROM Program p
WHERE p.department_id = ANY (dept_ids);
END;
$$ LANGUAGE plpgsql;

SELECT * FROM find_programs_by_departments(ARRAY[1, 2, 3]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PREPARE find_programs_by_departments (int[]) AS
SELECT *
FROM Program
WHERE department_id = ANY ($1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT *
FROM Program
WHERE department_id = ANY (string_to_array('1,2,3', ',')::int[]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE PROCEDURE find_students_by_ids
@StudentIds VARCHAR(MAX)
AS
BEGIN
SELECT *
FROM Student
WHERE id IN (SELECT value FROM STRING_SPLIT(@StudentIds, ','))
END;

EXEC find_students_by_ids '1001,1003,1007';
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TYPE StudentIDTableType AS TABLE
(
StudentID INT
);

CREATE PROCEDURE find_students_by_ids
@StudentIDs StudentIDTableType READONLY
AS
BEGIN
SELECT *
FROM Student
WHERE id IN (SELECT StudentID FROM @StudentIDs)
END;

DECLARE @StudentIDs StudentIDTableType
INSERT INTO @StudentIDs (StudentID) VALUES (1), (2), (3)

EXEC find_students_by_ids @StudentIDs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE PROCEDURE find_students_by_ids
@StudentIds VARCHAR(MAX)
AS
BEGIN
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'SELECT * FROM Student WHERE id IN (' + @StudentIds + ')'
EXEC sp_executesql @SQL
END;

EXEC find_students_by_ids '1001,1003,1007';