Tuesday, 18 April 2017

MSSQL - Pass multiple values to single paramenter in stored procedure - pass varchar



Watch on YouTube


1. Stored Procedure

CREATE PROCEDURE PassMultipleNames
    @NameList varchar(max)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT * FROM Product Where ',' + Convert(varchar(max), @NameList) + ','
     Like '%,' + Convert(varchar(max), Name) + ',%'
END

2. Calling Stored Procedure

Exec PassMultipleNames 'Sofa,Chair,Window'

1 comment: