Sql Server Proc 先看看簡單吧

CREATE PRoc [名字]
{
@參數 數據類型,
@參數 數據類型
OUTPUT[輸入]
}
AS
begin
select  INSERT UPDATE  (SQL)
end

--基本語句快

--以上是語句庫java

--先看看不帶參數的吧 他跟方法同樣 能夠帶參數也能夠不帶參數(固然我沒用過幾回不帶參數的)ui

--獲取一個表吧這種都感受像視圖了spa

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吧code

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 調了以後直接判斷有沒有值便可
--這是返回單行單列。須要用返回單行單列的放方法去接收

--再看看輸出參數的存儲過程blog

在JAVA中咱們須要調用帶參的方法時須要傳參給形參,列入比較兩個大小的方法 int compare( int first ,int second),比較10 和20的大小,則調用形式:tempcompare(10,20),方法compare返回值賦值給變量爲tmp.table

存儲過程當中也有與很像是,有兩種類型的參數的參數class

  ~輸入參數:調用是像存儲過程傳實參,用來向PROC傳值登錄

  ~輸出參數: 同JAVA 若是但願參數能夠帶出方法,則可使用輸出參數值帶出方法,則能夠輸出參數,經過定義參數 "OUTPUT"標記 ,代表該參數是輸出參數  ,執行存儲過程後吧  返回值存放在輸出中-變量

能夠給其餘T-SQL 語句訪問,object

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

`調用時能夠傳值也能夠不傳

相關文章
相關標籤/搜索