想象一下这样的表:
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
解决方法
@H_301_13@ 使用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