sql – 返回NEWSEQUENTIALID()作为输出参数
发布时间:2020-05-23 16:04:42 所属栏目:MsSql 来源:互联网
导读:想象一下这样的表: CREATE TABLE [dbo].[test]( [id] [uniqueidentifier] NULL, [name] [varchar](50) NULL)GOALTER TABLE [dbo].[test] ADD CONSTRAINT [DF_test_id] DEFAULT (newsequentialid()) FOR [id
|
想象一下这样的表: 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 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
