sql server - Get SQL Insert to work when PK is supplied or NOT -


i have following stored procedure:

alter procedure dbo.appl_serverenvironmentinsert (     @serverenvironmentname varchar(50),     @serverenvironmentdescription varchar(1000),     @usercreatedid uniqueidentifier,     @serverenvironmentid uniqueidentifier output ) recompile     -- stores serverenvironmentid.     declare @appl_serverenvironment table (serverenvironmentid uniqueidentifier)      -- insert data table.     insert appl_serverenvironment with(tablockx)     (         serverenvironmentname,         serverenvironmentdescription,         datecreated,         usercreatedid     )     output inserted.serverenvironmentid @appl_serverenvironment     values     (         @serverenvironmentname,         @serverenvironmentdescription,         getdate(),         @usercreatedid     )      -- if @serverenvironmentid not supplied.     if (@serverenvironmentid null)     begin         -- serverenvironmentid.         select @serverenvironmentid = serverenvironmentid         @appl_serverenvironment     end 

the serverenvironmentid column primary key default set on it, (newsequentialid()).

i need stored procedure work 2 scenarios:

  1. value supplied serverenvironmentid - works.
  2. value not supplied serverenvironmentid - not work - cannot insert null value. thought setting default on column fine.

someone please ammend procedure may work both scenarios. solution needs have minimal changes sp's following trend.

default values applied on inserts if column not included in insert list. i'd recommend following not entirely trivial change (i've commented out lines removed):

alter 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 @serverenvironmentname null         set @serverenvironmentname = newid()      -- insert data table.      insert appl_serverenvironment with(tablockx)      (          serverenvironmentname,          serverenvironmentdescription,          datecreated,          usercreatedid      )      --output inserted.serverenvironmentid @appl_serverenvironment      values      (          @serverenvironmentname,          @serverenvironmentdescription,          getdate(),          @usercreatedid      )       ---- if @serverenvironmentid not supplied.      --if (@serverenvironmentid null)      --begin      --    -- serverenvironmentid.      --    select @serverenvironmentid = serverenvironmentid      --    @appl_serverenvironment      --end 

the default constraint not used procedure, can leave in place if there other places rows may added table.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -