SqlServer獲取儲存過程返回值的例項

2021-07-02 22:14:07 字數 3920 閱讀 2500

sqlserver獲取儲存過程返回值的例項,需要的朋友可以參考一下

1.ouput引數返回值

複製**

**如下:

create procedure [dbo].[nb_order_insert](

@o_buyerid int ,

@o_id bigint output )

asbegin

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 output

2.return過程返回值

複製**

**如下:

create procedure [dbo].[nb_order_insert](

@o_buyerid int ,

@o_id bigint output )

asbegin

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

3.select 資料集返回值

複製**

**如下:

create procedure [dbo].[nb_order_select](

@o_id int )

asbegin

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

1.獲取return返回值

複製**

**如下:

sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["connstr"].tostring());

conn.open();

sqlcommand mycommand = new sqlcommand("nb_order", conn);  //儲存過程名字

mycommand.commandtype = commandtype.storedprocedure;   //指定型別為儲存過程

mycommand.parameters.add(new sqlparameter("@a", sqldbtype.int));

mycommand.parameters["@a"].value = 10;

mycommand.parameters.add(new sqlparameter("@b", sqldbtype.int));

mycommand.parameters["@b"].value = 20;

mycommand.parameters.add(new sqlparameter("@return", sqldbtype.int));

mycommand.parameters["@return"].direction = parameterdirection.returnvalue;

mycommand.executenonquery();                //執行儲存過程

response.write(mycommand.parameters["@return"].value.tostring()); //取得return的返回值

2.獲取output輸出引數值

複製**

**如下:

sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["connstr"].tostring());

conn.open();

sqlcommand mycommand = new sqlcommand("nb_order", conn);

mycommand.commandtype = commandtype.storedprocedure;

mycommand.parameters.add(new sqlparameter("@a", sqldbtype.int));

mycommand.parameters["@a"].value = 20;

mycommand.parameters.add(new sqlparameter("@b", sqldbtype.int));

mycommand.parameters["@b"].value = 20;

mycommand.parameters.add(new sqlparameter("@c", sqldbtype.int));

mycommand.parameters["@c"].direction = parameterdirection.output;

mycommand.executenonquery();

response.write(mycommand.parameters["@c"].value.tostring()); //指定取得儲存過程的返回值

c#接收儲存過程返回值:

複製**

**如下:

public static int user_add(user us)

catch (sqlexception ex)

finally

return iret; }

c#接收儲存過程的輸出引數:

複製**

**如下:

public static decimal cart_useramount(int uid)

catch (sqlexception ex)

finally

return iret; }

c#取得結果集:

複製**

**如下:

string sqlw = string.format("exec sp_userinfo ", uid);

datatable dsuser = sqlconn.getdataset(sqlw).tables[0];

public static dataset getdataset(string sql)

獲取儲存過程內容 SQL server

查詢儲存過程 內容 select name 儲存過程名稱,definition 儲存過程內容 from sys.sql modules as m inner join sys.all objects as o on m.object id o.object id where o.type p and...

SqlServer如何獲取儲存過程的返回值

1.output引數返回值 1 create procedure dbo upinformation 2 age int,3 id bigint output4 5as6begin 7set nocount on 8begin 9insert into information age 10value...

SQL SERVER 獲取儲存過程返回值的幾種方法

1 不帶任何引數的儲存過程 儲存過程語句中含有return 建立儲存過程 create procedure testreturn asreturn 145go 執行儲存過程 declare rcint exec rc testreturn select rc 說明 查詢結果為145 2 帶輸入引數的...