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