gpt4 book ai didi

sql - 返回 NEWSEQUENTIALID() 作为输出参数

转载 作者:行者123 更新时间:2023-12-02 09:31:39 24 4
gpt4 key购买 nike

想象一个如下所示的表格:

CREATE TABLE [dbo].[test](
[id] [uniqueidentifier] NULL,
[name] [varchar](50) NULL
)

GO

ALTER TABLE [dbo].[test] ADD CONSTRAINT [DF_test_id] DEFAULT (newsequentialid()) FOR [id]
GO

使用如下所示的 INSERT 存储过程:

CREATE PROCEDURE [Insert_test]
@name as varchar(50),
@id as uniqueidentifier OUTPUT
AS
BEGIN
INSERT INTO test(
name
)
VALUES(
@name
)
END

获取刚刚插入的 GUID 并将其作为输出参数返回的最佳方法是什么?

最佳答案

使用 Insert 语句的 Output 子句。

CREATE PROCEDURE [Insert_test]
@name as varchar(50),
@id as uniqueidentifier OUTPUT
AS
BEGIN
declare @returnid table (id uniqueidentifier)

INSERT INTO test(
name
)
output inserted.id into @returnid
VALUES(
@name
)

select @id = r.id from @returnid r
END
GO

/* Test the Procedure */
declare @myid uniqueidentifier
exec insert_test 'dummy', @myid output
select @myid

关于sql - 返回 NEWSEQUENTIALID() 作为输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3335014/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com