sql server - Simplify SQL Insert which uses NEWSEQUNETIALID() column default -
i have following insert stored procedure:
create procedure dbo.appl_serverenvironmentinsert ( @serverenvironmentname varchar(50), @serverenvironmentdescription varchar(1000), @usercreatedid uniqueidentifier, @serverenvironmentid uniqueidentifier output ) recompile -- stores serverenvironmentid. declare @appl_serverenvironment table (serverenvironmentid uniqueidentifier) -- if @serverenvironmentid not supplied. if (@serverenvironmentid null) begin -- insert data table. insert appl_serverenvironment with(tablockx) ( serverenvironmentname, serverenvironmentdescription, datecreated, usercreatedid ) output inserted.serverenvironmentid @appl_serverenvironment values ( @serverenvironmentname, @serverenvironmentdescription, getdate(), @usercreatedid ) -- serverenvironmentid. select @serverenvironmentid = serverenvironmentid @appl_serverenvironment end else begin -- insert data table. insert appl_serverenvironment with(tablockx) ( serverenvironmentid, serverenvironmentname, serverenvironmentdescription, datecreated, usercreatedid ) values ( @serverenvironmentid, @serverenvironmentname, @serverenvironmentdescription, getdate(), @usercreatedid ) end go
i have simplified above as:
create procedure dbo.appl_serverenvironmentinsert ( @serverenvironmentname varchar(50), @serverenvironmentdescription varchar(1000), @usercreatedid uniqueidentifier, @serverenvironmentid uniqueidentifier output ) recompile -- ensure @serverenvironmentid not null select isnull(@serverenvironmentid, newid()) -- insert data table. insert appl_serverenvironment with(tablockx) ( serverenvironmentid, serverenvironmentname, serverenvironmentdescription, datecreated, usercreatedid ) values ( @serverenvironmentid, @serverenvironmentname, @serverenvironmentdescription, getdate(), @usercreatedid ) go
but doing so, loose performance improvements of newsequentialid()
on newid().
newsequentialid()
can not set in code newid()
, can supplied default value on table column level.
any ideas on simplifying original query, utilising newsequentialid()
? or, original query simplified solution in achieving this?
since newsequentialid()
can used column default value, change original query to:
insert
@serverenvironmentid
if no value had been supplied, generating new sequential id , retrievingoutput
clausethen update row defined either
@serverenvironmentid
passed in originally, or new id created inserting "dummy row" table
not sure if faster / more efficient - have measurements on that.
Comments
Post a Comment