How to merge two stored procedures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gelezniyden
    New Member
    • Mar 2013
    • 14

    How to merge two stored procedures

    I have two stored procedures one for INSERT and second for Update and I'd like to merge them in one so could you please tell me which satement should I use for that ?

    Code:
    CREATE PROCEDURE Tools
    (@CategoryID_1 int,
     @CategoryName_2 nvarchar(100),
     @Description_3 ntext)
    
    AS INSERT INTO Categories 
     (CategoryID,
     CategoryName,
     Description) 
    
    VALUES 
    (@CategoryID_1,
     @CategoryName_2,
     @Description_3)
    go
    
    CREATE PROCEDURE Tools1
    
         (@CategoryID_1     [int],
     @CategoryID_2  [int],
     @CategoryName_3    [nvarchar](50),
     @Description_4     [ntext],
    
    AS UPDATE [Teachdb].[dbo].[Categories] 
    
    SET  [CategoryID]    = @CategoryID_2,
     [CategoryName]  = @CategoryName_3,
     [Description]   = @Description_4,
    
    WHERE 
    ( [CategoryID]   = @CategoryID_1)
    go
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You can use EXEC to call a stored procedure from another.

    Comment

    • gelezniyden
      New Member
      • Mar 2013
      • 14

      #3
      I'd like to create one for all Update and Insert

      Comment

      • gelezniyden
        New Member
        • Mar 2013
        • 14

        #4
        Resolved issue following way:

        Code:
        CREATE PROCEDURE Tools
        (
            @Mode nvarchar(100), 
            @CategoryID int,
            @CategoryName nvarchar(100),
            @Description ntext
        )
        
        AS 
        BEGIN 
            IF @Mode = 'INSERT'
            BEGIN
                INSERT INTO Categories (CategoryID, CategoryName, Description) 
                VALUES (@CategoryID, @CategoryName, @Description)
            END
            ELSE IF @Mode = 'UPDATE'
            BEGIN
                UPDATE Categories
                    SET  CategoryID   = @CategoryID,
                        CategoryName  = @CategoryName,
                        Description   = @Description
                WHERE ([CategoryID]   = @CategoryID)
            END
        END
        go

        Comment

        Working...