mssql 預存程序入門與執行個體應用

來源:互聯網
上載者:User

mssql 預存程序入門與執行個體應用
/*

首先來看看mssql 預存程序建立

create procedure proc_stu
  as
  select * from student
go

建立一個過程:例子
下面的語句建立的架構中的人力資來源程式remove_emp:

CREATE PROCEDURE remove_emp (employee_id NUMBER) AS
   tot_emps教程 NUMBER;
   BEGIN
      DELETE FROM employees
      WHERE employees.employee_id = remove_emp.employee_id;
   tot_emps := tot_emps - 1;
 END;


 
 關於預存程序簡單一實例看了,那麼我們來看文法
 

 CREATE { PROC | PROCEDURE } [schema_name.] procedure_name [ ; number ]
    [ { @parameter [ type_schema_name. ] data_type }
        [ VARYING ] [ = default ] [ OUT | OUTPUT ] [READONLY]
    ] [ ,...n ]
[ WITH <procedure_option> [ ,...n ] ]
[ FOR REPLICATION ]
AS { <sql_statement> [;][ ...n ] | <method_specifier> }
[;]
<procedure_option> ::=
    [ ENCRYPTION ]
    [ RECOMPILE ]
    [ EXECUTE AS Clause ]

<sql_statement> ::=
{ [ BEGIN ] statements [ END ] }

<method_specifier> ::=
EXTERNAL NAME assembly_name.class_name.method_name

好我們來看一個執行個體應用中的執行個體,
查詢id為1的記錄用預存程序執行個體

@Total int OUTPUT
-----------------------------------
SET @Sql=N'select a,b,c,d from t where id=1'
Exec sp_executesql @Sql, N'@Total Int Out',@Total Out
-----------------------------------
Return @Total


執行個體三

加入一筆記錄到表book,並查詢此表中所有書籍的總金額 
  

   Create proc insert_book
     @param1 char(10),@param2 varchar(20),@param3 money,@param4 money output
     with encryption ---------加密
     as
     insert book(編號,書名,價格) Values(@param1,@param2,@param3)
     select @param4=sum(價格) from book
     go
     執行例子:
     declare @total_price money
     exec insert_book '003','Delphi 控制項開發指南',$100,@total_price
     print '總金額為'+convert(varchar,@total_price)
     go


*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.