Stored Procedure with mode flag

  • Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

  • Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    No problem at all ... though this is not necessarily good programming practice.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    Yes, it is.
    But I would not recommend to do it.
    Create different procedures for different tasks.

    But - you can assign the same name "with index" to those different procedures.
    CREATE PROCEDURE TestSelect;1
    as

    select * from sys.objects
    go

    CREATE PROCEDURE TestSelect;2
    as

    select * from sys.columns
    go

    exec TestSelect;1
    exec TestSelect;2

    When you drop procedure TestSelect you drop every instance of it.

    _____________
    Code for TallyGenerator

  • Can you provide a rough example or a link?  Also, why is it not good practice?

  • Sergiy - Monday, August 27, 2018 7:00 AM

    Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    Yes, it is.
    But I would not recommend to do it.
    Create different procedures for different tasks.

    But - you can assign the same name "with index" to those different procedures.
    CREATE PROCEDURE TestSelect;1
    as

    select * from sys.objects
    go

    CREATE PROCEDURE TestSelect;2
    as

    select * from sys.columns
    go

    exec TestSelect;1
    exec TestSelect;2

    When you drop procedure TestSelect you drop every instance of it.

    Can i still pass parameters to the SP that way?

  • I think i understand.  The SP is for Inventory lot allocation. So, I should just create separate procedures. i.e. IM_Lot_Select, IM_Lot_Update, IM_Lot_Delete?

  • Sergiy - Monday, August 27, 2018 7:00 AM

    Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    Yes, it is.
    But I would not recommend to do it.
    Create different procedures for different tasks.

    But - you can assign the same name "with index" to those different procedures.
    CREATE PROCEDURE TestSelect;1
    as

    select * from sys.objects
    go

    CREATE PROCEDURE TestSelect;2
    as

    select * from sys.columns
    go

    exec TestSelect;1
    exec TestSelect;2

    When you drop procedure TestSelect you drop every instance of it.

    Or you could just have the flag as a parameter to the stored procedure
    CREATE PROCEDURE TestSelect
    (
        @Flag int
    ) as
    IF @Flag=1
    select * from sys.objects
    ELSE IF @Flag=2
    select * from sys.columns
    go

    exec TestSelect 1
    exec TestSelect 2

  • Jackie Lowery - Monday, August 27, 2018 7:03 AM

    Sergiy - Monday, August 27, 2018 7:00 AM

    Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    Yes, it is.
    But I would not recommend to do it.
    Create different procedures for different tasks.

    But - you can assign the same name "with index" to those different procedures.
    CREATE PROCEDURE TestSelect;1
    as

    select * from sys.objects
    go

    CREATE PROCEDURE TestSelect;2
    as

    select * from sys.columns
    go

    exec TestSelect;1
    exec TestSelect;2

    When you drop procedure TestSelect you drop every instance of it.

    Can i still pass parameters to the SP that way?

    It's easy to find out:
    CREATE PROCEDURE TestSelect;3
        @NameLike nvarchar(20)
    as

    select * from sys.objects
    where name like @NameLike
    go

    CREATE PROCEDURE TestSelect;4
        @NameLike nvarchar(20)
    as

    select * from sys.columns
    where name like @NameLike
    go

    exec TestSelect;3 '%s'
    exec TestSelect;4 '%d'

    _____________
    Code for TallyGenerator

  • Sergiy - Monday, August 27, 2018 7:07 AM

    It's easy to find out:

    lol. Yes, that was a lazy question.  Thanks.

  • Jonathan AC Roberts - Monday, August 27, 2018 7:07 AM

    Sergiy - Monday, August 27, 2018 7:00 AM

    Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    Yes, it is.
    But I would not recommend to do it.
    Create different procedures for different tasks.

    But - you can assign the same name "with index" to those different procedures.
    CREATE PROCEDURE TestSelect;1
    as

    select * from sys.objects
    go

    CREATE PROCEDURE TestSelect;2
    as

    select * from sys.columns
    go

    exec TestSelect;1
    exec TestSelect;2

    When you drop procedure TestSelect you drop every instance of it.

    Or you could just have the flag as a parameter to the stored procedure
    CREATE PROCEDURE TestSelect
    (
        @Flag int
    ) as
    IF @Flag=1
    select * from sys.objects
    ELSE IF @Flag=2
    select * from sys.columns
    go

    exec TestSelect 1
    exec TestSelect 2

    The problem with this approach is it is a performance timebomb. The execution plan can get cached for one execution path and then need to run with the other one resulting in really terrible performance. Gail has a great blog post on this topic. https://sqlinthewild.co.za/index.php/2009/09/15/multiple-execution-paths/

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I have read about that in the past, but didn't think about it.  Another good point.  I'm just going to split up the SPs based on function.  Thanks guys.

  • Mode is quite OK to do as long as you make the initial proc just a driver proc: it adjusts param(s) as needed and then calls another, customized proc to do the processing required.  In fact, this is actually quite useful when there are a lot of options and you want to be able to easily add more in the future.


    CREATE PROCEDURE categ_driver
        @param_value1 ...,
        @param-value2 ..,.
        @mode varchar(10)
    AS
    SET ...
    IF @param ...
        SET @param ...
    IF @mode = 'AUDIT'
        EXEC categ_audit_... @param ...
    ELSE
    IF @mode = 'REPORT'
        EXEC categ_report_... @param ...
    ELSE
    IF @mode = 'SELECT'
        EXEC categ_select_... @param ...
    ELSE
    IF @mode = 'UPDATE'
        EXEC categ_update_... @param ...
    ...

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Jonathan AC Roberts - Monday, August 27, 2018 7:07 AM

    Sergiy - Monday, August 27, 2018 7:00 AM

    Jackie Lowery - Monday, August 27, 2018 6:30 AM

    Is it possible to create a stored procedure with a mode? flag and a couple of other parameters that can either return a set of records or perform an update?

    Yes, it is.
    But I would not recommend to do it.
    Create different procedures for different tasks.

    But - you can assign the same name "with index" to those different procedures.
    CREATE PROCEDURE TestSelect;1
    as

    select * from sys.objects
    go

    CREATE PROCEDURE TestSelect;2
    as

    select * from sys.columns
    go

    exec TestSelect;1
    exec TestSelect;2

    When you drop procedure TestSelect you drop every instance of it.

    Or you could just have the flag as a parameter to the stored procedure
    CREATE PROCEDURE TestSelect
    (
        @Flag int
    ) as
    IF @Flag=1
    select * from sys.objects
    ELSE IF @Flag=2
    select * from sys.columns
    go

    exec TestSelect 1
    exec TestSelect 2

    The problem with such a method is that SQL Server won't necessarily compile the execution plan for what you want..  You can get some pretty serious less-than-optimal performance from such a thing.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 13 posts - 1 through 12 (of 12 total)

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