当前位置: 编程技术>.net/c#/asp.net
Sql Server获取存储过程返回值的综合例子
来源: 互联网 发布时间:2014-08-30
本文导语: 一、Sql Server存储过程反回值应用举例 1、OUPUT参数返回值 代码示例: CREATE PROCEDURE [dbo].[nb_order_insert]( @o_buyerid int , @o_id bigint OUTPUT ) AS BEGIN SET NOCOUNT ON; BEGIN INSERT INTO [Order](o_buyerid ) VALUES (@o_buyerid ) SET @o_id = @@IDENTITY END E...
一、Sql Server存储过程反回值应用举例
1、OUPUT参数返回值
代码示例:
CREATE PROCEDURE [dbo].[nb_order_insert](
@o_buyerid int ,
@o_id bigint OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN
INSERT INTO [Order](o_buyerid )
VALUES (@o_buyerid )
SET @o_id = @@IDENTITY
END
END
@o_buyerid int ,
@o_id bigint OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN
INSERT INTO [Order](o_buyerid )
VALUES (@o_buyerid )
SET @o_id = @@IDENTITY
END
END
调用示例:
代码示例:
DECLARE @o_buyerid int
DECLARE @o_id bigint
EXEC [nb_order_insert] @o_buyerid,@o_id output
DECLARE @o_id bigint
EXEC [nb_order_insert] @o_buyerid,@o_id output
2、RETURN过程返回值
代码示例:
CREATE PROCEDURE [dbo].[nb_order_insert](
@o_buyerid int ,
@o_id bigint OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
IF(EXISTS(SELECT * FROM [Shop] WHERE [s_id] = @o_buyerid ))
BEGIN
INSERT INTO [Order](o_buyerid ) VALUES (@o_buyerid )
SET @o_id = @@IDENTITY
RETURN 1 — 插入成功返回1
END
ELSE
RETURN 0 — 插入失败返回0 END
@o_buyerid int ,
@o_id bigint OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
IF(EXISTS(SELECT * FROM [Shop] WHERE [s_id] = @o_buyerid ))
BEGIN
INSERT INTO [Order](o_buyerid ) VALUES (@o_buyerid )
SET @o_id = @@IDENTITY
RETURN 1 — 插入成功返回1
END
ELSE
RETURN 0 — 插入失败返回0 END
调用示例:
代码示例:
DECLARE @o_buyerid int
DECLARE @o_id bigint
DECLARE @result bit
EXEC @result = [nb_order_insert] @o_buyerid ,o_id output
DECLARE @o_id bigint
DECLARE @result bit
EXEC @result = [nb_order_insert] @o_buyerid ,o_id output
3、SELECT 数据集返回值
代码示例:
CREATE PROCEDURE [dbo].[nb_order_select](
@o_id int
)
AS
BEGIN
SET NOCOUNT ON;
SELECT o_id,o_buyerid FROM [Order]
WHERE o_id = @o_id
GO
@o_id int
)
AS
BEGIN
SET NOCOUNT ON;
SELECT o_id,o_buyerid FROM [Order]
WHERE o_id = @o_id
GO
调用示例:
1)、使用临时表
代码示例:
CREATE TABLE [dbo].[Temp](
[o_id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[o_buyerid] [int] NOT NULL
)
INSERT [Temp] EXEC [nb_order_select] @o_id
– 这时 Temp 就是EXEC执行SELECT 后的结果集
SELECT * FROM [Temp]
DROP [Temp] — 删除临时表
[o_id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[o_buyerid] [int] NOT NULL
)
INSERT [Temp] EXEC [nb_order_select] @o_id
– 这时 Temp 就是EXEC执行SELECT 后的结果集
SELECT * FROM [Temp]
DROP [Temp] — 删除临时表
1 2 下一页 尾页