asp調用預存程序2

來源:互聯網
上載者:User
5. 同時具有傳回值、輸入參數、輸出參數的預存程序
    前面說過,在調用預存程序時,聲明參數的順序要與預存程序中定義的順序相同。還有一點要特別注意:如果預存程序同時具有傳回值以及輸入、輸出參數,傳回值要最先聲明。

    為了示範這種情況下的調用方法,我們改善一下上面的例子。還是取得ID為1的使用者的使用者名稱,但是有可能該使用者不存在(該使用者已刪除,而userid是自增長的欄位)。預存程序根據使用者存在與否,返回不同的值。此時,預存程序和ASP代碼如下:

    /*SP5*/
    CREATE PROCEDURE dbo.getUserName

       --為了加深對"順序"的印象,將以下兩參數的定義順序顛倒一下
       @UserName varchar(40) output,

       @UserID int

    as

    set nocount on

    begin

       if @UserID is null return

       select @UserName=username

           from dbo.[userinfo]

           where userid=@UserID

       if @@rowcount>0

          return 1

       else

          return 0

       return

    end

    go

    '**調用同時具有傳回值、輸入參數、輸出參數的預存程序**

    DIM MyComm,UserID,UserName

    UserID = 1

    Set MyComm = Server.CreateObject("ADODB.Command")

    with MyComm

       .ActiveConnection = MyConStr          'MyConStr是資料庫連接字串
       .CommandText      = "getUserName"     '指定預存程序名

       .CommandType      = 4                 '表明這是一個預存程序

       .Prepared         = true              '要求將SQL命令先行編譯
       '傳回值要最先被聲明
       .Parameters.Append .CreateParameter("RETURN",2,4)

       '以下兩參數的聲明順序也做相應顛倒
       .Parameters.append .CreateParameter("@UserName",200,2,40)

       .Parameters.append .CreateParameter("@UserID",3,1,4,UserID)

       .Execute

    end with

    if MyComm(0) = 1 then

       UserName = MyComm(1)

    else

       UserName = "該使用者不存在"

    end if

    Set MyComm = Nothing



    6. 同時返回參數和記錄集的預存程序

    有時候,我們需要預存程序同時返回參數和記錄集,比如在利用預存程序分頁時,要同時返回記錄集以及資料總量等參數。以下給出一個進行分頁處理的預存程序:

    /*SP6*/
    CREATE PROCEDURE dbo.getUserList

       @iPageCount int OUTPUT,   --總頁數
       @iPage int,               --當前頁號

       @iPageSize int            --每頁記錄數
    as

    set nocount on

    begin

       --建立暫存資料表

       create table #t (ID int IDENTITY,   --自增欄位
                        userid int,

                        username varchar(40))

       --向暫存資料表中寫入資料
       insert into #t

          select userid,username from dbo.[UserInfo]

             order by userid

      

       --取得記錄總數

       declare @iRecordCount int

       set @iRecordCount = @@rowcount

       --確定總頁數

       IF @iRecordCount%@iPageSize=0

          SET @iPageCount=CEILING(@iRecordCount/@iPageSize)

       ELSE

          SET @iPageCount=CEILING(@iRecordCount/@iPageSize)+1


       --若請求的頁號大於總頁數,則顯示最後一頁

       IF @iPage > @iPageCount

          SELECT @iPage = @iPageCount

       --確定當前頁的始末記錄
       DECLARE @iStart int    --start record

       DECLARE @iEnd int      --end record

       SELECT @iStart = (@iPage - 1) * @iPageSize

       SELECT @iEnd = @iStart + @iPageSize + 1

       --取當前頁記錄   

       select * from #t where ID>@iStart and ID<@iEnd

       --刪除暫存資料表

       DROP TABLE #t

       --返回記錄總數

       return @iRecordCount

    end

    go

    在上面的預存程序中,輸入當前頁號及每頁記錄數,返回當前頁的記錄集,總頁數及記錄總數。為了更具典型性,將記錄總數以傳回值的形式返回。以下是調用該預存程序的ASP代碼(具體的分頁操作略去):

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.