帶輸出引數的儲存過程的使用及在C 中呼叫問題

2021-08-29 03:21:23 字數 983 閱讀 4178

建立示例儲存過程的完整的語句:

use pubs

if exists (select name from sysobjects

where name = 'up_test ' and type = 'p')

drop procedure up_test

gocreate procedure up_test

@out_val integer=0 output

--@out_val integer output ------1

asbegin

--set @out_val=0

-----2

if(1=1)

-----3

set @out_val=@out_val+1

return 9;

end儲存過程在查詢分析其中使用示例:

1、簡單呼叫

exec up_test 3

--注:呼叫時也要給輸出引數傳值,有給定預設值時可以不再給引數指定值

2、呼叫並使用儲存過程中的輸出引數和返回值

declare @ret_val int

declare @output_val int

exec @ret_val=up_test @out_val=@output_val output

-----4

select @ret_val,@output_val

值得說明的是:如果只是給定了預設值,呼叫時未給該輸出引數傳值,並且儲存過程體中並沒有諸如

set @out_val=0的顯式為

輸出引數

賦值的語句(使用

set @out_val=@out_val+1

是不行的

),那麼在執行完儲存過程後,輸出引數的帶回值是null

。這一點我在使用c#的呼叫中遇到了,還讓我除錯程式好久。後來在sql serve 查詢分析器了看到null

才恍悟。

帶輸出引數的儲存過程的使用及在C 中呼叫問題

建立示例儲存過程的完整的語句 use pubs if exists select name from sysobjects where name up test and type p drop procedure up test gocreate procedure up test out val ...

帶輸出引數的儲存過程的使用及在C 中呼叫問題

建立示例儲存過程的完整的語句 use pubs if exists select name from sysobjects where name up test and type p drop procedure up test gocreate procedure up test out val ...

帶輸出引數的儲存過程的使用及在C 中呼叫問題

建立示例儲存過程的完整的語句 use pubs if exists select name from sysobjects where name up test and type p drop procedure up test gocreate procedure up test out val ...