Question

I have the following stored procedure:

ALTER Procedure dbo.APPL_ServerEnvironmentInsert
(
    @ServerEnvironmentName varchar(50),
    @ServerEnvironmentDescription varchar(1000),
    @UserCreatedId uniqueidentifier,
    @ServerEnvironmentId uniqueidentifier OUTPUT
)
WITH RECOMPILE
AS
    -- Stores the ServerEnvironmentId.
    DECLARE @APPL_ServerEnvironment TABLE (ServerEnvironmentId uniqueidentifier)

    -- Insert the data into the table.
    INSERT INTO APPL_ServerEnvironment WITH(TABLOCKX)
    (
        ServerEnvironmentName,
        ServerEnvironmentDescription,
        DateCreated,
        UserCreatedId
    )
    OUTPUT Inserted.ServerEnvironmentId INTO @APPL_ServerEnvironment
    VALUES
    (
        @ServerEnvironmentName,
        @ServerEnvironmentDescription,
        GETDATE(),
        @UserCreatedId
    )

    -- If @ServerEnvironmentId was not supplied.
    IF (@ServerEnvironmentId IS NULL)
    BEGIN
        -- Get the ServerEnvironmentId.
        SELECT @ServerEnvironmentId = ServerEnvironmentId
        FROM @APPL_ServerEnvironment
    END

The ServerEnvironmentId column is a primary key with a default set on it, which is (newsequentialid()).

I need this stored procedure to work for 2 scenarios:

  1. Value supplied for ServerEnvironmentId - WORKS.
  2. Value not supplied for ServerEnvironmentId - DOES NOT WORK - CANNOT INSERT NULL VALUE. I thought by setting a default on this column this would be fine.

Someone please help to ammend this procedure so that it may work for both scenarios. Solution needs to have minimal changes as all sp's currently following this trend.

OTHER TIPS

Default values are only applied on inserts if the column is not included in the INSERT list. I'd recommend the following not entirely trivial change (I've commented out the lines to be removed):

ALTER Procedure dbo.APPL_ServerEnvironmentInsert 
( 
    @ServerEnvironmentName varchar(50), 
    @ServerEnvironmentDescription varchar(1000), 
    @UserCreatedId uniqueidentifier, 
    @ServerEnvironmentId uniqueidentifier OUTPUT 
) 
WITH RECOMPILE 
AS 
    ---- Stores the ServerEnvironmentId. 
    --DECLARE @APPL_ServerEnvironment TABLE (ServerEnvironmentId uniqueidentifier) 

    IF @ServerEnvironmentName is null
        SET @ServerEnvironmentName = newid()

    -- Insert the data into the table. 
    INSERT INTO APPL_ServerEnvironment WITH(TABLOCKX) 
    ( 
        ServerEnvironmentName, 
        ServerEnvironmentDescription, 
        DateCreated, 
        UserCreatedId 
    ) 
    --OUTPUT Inserted.ServerEnvironmentId INTO @APPL_ServerEnvironment 
    VALUES 
    ( 
        @ServerEnvironmentName, 
        @ServerEnvironmentDescription, 
        GETDATE(), 
        @UserCreatedId 
    ) 

    ---- If @ServerEnvironmentId was not supplied. 
    --IF (@ServerEnvironmentId IS NULL) 
    --BEGIN 
    --    -- Get the ServerEnvironmentId. 
    --    SELECT @ServerEnvironmentId = ServerEnvironmentId 
    --    FROM @APPL_ServerEnvironment 
    --END

The default constraint will not be used by this procedure, but you can leave it in place if there are other places where rows may be added to the table.

(My first answer was long and so it this one, so I'm posting a second answer.)

I missed that you were using NewSequentialId. Again, if a column is specified within the insert statement, any DEFAULT values assigned to that column will not be used [unless you use the DEFAULT keyword in the INSERT statement, but that's still all or nothing--you can't say "if @Var is null then DEFAULT"]. I think you are stuck with simple branching and semi-redundant code, along the lines of:

ALTER Procedure dbo.APPL_ServerEnvironmentInsert 
( 
    @ServerEnvironmentName varchar(50), 
    @ServerEnvironmentDescription varchar(1000), 
    @UserCreatedId uniqueidentifier, 
    @ServerEnvironmentId uniqueidentifier OUTPUT 
) 
WITH RECOMPILE 
AS 
    -- Stores the ServerEnvironmentId. 
    DECLARE @APPL_ServerEnvironment TABLE (ServerEnvironmentId uniqueidentifier) 

    IF @ServerEnvironmentId is null
     BEGIN
        --  ServerEnvironmentId not provided by user, generate during the insert
        INSERT INTO APPL_ServerEnvironment WITH(TABLOCKX) 
        ( 
            ServerEnvironmentName, 
            ServerEnvironmentDescription, 
            DateCreated, 
            UserCreatedId 
        ) 
        OUTPUT Inserted.ServerEnvironmentId INTO @APPL_ServerEnvironment 
        VALUES 
        ( 
            @ServerEnvironmentName, 
            @ServerEnvironmentDescription, 
            GETDATE(), 
            @UserCreatedId 
        ) 

        -- Get the new ServerEnvironmentId
        SELECT @ServerEnvironmentId = ServerEnvironmentId 
        FROM @APPL_ServerEnvironment 
     END 

    ELSE
     BEGIN
        --  ServerEnvironmentId is provided by user
        INSERT INTO APPL_ServerEnvironment WITH(TABLOCKX) 
        ( 
            ServerEnvironmentName, 
            ServerEnvironmentDescription, 
            DateCreated, 
            UserCreatedId,
            ServerEnvironmentId
        ) 
        OUTPUT Inserted.ServerEnvironmentId INTO @APPL_ServerEnvironment 
        VALUES 
        ( 
            @ServerEnvironmentName, 
            @ServerEnvironmentDescription, 
            GETDATE(), 
            @UserCreatedId,
            @ServerEnvironmentId
        ) 
     END

(Why lock the entire table during the insert?)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top