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

前端之家收集整理的这篇文章主要介绍了sql – 返回NEWSEQUENTIALID()作为输出参数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
想象一下这样的表: @H_301_2@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存储过程,如下所示:

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

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

解决方法

使用Insert语句的Output子句. @H_301_2@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

猜你在找的MsSQL相关文章