Domanda

Ho il seguente 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

La colonna ServerEnvironmentId è una chiave primaria con un insieme predefinito su di esso, che è (NEWSEQUENTIALID ()).

Ho bisogno di questo stored procedure per lavoro per 2 scenari:

  1. valore fornito per ServerEnvironmentId -. WORKS
  2. Il valore non in dotazione per ServerEnvironmentId - non funziona - CAN NOT NULL INSERIRE VALORE. Ho pensato impostando un default su questa colonna questo sarebbe bene.

Qualcuno per favore aiutatemi a ammend questa procedura in modo che possa funzionare per entrambi gli scenari. bisogni soluzione per avere minime variazioni come tutti sp sta attualmente seguendo questa tendenza.

Altri suggerimenti

I valori di default vengono applicati solo su inserti se la colonna non è incluso nella lista INSERT. Mi raccomando la seguente modifica non del tutto banale (Ho commentato le linee da rimuovere):

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

Il vincolo di default non verrà utilizzato da questa procedura, ma si può lasciare al suo posto se ci sono altri luoghi in cui possono essere aggiunti righe alla tabella.

(La mia prima risposta è stata lunga e quindi è questo, quindi sto postando una seconda risposta.)

mi mancava che si stava utilizzando NEWSEQUENTIALID. Anche in questo caso, se una colonna viene specificato all'interno della dichiarazione dell'inserto, non verranno utilizzati i valori predefiniti assegnati a quella colonna [ se non si utilizza la parola chiave DEFAULT nell'istruzione INSERT, ma questo è ancora tutto o niente - non è possibile dire "se @var è nullo allora DEFAULT"] . Penso che si sono bloccati con semplici ramificazione e codice di semi-ridondante, lungo le linee di:

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

(Perché bloccare l'intera tabella durante l'inserto?)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top