在提供PK时,请获取SQL插入物进行工作
-
28-09-2019 - |
题
我有以下存储过程:
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
ServerenVironmentID列是一个主键,其默认设置为(newSequentionId())。
我需要此存储过程来适用于2种情况:
- 为ServerenvironMentID提供的值 - 作品。
- 无法为serverenvironmentID提供的值 - 不起作用 - 无法插入null值。我认为通过在此列上设置默认值,这很好。
有人请帮助加重此过程,以便对两种情况有效。解决方案需要最小的变化,因为所有SP当前都遵循这一趋势。
其他提示
默认值仅在插入列表中不包含该列时仅应用于插入。我建议以下不是完全琐碎的变化(我已经评论了要删除的行):
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
此过程不会使用默认约束,但是如果还有其他地方可以将行添加到表格中,则可以将其保留在适当的位置。
(我的第一个答案很长,所以这个答案很长,所以我要发布第二个答案。)
我想念您正在使用newSequentionID。同样,如果在插入语句中指定了一列,则将不使用分配给该列的任何默认值[除非您在插入语句中使用默认关键字,但这仍然是全部或全部内容 - 您不能说“如果@Var为null,则默认为默认值”. 。我认为您被简单的分支和半冗余代码所困扰:
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
(为什么在插入过程中锁定整个桌子?)
不隶属于 StackOverflow