儲存過程的三種返回值與其獲得方法

2021-08-23 15:51:03 字數 1795 閱讀 8195

create table [dbo].[order](

[o_id] [bigint] identity(1,1) not for replication not null,

[o_buyerid] [int] not null

)1.ouput引數返回值

例: 向order表插入一條記錄,返回其標識

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

endend

儲存過程中獲得方法:

declare @o_buyerid int

declare @o_id bigint

exec [nb_order_insert] @o_buyerid ,o_id bigint

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_shopid))

begin

insert into [order](o_buyerid )

values (@o_buyerid )

set @o_id = @@identity

return 1 — 插入成功返回1

endelse

return 0 — 插入失敗返回0

end儲存過程中的獲取方法

declare @o_buyerid int

declare @o_id bigint

declare @result bit

exec @result = [nb_order_insert] @o_buyerid ,o_id bigint

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儲存過程中的獲取方法:

3.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] — 刪除臨時表

3.2使用openrowset(不過就是速度不怎麼樣)

select * from openrowset(』provider_name','trusted_connection=yes』,'exec nb_order_select』)

儲存過程的三種返回值與其獲得方法

create table dbo order o id bigint identity 1,1 not for replication not null,o buyerid int not null 1.ouput引數返回值 例 向order表插入一條記錄,返回其標識 create procedur...

獲得儲存過程返回值的方法 return的值

獲得儲存過程的返回值 system.data.sqlclient.sqlconnection scon new system.data.sqlclient.sqlconnection server netangel uid etopsus pwd etops database etops syste...

mybatis呼叫儲存過程獲得取返回值

文章目錄 前言 一 service層 二 dao層 三 map.xml 總結mybatis呼叫儲存過程的一種寫法記錄 override public returnmsg checkonlinebookout mapparam param mapparams new hashmap params.pu...