想象一下这样一个表:
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并将其作为输出参数返回的最佳方法是什么?
发布于 2010-07-26 21:21:58
使用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发布于 2010-07-26 21:15:33
试一试
SELECT @ID = ID FROM Test WHERE Name = @Name(如果名称具有唯一约束)
https://stackoverflow.com/questions/3335014
复制相似问题