stored procedure with optional parameters

  • Hi Everybody,

    I have a stored procedure with does multiple tasks like displaying the data in multiple grids, updating the records, adding the records etc. All these are done in a single procedure. I want to create a stored procedure with accepts optional parameters. The user may not send all the parameters if he is calling the stored procedure for data display. He will pass all the parameters when ever he wants to add records.

    Thanks & Regards,

    Naveen kumar

    Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.:)

  • [font="Verdana"]Parameters in SProcs are not optional. You have to mentioend them while calling the SProc. You can instead mention 'DEFAULT' keyword while passing the parameters. Like:

    EXEC dbo.sp_Sample Default, Default

    Mahesh[/font]

    MH-09-AM-8694

  • Mahesh Bote (2/2/2009)


    [font="Verdana"]Parameters in SProcs are not optional. You have to mentioend them while calling the SProc. You can instead mention 'DEFAULT' keyword while passing the parameters. Like:

    EXEC dbo.sp_Sample Default, Default

    Mahesh[/font]

    If you use default values for your parameters you don't have to mention them. A good example is sp_send_dbmail which has about 20 parameters, but most of the time I supply only 2 or 3.

    [font="Verdana"]Markus Bohse[/font]

  • Hi,

    I got the solution. create sp like

    create PROCEDURE COMPLIANCE_TASK_DETAILS(@USR_ID INT, @OPERATION VARCHAR(10), @GET_ID INT = null,

    @GET_TASK_NAME VARCHAR(300) = null, @GET_STATUS VARCHAR(100) = null,

    @ASSIGNED_TO INT = null, @GET_COMMENTS VARCHAR(500) = null

    )

    AS

    BEGIN

    statements........

    end

    Who is wise? He that learns from everyone. Who is powerful? He that governs his passions. Who is rich? He that is content. Who is that? Nobody.:)

  • Dynamic sql will allow you do exactly what you are wanting.

  • CREATE PROC usp_procdetails

    @Param1 INT = 1

    @Param2 VARCHAR(100) = 'Default'--This can work by adding value also.

    AS

    BEGIN

    --Add your code depends upon default.

    END

    GO

    EXEC usp_procdetails

    GO

    Regards,
    Mitesh OSwal
    +918698619998

  • mtaylor7210 63514 (4/9/2010)


    Dynamic sql will allow you do exactly what you are wanting.

    ... but also opens you up to SQL Injection attacks.

  • True but anytime you are taking parameters you open yourself up to injection.

  • The security vulnerability footprint is much higher when using dynamic sql over a properly written parametrized stored procedure.

    I apologize to the original poster for getting off the "thread" topic.

  • mtaylor7210 63514 (4/13/2010)


    True but anytime you are taking parameters you open yourself up to injection.

    No, I believe you are mistaken.

    Please explain why you think using parameterized stored procedures opens you up to SQL injection?

    Unless, of course, you use those parameters to create dynamic SQL.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply