简体   繁体   中英

Use input and output stored procedure to list multiple values

Hello I have written a stored procedure which has input dept id and output as varchar. I have to list all the records having dept id passed through input. Can anyone help me with it. I am able to print only single value but i want all the values having dept id passed in input.

You can pass the departmentIDs are comma separated list and then split them and use them as given below.

STRING_SPLIT function is available from SQL Server 2016 onwards.

DECLARE @dept table (deptId int, deptName varchar(30))

INSERT INTO @dept 
values (1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5');

DECLARE @DeptidList VARCHAR(4000) = '1,3,4'

SELECT * FROM @dept
WHERE deptID IN
(
select value from string_split(@DeptidList,',')
)

+--------+----------+
| deptId | deptName |
+--------+----------+
|      1 |        1 |
|      3 |        3 |
|      4 |        4 |
+--------+----------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM