Monday 27 February 2017

SQL Server - Pass multiple strings (varchars) to single parameter in stored procedure

Watch this example on YouTube



1. here is my stored procedure
ALTER PROCEDURE spPassStrings
    @SelectedNames Varchar(max)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    Declare @SQL Varchar(max)
    Set @Sql = 'SELECT * FROM Customers WHERE FisrtName IN (' + @SelectedNames + ')'
    Exec (@SQL)
END
GO

2. and here is how to call it
Declare @FNames Varchar(Max) = '''Mark'', ''Frank'''

Exec spPassStrings @FNames

1 comment:

  1. What if we have two variables. One same as above and the other one with single value.

    ReplyDelete