Sql Server Proc 先看看簡單吧


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

`調用時可以傳值也可以不傳

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM