Question

J'ai la procédure stockée suivante:

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 colonne ServerEnvironmentId est une clé primaire avec un ensemble par défaut sur elle, qui est (newsequentialid ()).

J'ai besoin de cette procédure stockée au travail pour 2 scénarios:

  1. Valeur fournie pour ServerEnvironmentId -. WORKS
  2. Valeur non fournie pour ServerEnvironmentId - NE FONCTIONNE PAS - NE PEUT PAS INSERT NULL VALEUR. Je pensais en définissant un défaut sur cette colonne ce serait bien.

Quelqu'un vous plaît aider à ammend cette procédure afin qu'il puisse travailler pour les deux scénarios. besoins de solution pour avoir des changements minimes que tous les SPs suivent actuellement cette tendance.

Autres conseils

Les valeurs par défaut ne sont appliquées que sur des inserts si la colonne ne figure pas dans la liste INSERT. Je vous recommande la suite d'un changement pas tout à fait trivial (je l'ai fait remarquer les lignes à supprimer):

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

La contrainte par défaut ne sera pas utilisé par cette procédure, mais vous pouvez le laisser en place s'il y a d'autres endroits où les lignes peuvent être ajoutées à la table.

(Ma première réponse a été longue et il celui-ci, donc je suis une deuxième réponse l'affichage.)

J'ai manqué que vous utilisiez newsequentialid. Encore une fois, si une colonne est spécifiée dans l'instruction d'insertion, toutes les valeurs par défaut affectées à cette colonne ne seront pas utilisés [ sauf si vous utilisez le mot-clé DEFAULT dans l'instruction INSERT, mais qui est encore tout ou rien - vous ne pouvez pas dire "si @var est nulle alors DEFAULT"] . Je pense que vous êtes coincé avec une ramification simple et un code semi-redondant, le long des lignes de:

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

(Pourquoi verrouiller la table entière pendant l'insert?)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top