sybase存儲過程-----不定時更新

1.建立存儲過程sql

  create procedure [procedure_name]
  as begin
         SQL_statements [return]
   end

在存儲過程當中能夠包含SQL語句,可是不能包含:use, create view, create rule, create default, create proc, create trigger數據庫

2.執行存儲過程express

exec [procedure_name] [參數]

3.查看自建的存儲過程函數

select name from sysobjects where type="P"
go

4.查看建立的存儲過程源代碼oop

 sp_helptext [procedure_name] 

5.建立帶參數的存儲過程 例:fetch

create proc sp_show_stu1 (@sno char(7)) as 
begin 
    select * from STUDENT where SNO = @sno return
end
go

exec sp_show_stu1 @sno='9302303'
go
drop proc sp_show_stu
go

參數可缺省,設置默認值,執行時若沒傳入值則按缺省值執行spa

5.帶有返回參數的存儲過程code

if exists ( select 1 from sysobjects where name = 'test_proc' )
drop proc test_proc
go
 
if exists ( select 1 from sysobjects where name = 't123')
drop table t123
go
 
create table t123(id int primary key, col2 varchar(32) not null)
insert into t123 values(1, 'iihero')
insert into t123 values(2, 'Sybase')
insert into t123 values(3, 'ASE')
go
 
create proc test_proc (@id_min int, @num_t123 int output) with recompile
as
select @num_t123 = count( a.id ) from t123 a where a.id > = @id_min   //計算id大於等於參數@id_min的id數量存入參數@num_123中返回
go 


調用:

1> declare @num_t123 int
2> exec test_proc 1, @num_t123 output
3> go
(return status = 0)

Return parameters:

             
 ----------- 
           3 
(1 row affected)
use pubs2
go

create proc proc_num_sales (@book_id char(6) = null/* 輸入參數 */,
@tot_sales int output/* 輸出參數 */)
as
begin
/* 過程將返回對於給定書號的書的總銷售量 */
 select @tot_sales = sum(qty) 
  from salesdetail 
  where title_id = @book_id 
 return 
end
go


調用:
1> declare @tot_sales int 
2> exec proc_num_sales 'TC7777', @tot_sales output
3> go

6 存儲過程返回狀態值server

create proc procedure_name ( …… ) 
as begin 
    SQL_statements 
    return [ integer ] 
end

integer爲一整數。若是不指定,系統將自動返回一個 整數值。系統使用0表示該過程執行成功;-1至¨C14 表示該 過程執行有錯,-15至 -99爲系統保留值。
用戶通常使用大於 0的整數,或小於 -100的負整數。

7.局部變量,全局變量對象

局部變量由用戶定義 初值爲null

DECLARE @var_name data_type [, @var_name data_type] …… 舉例 declare @msg varchar(40) declare @myqty int, @myid char(4)
使用SELECT語句將指定值賦給局部變量。 
語法
select @var = expression [,@var = expression ] [from… [where…]… 舉例 declare @var1 int select @var1=99
/*
  在一個賦值給局部變量的select 語句中, 可使用常數、 從表中取值、或使用表達式給局部變量賦值。
  不能使用同一SELECT 語句既給局部變量賦值,又檢索 數據返回給客戶。 — 一個賦值給局部變量的SELECT 語句,不向用戶顯示任 何值。
*/

局部變量必須先用DECLARE定義,再用SELECT語句賦值後才能使用。 局部變量只能使用在T-SQL語句中使用常量的地方。 局部變量不能使用在表名、列名、其它數據庫對象名、保留字使用的地方。 局部變量是標量,它們擁有一個確切的值。 賦值給局部變量的SELECT語句應該返回單個值。若是賦值的SELECT語句沒有返 回值,則該局部變量的值保持不變;若是賦值的SELECT語句返回多個值,則該局 部變量取最後一個返回的值。

8.全局變量

全局變量由sql server系統提供並賦值,用戶不能建立或賦值,由@@開頭

可以使用系統存儲過程sp_monitor顯示當前全局變量的值。

經常使用全局變量  

@@error :由最近一個語句產生的錯誤號

@@rowcount : 被最近一個語句影響的行數 

@@version : sql server版本號

@@max_connections : 容許最大用戶鏈接量

@@Servername : 該sql_server的名字

select @@version declare @book_price money select @book_price = price from titles where title_id = 'BU1032' if @@rowcount = 0 print 'no such title_id' else begin print 'title_id exists with' select 'price of' = @book_price end

9. 存儲過程流程控制

IF ELSE:
部分語法(ASE) if boolean_expression statement [else [if boolean_expression1] statement1 ] 部分語法(IQ) if boolean_expression then statement [else [if boolean_expression1] statement1 ] End if
IF EXISTS 和 IF NOT EXISTS
語法(ASE)  
if [not] exists (select statement) statement block

 舉例(ASE) 舉例 /* 是否存在姓「Smith」的做者 */ 
declare @lname varchar(40) 
select @lname = 'Smith' if exists ( select * from authors where au_lname = @lname)
select 'here is a ' + @lname else select 'here is no author called'+@lname
BEGIN ... END
功能 當須要將一個以上的SQL 語句做爲一組語句對待時, 能夠 使用BEGIN 和END 將它們括起來造成一個SQL 語句塊。從 語法上看,一個SQL 語句塊至關於一個SQL 語句。在流控制 語言中, 容許用一個SQL 語句塊替代單個SQL 語句出現的地 方。
語法 BEGIN statement block END
statement block 爲一個以上的sql語句
WHILE
語法(ASE) 語法 
while boolean exprission statement block 
語法(IQ) 語法 
while boolean exprission loop statement block end loop

例:
while (select avg(price) from titles) < $40 
begin 
select title_id, price from titles where price > $20 update titles set price = price + $2 
end 
select title_id, price from titles 
print "Too much for the market to bear"

10.嵌套事務

 嵌套事務 是指在存儲過程當中的事務的間接嵌套, 即嵌套事務的造成是由於調用 含有事務的過程。@@trancount 記錄了事務嵌套級次。@@trancount在第一個 begin tran語句後值爲1,之後每遇到一個 begin tran 語句,不管是否在嵌套 過程當中,@@trancount的值增長1;每遇到 一個commit,@@trancount的值就減小 1。若@@trancount的 值 等於 零,表示當前沒有事務;若@@trancount的值不等 於零,其值 假定爲i,代表當前處於第 i 級嵌套事務中。對於嵌套事務,直 到 使用@@trancount 的值爲零的那個 commit語句被執行,整個 事務才被提交。 select @@trancount

11.存儲過程當中的遊標

語法 
create proc procedure_name as SQL_statements containing cursor processing 
其中:SQL_statements containing cursor processing 
是指包含遊標處理的SQL語句。

舉例

CREATE proc proc_fetch_book 
As BEGIN 
    DECLARE @book_title char(30), @book_id char(6) 
    DECLARE biz_book CURSOR for SECLET title, title_id from titles WHERE type = "business" OPEN biz_book FETCH biz_book INTO @book_title, @book_id …… -- 在這裏作某些處理 
CLOSE biz_book DEALLOCATE CURSOR biz_book 
RETURN 
END    

12.帶參數的函數

create function func_test(@id_min int)
returns int
as
begin
    declare @num_t123 int
    select @num_t123 = count( a.id ) from t123 a where a.id > = @id_min
    return @num_t123
end
go

1> select dbo.func_test(1)
2> go
             
 ----------- 
           5

 12.@@error

語句執行成功時 @@error值爲0;

相關文章
相關標籤/搜索