CREATE PRoc [名字] { @引數 資料型別, @引數 資料型別 OUTPUT[輸入] } AS begin select INSERT UPDATE (SQL) end --基本陳述句快
--以上是陳述句庫
--先看看不帶引數的吧 他跟方法一樣 可以帶引數也可以不帶引數(當然我沒用過幾次不帶引數的)
--獲取一個表吧這種都感覺像視圖了
IF(SELECT * FROM sysobjects WHERE Name ='proc_table') DROP PROC proc_table GO CREATE PROC proc_table AS SELECT * FROM Users WHERE S_ID='' GO EXEC proc_table
--帶引數的吧--就看看登錄的SQL吧
IF(SELECT * FROM sysobjects WHERE Name ='P_LOG') DROP PROC P_LOG GO CREATE PROC P_LOG @acctount VARCHAR(50), @accountpwd VARCHAR(50) AS BEGIN SELECT COUNT(*) FROM Users WHERE U_LoginName=@acctount AND U_Password=@accountpwd; END EXEC P_LOG'1','123456'
--C#orjava 調了之后直接判斷有沒有值即可
--這是回傳單行單列,需要用回傳單行單列的放方法去接收
--再看看輸出引數的存盤程序
在JAVA中我們需要呼叫帶參的方法時需要傳參給形參,列入比較兩個大小的方法 int compare( int first ,int second),比較10 和20的大小,則呼叫形式:tempcompare(10,20),方法compare回傳值賦值給變數為tmp.
存盤程序中也有與很像是,有兩種型別的引數的引數
~輸入引數:呼叫是像存盤程序傳實參,用來向PROC傳值
~輸出引數: 同JAVA 如果希望引數可以帶出方法,則可以使用輸出引數值帶出方法,則可以輸出引數,通過定義引數 "OUTPUT"標記 ,表明該引數是輸出引數 ,執行存盤程序后吧 回傳值存放在輸出中-
可以給其他T-SQL 陳述句訪問,
CREATE proc [dbo].[P_GetConsumeOrderPaged] @PageSize int, @PageIndex int, @Count int output, @MC_CradID varchar(20), @MC_Mobile varchar(20), @BeginDate varchar(20), @EndDate varchar(20), @CO_OrderType int, @S_ID int as begin select top(@PageSize) * from ConsumeOrders M inner join MemCards A on M.MC_ID=A.MC_ID inner join CategoryItems B on M.CO_OrderType=B.CI_ID where A.S_ID=@S_ID and M.CO_ID not in( select top(@PageSize*(@PageIndex-1)) M.CO_ID from ConsumeOrders M inner join MemCards A on M.MC_ID=A.MC_ID inner join CategoryItems B on M.CO_OrderType=B.CI_ID and A.S_ID=@S_ID and B.C_Category='CO_OrderType' and ((A.MC_CardID=@MC_CradID or A.MC_Mobile=@MC_Mobile) or (@MC_CradID='' and @MC_Mobile='')) and ((@BeginDate='' or @EndDate='') or (M.CO_CreateTime between @BeginDate and @EndDate)) and ((@CO_OrderType=0) or (M.CO_OrderType=@CO_OrderType)) ) and B.C_Category='CO_OrderType' and ((A.MC_CardID=@MC_CradID or A.MC_Mobile=@MC_Mobile) or (@MC_CradID='' and @MC_Mobile='')) and ((@BeginDate='' or @EndDate='') or (M.CO_CreateTime between @BeginDate and @EndDate)) and ((@CO_OrderType=0) or (M.CO_OrderType=@CO_OrderType)) select @Count=COUNT(*) from ConsumeOrders M inner join MemCards A on M.MC_ID=A.MC_ID inner join CategoryItems B on M.CO_OrderType=B.CI_ID where A.S_ID=@S_ID and B.C_Category='CO_OrderType' and ((A.MC_CardID=@MC_CradID or A.MC_Mobile=@MC_Mobile) or (@MC_CradID='' and @MC_Mobile='')) and ((@BeginDate='' or @EndDate='') or (M.CO_CreateTime between @BeginDate and @EndDate)) and ((@CO_OrderType=0) or (M.CO_OrderType=@CO_OrderType)) end GO
-- 創建引數帶有默認值的PROC
在調PROC時,有些引數變化很少,這時,可以給這些引數一個默認值,即使呼叫時不輸入值,也會在存盤程序中使用默認值,在很大程度上方便調,
IF(SELECT * FROM sysobjects WHERE Name ='proc_insertstu') DROP PROC proc_insertstu GO create pro proc_insertstu @stuname varchar(20), @stusex char(2)='男', @classid int =2 AS begin INSERT INTO stuinfoO(StuName,stusexmclassid) values(@stuname ,@stusex,@classid ) end go exec proc_insertstu'唐勝' exec proc_insertstu'‘‘ZHUBAJIE’@CLASSID=1
`呼叫時可以傳值也可以不傳
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/7387.html
標籤:SQL Server
上一篇:查詢資料庫創建時間
