Monday, 27 February 2017

SQL Server - how to pass multiple integers to single parameter in stored procedure

Watch this example on YouTube


1. here is my stored procedure
CREATE PROCEDURE spPassIntegers
    @SelectedIDs Varchar(Max) = null
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    Declare @SQL AS NVarchar(MAX);

    SET @SQL = 'SELECT * FROM Customers WHERE ID IN (' + @SelectedIDs + ')'
    Exec (@SQL)
END
GO

2. here I'm passing multiple integers to parameter

Declare @IDs Varchar(Max) = '1,2, 1003'
Exec spPassIntegers @IDs

3 comments:

  1. Super Sir
    Voww its an Excellent answer now a days Interviewers are asking the Same Question Now I got the Answers for that Thank you very Mch

    ReplyDelete
  2. Can you please do it by using user defined table typed??

    ReplyDelete