This article shows how to Split a String into Rows using Stored Procedure.
Let's say we have a table with below script and data.
CREATE TABLE [dbo].[Employees](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL, [EmployeeName] [nvarchar](max) NULL, [EmployeeGender] [nvarchar](max) NULL, [EmployeeDOB] [nvarchar](max) NULL, CONSTRAINT [PK_dbo.Employees] PRIMARY KEY CLUSTERED ( [EmployeeId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Passing comma separated (delimited) values as Parameter to Stored Procedure
CREATE PROCEDURE GetEmployees @EmployeeIds VARCHAR(100) AS BEGIN SELECT EmployeeId, EmployeeName, EmployeeGender, EmployeeDOB FROM Employees WHERE EmployeeId IN( SELECT CAST(Item AS INTEGER) FROM dbo.SplitString(@EmployeeIds, ',') ) END
Execute the procedure with below statement.
EXEC GetEmployees '1,3,5'
The output should like below.
Post a Comment