Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

sql server - Simplify SQL Insert which uses NEWSEQUNETIALID() column default

I have the following insert stored procedure:

CREATE 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 was not supplied.
    IF (@ServerEnvironmentId IS NULL)
    BEGIN
        -- 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
        )

        -- Get the ServerEnvironmentId.
        SELECT @ServerEnvironmentId = ServerEnvironmentId
        FROM @APPL_ServerEnvironment
    END
    ELSE
    BEGIN
        -- Insert the data into the table.
        INSERT INTO APPL_ServerEnvironment WITH(TABLOCKX)
        (
                ServerEnvironmentId,
                ServerEnvironmentName,
                ServerEnvironmentDescription,
                DateCreated,
                UserCreatedId
        )
        VALUES
        (
                @ServerEnvironmentId,
                @ServerEnvironmentName,
                @ServerEnvironmentDescription,
                GETDATE(),
                @UserCreatedId
        )
    END
GO

I could have simplified the above as:

CREATE Procedure dbo.APPL_ServerEnvironmentInsert
(
    @ServerEnvironmentName varchar(50),
    @ServerEnvironmentDescription varchar(1000),
    @UserCreatedId uniqueidentifier,
    @ServerEnvironmentId uniqueidentifier OUTPUT
)
WITH RECOMPILE
AS
-- Ensure @ServerEnvironmentId IS NOT NULL
SELECT ISNULL(@ServerEnvironmentId, newid())

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

But by doing so, i loose the performance improvements of the newsequentialid() over newid(). newsequentialid() can not be set in code as newid(), it can only be supplied as a default value on a table column level.

Any ideas anyone on simplifying the original query, but utilising newsequentialid()? Or, is the original query the most simplified solution in achieving this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since the newsequentialid() can only be used as a column default value, you could change your original query to:

  • insert just the @ServerEnvironmentId if no value had been supplied, thus generating a new sequential ID and retrieving it from the OUTPUT clause

  • then update that row defined by either the @ServerEnvironmentId passed in originally, or the new ID you just created by inserting a "dummy row" into your table

Not sure if that would be any faster / more efficient - you would have to do some measurements on that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...