Home Forums SQL Server 7,2000 T-SQL Parmeterized list to a stored procedure RE: Parmeterized list to a stored procedure

  • I didn't read the article, so I'm not sure that what you mean by parameterized list is what I think of when I hear parameterized list.

    A parameterized list to me means a list of parameters. When you create a stored procedure, you can add parameters to it. Like so:

    Create Procedure dbo.MyProcedure

     @Parameter1 int,

     @parameter2 varchar(50),

     @Parameter3 sysname,

     @Parameter4 datetime = getdate()

    AS

    Then when you call the sp, you can pass in the list of parameters in a couple of easy ways. Either include the list of parameters in the exact same order as they appear in the sp or include the list of parameters in no particular order by naming them to match the parameters in the sp.

    So in Query Analyzer, here are two examples of how to call the sp:

    1. Include the list of parameters in the exact same order as they appear in the sp:

    Exec dbo.MyProcedure 1, 'SecondParam', 'MyTable', '7/4/1776'

    2. Include the list of parameters in no particular order by naming them to match the parameters in the sp:

    Exec dbo.MyProcedure @Parameter1 = 1, @Parameter3 = 'MyTable', @Parameter4 = '7/4/1776', @parameter2 = 'SecondParam'

    There is no parsing to do. Each parameter is assigned a variable name (@Parameter1, and so on) that you use in the sp to refer to each parameter. Like so:

    Create Procedure dbo.MyProcedure

     @Parameter1 int,

     @parameter2 varchar(50),

     @Parameter3 sysname,

     @Parameter4 datetime = getdate()

    AS

    Declare @SQL varchar(2000)

    Set @SQL = 'Update [' + @Parameter3 + '] Set Field2 = ''' + @parameter2 + ''' Where FieldID = ' + @Parameter1 + ' And FieldUpdateDate > ''' + @Parameter4 + ''''

    Exec(@SQL)

    Go


    My blog: SQL Soldier[/url]
    SQL Server Best Practices:
    SQL Server Best Practices
    Twitter: @SQLSoldier
    My book: Pro SQL Server 2008 Mirroring[/url]
    Microsoft Certified Master: SQL Server, Data Platform MVP
    Database Engineer at BlueMountain Capital Management[/url]