sql個人總結4,sql個人總結

來源:互聯網
上載者:User

sql個人總結4,sql個人總結

說到sql,無疑預存程序很重要,這篇就和大家討論儲存過程。

1:儲存過程

a:不帶參數的儲存過程

USE testGOCREATE PROCEDURE WLY    --儲存過程,不帶參數ASSELECT * FROM roleGO

b:帶參數的儲存過程

USE testGOCREATE PROCEDURE WLY1 @A INTAS DECLARE @B INTSET @B=1SET @A=@BPRINT @AGO

c:帶有輸出參數的儲存過程

USE testGOCREATE PROCEDURE WLY2 @A INT OUTPUT    --輸出參數值的參數AS DECLARE @B INTSET @B=1select @A=@BPRINT @AGO

d:預存程序的一些參數

SP_HELP WLY1    --返回儲存過程的名稱,時間,參數等SP_HELPTEXT WLY1 --返回儲存過程的建立語句SP_RENAME WLY,WLY2  --重新命名儲存過程GO

e:執行儲存過程

execute dbo.WLYEXECUTE WLY1 1 --有參數的儲存過程DECLARE @C INT    --帶參數的輸出EXECUTE WLY2 @C OUTPUTGO

2:遊標

DECLARE C_ROLE CURSOR SCROLLFOR SELECT RID,RNAME FROM ROLEFOR READ ONLYDECLARE @COUNT INT,@VRID INT,@VRNAME VARCHAR(1)OPEN C_ROLEFETCH NEXT FROM C_ROLEINTO @VRID,@VRNAMESET @COUNT=0WHILE @@FETCH_STATUS=0BEGIN     PRINT @VRID    PRINT @VRNAME    PRINT @COUNT    SET @COUNT=@COUNT+1    FETCH NEXT FROM C_ROLE INTO @VRID,@VRNAMEENDCLOSE C_ROLEDEALLOCATE C_ROLEGO 

我感覺遊標的使用盡量的避免,因為是取出資料再存起來,如果資料量很大的話就不好了。


相關文章

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.