當前位置:首頁 » 存儲配置 » mysql存儲過程參數定義

mysql存儲過程參數定義

發布時間: 2022-09-01 02:36:54

A. mysql存儲過程函數怎麼定義變數

以 DECLARE 關鍵字聲明的變數,只能在存儲過程中使用,稱為存儲過程變數,
例如:
DECLARE var1 INT DEFAULT 0;
主要用在存儲過程中,或者是給存儲傳參數中。

B. 淺談MySQL存儲過程中declare和set定義變數的區別

MySQL存儲過程中,定義變數有兩種方式:
1.使用set或select直接賦值,變數名以 @ 開頭.
例如:set @var=1;
可以在一個會話的任何地方聲明,作用域是整個會話,稱為會話變數。

2.以 DECLARE 關鍵字聲明的變數,只能在存儲過程中使用,稱為存儲過程變數,例如:
DECLARE var1 INT DEFAULT 0;
主要用在存儲過程中,或者是給存儲傳參數中。

兩者的區別是:
在調用存儲過程時,以DECLARE聲明的變數都會被初始化為 NULL。而會話變數(即@開頭的變數)則不會被再初始化,在一個會話內,只須初始化一次,之後在會話內都是對上一次計算的結果,就相當於在是這個會話內的全局變數。

在存儲過程中,使用動態語句,預處理時,動態內容必須賦給一個會話變數。
例:
set @v_sql= sqltext;
PREPARE stmt FROM @v_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

C. 在MySQL中如何創建一個帶輸出參數為一個表類型的存儲過程

在MySQL中如何創建一個帶輸出參數為一個表類型的存儲過程
首先需要知道「另一個存儲過程」的結果集的所有列的類型。
假設「另一個存儲過程」的名字是sp1,沒有參數,返回的結果集共3列,全部為int型,那麼「存儲過程」里添加一個與結果集列數相同的臨時表或表變數用於接收「另一個存儲過程」的結果集
如下
CREATE PROCEDURE sp2
AS
DECLARE @t table(a int,b int,c int)

INSERT INTO @t(a,b,c)
EXEC sp1

SELECT * FROM @t
使用SQLSERVER存儲過程可以很大的提高程序運行速度,簡化編程維護難度,現已得到廣泛應用。
創建存儲過程
和數據表一樣,在使用之前需要創建存儲過程,它的簡明語法是:

引用:
Create PROC 存儲過程名稱
[參數列表(多個以「,」分隔)]
AS
SQL 語句

例:

引用:
Create PROC upGetUserName
@intUserId INT,
@ostrUserName NVARCHAR(20) OUTPUT -- 要輸出的參數
AS
BEGIN
-- 將uName的值賦給 @ostrUserName 變數,即要輸出的參數
Select @ostrUserName=uName FROM uUser Where uId=@intUserId
END

其中 Create PROC 語句(完整語句為Create PROCEDURE)的意思就是告訴SQL SERVER,現在需要建立一個存儲過程,upGetUserName 就是存儲過程名稱,@intUserId 和 @ostrUserName 分別是該存儲過程的兩個參數,注意,在SQL SERVER中,所有用戶定義的變數都以「@」開頭,OUTPUT關鍵字表示這個參數是用來輸出的,AS之後就是存儲過程內容了。只要將以上代碼在「查詢分析器」里執行一次,SQL SERVER就會在當前資料庫中創建一個名為「upGetUserName」的存儲過程。你可以打開「企業管理器」,選擇當前操作的資料庫,然後在左邊的樹型列表中選擇「存儲過程」,此時就可以在右邊的列表中看到你剛剛創建的存儲過程了(如果沒有,刷新一下即可)。
二、存儲過程的調用

之前已經創建了一個名為「upGetUserName」的存儲過程,從字面理解該存儲過程的功能是用來取得某一個用戶的名稱。存儲過程建立好了,接下來就是要在應用程序里調用了,下面看一下在ASP程序里的調用。

D. mysql 存儲過程中變數的定義與賦值操作

一、變數的定義
mysql中變數定義用declare來定義一局部變數,該變數的使用范圍只能在begin...end
塊中使用,變數必須定義在復合語句的開頭,並且是在其它語句之前,也可以同時申明多個變數,如果需要,可以使用default賦默認值。
定義一個變數語法如下:
declare
var_name[,...]
type[default
value]看一個變數定義實例
declare
last
date;二、mysql存儲過程變數賦值
變數的賦值可直接賦值與查詢賦值來操作,直接賦值可以用set來操作,可以是常量或表達式如果下
復制代碼
代碼如下:
set
var_name=
[,var_name
expr]...給上面的last變數賦值方法如下
set
last
=
date_sub(
current_date(),interval
1
month);下面看通過查詢給變數賦值,要求查詢返回的結果必須為一行,具體操作如下
select
col
into
var_name[,...]
table_expr我們來通過查詢給v_pay賦值。
create
function
get
_cost(p_custid
int,p_eff
datetime)
return
decimal(5,2)
deterministic
reads
sql
data
begin
declare
v_pay
decimail(5,2);
select
ifnull(
sum(pay.amount),0)
into
vpay
from
payment
where
pay.payd<=p_eff
and
pay.custid=pid
reutrn
v_rent
+
v_over
-
v_pay;
end
$$
好了,這篇簡單的存儲過程中變數的定義賦值教程就到這里了,下面我們會接著講關於myql存儲過程的條件的定義與處理。
以下是其它網友的補充
在MySQL的存儲過程中,可以使用變數,它用於保存處理過程中的值。
定義變數使用DECLARE語句,語法格式如下:
DECLARE
var_name[,...]
type
[DEFAULT
value]
其中,var_name為變數名稱,type為MySQL支持的任何數據類型,可選項[DEFAULT
value]為變數指定默認值。一次可以定義多個同類型的變數,各變數名稱之間以逗號「,」隔開。
定義與使用變數時需要注意以下幾點:

DECLARE語句必須用在DEGIN…END語句塊中,並且必須出現在DEGIN…END語句塊的最前面,即出現在其他語句之前。

DECLARE定義的變數的作用范圍僅限於DECLARE語句所在的DEGIN…END塊內及嵌套在該塊內的其他DEGIN…END塊。

存儲過程中的變數名不區分大小寫。
定義後的變數採用SET語句進行賦值,語法格式如下:
SET
var_name
=
expr
[,var_name
=
expr]
...
其中,var_name為變數名,expr為值或者返回值的表達式,可以使任何MySQL支持的返回值的表達式。一次可以為多個變數賦值,多個「變數名=值」對之間以逗號「,」隔開。
例如:
復制代碼
代碼如下:
begin
declare
no
varchar(20);
declare
title
varchar(30);
set
no='101010',title='存儲過程中定義變數與賦值';
end
提示:存儲過程中所有的關鍵字也是不區分大小寫的,如BEGIN可以寫出begin。

E. mysql 存儲過程

你應該在做統計吧,估計你不會的就是mysql存儲過程的語法 我之前也寫過 很是郁悶 我給你一段代碼 是我用mysql寫過的一個存儲過程 你看看 主要是了解裡面的語法 看懂了 你所說的需求並不難
有看不懂的地方一起討論 :
begin

declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moleid int default 0;
declare couid int default 0;
declare mname varchar(255) ;
declare opsid int default 0;

declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status<>0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moleID from space_operation where status<>0 and ID=opsid into moleid;
if moleid <> '' then
select Nvc_identification from space_operation where status<>0 and ID=opsid into @identiftion;
if moleid > 0 then
Select Nvc_ename from space_mole where status<>0 and ID=moleid into mname;
else
set mname = 'space';
end if;

create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status<>0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;

set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID > @ihod order by ID asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;

if @content0 <> '' then
set @contentp = CONCAT(@operarry1,concat('|',@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));
end if;

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;

until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;

UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID > @uoj order by ID asc limit 0,1;
if @io1k <> '' then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;

end

F. mysql存儲過程和函數怎麼寫

1、函數必須指定返回值,且參數默認為IN類型。
2、存儲過程沒返回值,參數可以是 IN,OUT,IN OUT類型,有的人可能會理解成OUT 也算是返回值。
3、調用方式:函數 select my_fun() ;過程 call my_pro( ) ;
4、DEMO

mysql> call my_pro(1,2,@c);
Query OK, 0 rows affected (0.00 sec)

mysql> select @c;
+------+
| @c |
+------+
| 3 |
+------+
1 row in set (0.00 sec)

mysql> select my_fun(1,2);
+-------------+
| my_fun(1,2) |
+-------------+
| 3 |
+-------------+
1 row in set (0.00 sec)

G. mysql 存儲過程參數

MySQL 存儲過程, 定義參數, 不需要加那個 @


例子代碼如下:


mysql> DELIMITER //
mysql> CREATE PROCEDURE HelloWorld1(vUserName VARCHAR(10))
-> BEGIN
-> SELECT CONCAT('Hello ', vUserName);
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> call HelloWorld1('Edward');
-> //
+-----------------------------+
| CONCAT('Hello ', vUserName) |
+-----------------------------+
| Hello Edward |
+-----------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.02 sec)

H. mysql中存儲過程是什麼意思

存儲過程(stored
procere)是一組為了完成特定功能的sql語句集,經編譯後存儲在資料庫中,用戶通過指定存儲過程的名字並給定參數(如果該存儲過程帶有參數)來調用執行它。
一個存儲過程是一個可編程的函數,它在資料庫中創建並保存。

I. mysql 存儲過程總結(二)if語句、參數

1、if :用於做條件判斷,具體的語法結構為:

在if條件判斷的結構中,ELSE IF 結構可以有多個,也可以沒有。 ELSE結構可以有,也可以沒有。

案列:

根據定義的分數score變數,判定當前分數對應的分數等級。

score >= 90分,等級為優秀。

score >= 80分,等級為良好

score >= 60分,等級為及格

score < 60分,等級為不及格。

上述的需求我們雖然已經實現了,但是也存在一些問題,比如:score 分數我們是在存儲過程中定義 死的,而且最終計算出來的分數等級,我們也僅僅是最終查詢展示出來而已。

那麼我們能不能,把score分數動態的傳遞進來,計算出來的分數等級是否可以作為返回值返回呢? 答案是肯定的,我們可以通過接下來所講解的 參數 來解決上述的問題。

2、參數的類型

主要分為以下三種:IN、OUT、INOUT。 具體的含義如下:

(1)in :該類參數作為輸入,也就是需要調用時傳入值 默認

(2)out:該類參數作為輸出,也就是該參數可以作為返回值

(3)inout:既可以作為輸入參數,也可以作為輸出參數

用法:

案例一:

案列二:

J. mysql 存儲過程 DDL 參數

MySQL8.0 開始支持原⼦ DDL(atomic DDL),數據字典的更新,存儲引擎操作,寫⼆進制日誌結合成了一個事務。在沒有原⼦DDL之前,DROP TABLE test1,test2;如遇到server crash,可能會有test1被drop了,test2沒有被drop掉。下面來看下在MySQL8.0之前和MySQL8.0 數據字典的區別

在MySQL8.0 之前,Data Dictionary除了存在與.FRM, .TRG, .OPT ⽂件外,還存在於系統表中(MyISAM ⾮事務引擎表中),在MySQL8.0 ,Data Dictionary 全部存在於Data Dictionary Storage Engine(即 InnoDB表中),這使crash recovery 維持原⼦性成為了可能


存儲引擎⽀持

目前,只有InnoDB存儲引擎⽀持原子DDL,為了實現原子DDL,Innodb要寫DDL logs 到 mysql.innodb_ddl_log 表,這是⼀個隱藏在mysql.ibd 數據字典表空間⾥的數據字典表。要看mysql.innodb_ddl_log 中的內容,需要

SET GLOBALLOG_ERROR_VERBOSITY=3;(MySQL 8.0 默認為2,error log 記錄Errors and

warnings,不不記錄notes)

SET GLOBAL innodb_print_ddl_logs=1;

CREATE TABLEt1 (c1 INT)ENGINE=InnoDB;

查看error log

[Note] [MY-011066] InnoDB: DDL loginsert: [DDLrecord:DELETE SPACE,id=30,

thread_id=25, space_id=9, old_file_path=./test/t1.ibd]

[Note] [MY-011066]InnoDB:DDL logdelete:by id30

[Note] [MY-011066]InnoDB:DDL loginsert: [DDLrecord: REMOVECACHE,id=31,

thread_id=25, table_id=1066, new_file_path=test/t1]

[Note] [MY-011066]InnoDB:DDL logdelete:by id31

[Note] [MY-011066]InnoDB:DDL loginsert: [DDLrecord: FREE,id=32, thread_

id=25, space_id=9, index_id=143, page_no=4]

[Note] [MY-011066]InnoDB:DDL log delete:by id32

[Note] [MY-011066]InnoDB:DDL logpost ddl :begin for thread id: 25

[Note] [MY-011066]InnoDB:DDL logpost ddl :end for thread id: 25


原子DDL 操作步驟

  • 准備:創建所需的對象並將DDL⽇志寫入 mysql.innodb_ddl_log表中。DDL日誌定義了如何前滾和回滾DDL操作。

  • 執行:執⾏DDL操作。例如,為CREATE TABLE操作執⾏創建。

  • 提交:更新數據字典並提交數據字典事務。

  • Post-DDL:重播並從mysql.innodb_ddl_log表格中刪除DDL⽇志。為確保回滾可以安全執⾏⽽不引⼊不⼀致性,在此最後階段執⾏⽂件操作(如重命名或刪除數據文件)。這一階段還從 mysql.innodb_dynamic_metadata的數據字典表刪除的動態元數據為了DROP TABLE,TRUNCATE和其它重建表的DDL操作。

  • ⽆論事務是提交還是回滾,DDL日誌都會mysql.innodb_ddl_log在Post-DDL階段重播並從表中刪除 。mysql.innodb_ddl_log如果伺服器在DDL操作期間暫停,DDL⽇志應該只保留在表中。在這種情況下,DDL⽇志會在恢復後重播並刪除。

    在恢復情況下,當伺服器重新啟動時,可能會提交或回退DDL事務。如果在重做⽇志和⼆進制日誌中存在DDL操作的提交階段期間執⾏的數據字典事務,則該操作被認為是成功的並且被前滾。否則,在InnoDB重放數據字典重做日誌時回滾不完整的數據字典事務 ,並且回滾DDL事務。

    原⼦DDL ⽀持類型

    • DROP TABLES , all tables dropped or none

    • DROP SCHEMA, all entities in the schema are dropped, or none

    • Note that atomic DDL statements will be rolled back or committed even in case of crash, e.g. RENAME TABLES

    • CREATE TABLE would be successfully committed or rolled back (no orphan ibd left)

    • TRUNCATE TABLE (including InnoDB tables with FTS AUX tables) would be successfully committed or rolled back

    • RENAME TABLES, all or none

    • ALTER TABLE successful or not done

    示例

    結論

    在MySQL8.0之前,alter table 操作在server crash的情況下,會遺留.frm,.ibd文件。MySQL8.0 能實現原⼦DDL(包括 DROP TABLE, DROP SCHEMA, CREATE TABLE, TRUNCATE TABLE, ALTER TABLE),alter table 操作,在server crash的情況下,不會遺留.frm,.ibd臨時文件。讓我們⼀起期待MySQL8.0 GA的到來吧!

熱點內容
安卓蘋果通訊錄怎麼同步 發布:2025-03-17 09:58:12 瀏覽:173
ai緩存 發布:2025-03-17 09:48:27 瀏覽:921
翹嘴水滴輪什麼配置 發布:2025-03-17 09:47:34 瀏覽:582
小蟻攝像機如何雲存儲 發布:2025-03-17 09:42:42 瀏覽:817
如何修改自己的名字和密碼 發布:2025-03-17 09:32:20 瀏覽:788
寶馬3系槍灰色輪轂是哪個配置 發布:2025-03-17 09:28:17 瀏覽:514
鴻蒙系統退回安卓系統怎麼備份 發布:2025-03-17 09:14:33 瀏覽:615
資料庫服務公司 發布:2025-03-17 08:56:43 瀏覽:496
我的世界伺服器載入不了區塊 發布:2025-03-17 08:56:39 瀏覽:818
如何給無線路由器設置密碼 發布:2025-03-17 08:51:40 瀏覽:887