當前位置:首頁 » 存儲配置 » mysql存儲過程類型

mysql存儲過程類型

發布時間: 2023-05-11 18:04:55

㈠ Mysql裡面sql語句調用存儲過程,該如何寫

這樣:

CREATEPROCEDUREsp_add(a int, b int,outc int)

begin

set c=a+ b;

end;

調用過程:

call sp_add (1,2,@a);

select @a;

(1)mysql存儲過程類型擴展閱讀:

注意事項

存儲過程(stored procere)是一組為了完成特定功能的SQL語句集合,經編譯後存儲在伺服器端的資料庫中,利用存儲過程可以加速SQL語句的執行。

存儲過程分為系統存儲過程和自定義存儲過程。

系統存儲過程在master資料庫中,但是在其他的資料庫中可以直接調用,並且在調用時不必在存儲過程前加上資料庫名,因為在創建一個新資料庫時,系統存儲過程在新的資料庫中會自動創建。

自定義存儲過程,由用戶創建並能完成某一特定功能的存儲過程,存儲過程既可以有參數又有返回值,但是它與函數不同,存儲過程的返回值只是指明執行是否成功,並不能像函數那樣被直接調用,只能利用execute來執行存儲過程。

創建存儲過程

SQL Server創建存儲過程:

create procere 過程名

@parameter 參數類型

@parameter 參數類型

。。。

as

begin

end

執行存儲過程:execute 過程名

㈡ mysql 存儲過程和函數的區別

mysql 存儲過程和函數的區別

1、

存儲過程實現的功能要復雜一點,函數實現的功能針對性比較強。

存儲過程,功能強大,可以執行包括修改表等一系列資料庫操作;

用戶定義函數不能用於執行一組修改全局資料庫狀態的操作。


2、

對於存儲過程來說可以返回參數,如記錄集,函數只能返回值或者表對象。

函數只能返回一個變數;而存儲過程可以返回多個;

存儲過程的參數可以有IN,OUT,INOUT三種類型,而函數只能有IN類;

存儲過程聲明時不需要返回類型,而函數聲明時需要描述返回類型,且函數體中必須包含一個有效的RETURN語句。



3、

存儲過程,可以使用非確定函數,不允許在用戶定義函數主體中內置非確定函數。




4、

存儲過程一般是作為一個獨立的部分來執行( EXECUTE 語句執行),而函數可以作為查詢語句的一個部分來調用(SELECT調用)。

由於函數可以返回一個表對象,因此它可以在查詢語句中位於FROM關鍵字的後面。 SQL語句中不可用存儲過程,而可以使用函數。

㈢ mysql存儲過程參數數據類型有什麼規范

mysql存儲過程參數數據類型有什麼規范
SQL Server存儲過程是SQL資料庫的重要組成部分,其中可以用到許多參數。在SQL Server存儲過程中,支持輸入(Input)、輸出參數(Output),也支持返回值參數(ReturnValue)。

㈣ MySQL存儲過程里怎麼定義一個參數類型和表的變數類型一樣

MySQL存儲過程中,定義變數有兩種方式:
  1、使用set或select直接賦值,變數名以@開頭,可以在一個會話(即連接)的任何地方聲明,作用域是整個會話,稱為用戶變數。例如:set @var=1;
  2、 以declare關鍵字聲明的變數,只能在存儲過程中使用,稱為存儲過程變數,主要用在存儲過程中,或者是給存儲傳參數中。例如: declare var1 int default 0;

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

㈤ mysql存儲過程double類型參數問題

你的代碼粘出來
使用BigDecimal試試看
方法網上有的,如果是碰團察加用下面方笑茄法
public static double add(double v1, double v2) {

BigDecimal b1 = new BigDecimal(Double.toString(v1));

BigDecimal b2 = new BigDecimal(Double.toString(v2));

return b1.add(b2).doubleValue();

}
其他的你照或派著寫就可以了

㈥ mysql 存儲過程

.關於MySQL的存儲過程

存儲過程是資料庫存儲的一個重要的功能,但是MySQL在5.0以前並不支持存儲過程,這使得MySQL在應用上大打折扣。好在MySQL 5.0終於開始已經支持存儲過程,這樣即可以大大提高資料庫的處理速度,同時也可以提高資料庫編程的靈活性。

MySQL存儲過程的創建

(1).格式

MySQL存儲過程創建的格式:CREATE PROCEDURE過程名([過程參數[,...]])
[特性...]過程體

這里先舉個例子:

  • mysql>DELIMITER//

  • mysql>CREATEPROCEDUREproc1(OUTsint)

  • ->BEGIN

  • ->SELECTCOUNT(*)INTOsFROMuser;

  • ->END

  • ->//

  • mysql>DELIMITER;


  • 註:
  • (1)這里需要注意的是DELIMITER //和DELIMITER ;兩句,DELIMITER是分割符的意思,因為MySQL默認以";"為分隔符,如果我們沒有聲明分割符,那麼編譯器會把存儲過程當成SQL語句進行處理,則存儲過程的編譯過程會報錯,所以要事先用DELIMITER關鍵字申明當前段分隔符,這樣MySQL才會將";"當做存儲過程中的代碼,不會執行這些代碼,用完了之後要把分隔符還原。

    (2)存儲過程根據需要可能會有輸入、輸出、輸入輸出參數,這里有一個輸出參數s,類型是int型,如果有多個參數用","分割開。

    (3)過程體的開始與結束使用BEGIN與END進行標識。

㈦ 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

㈧ 在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程序里的調用。

㈨ mysql存儲過程的基本用法有哪些

mysql存儲過程的基本用法有哪些
在外部程序訪問資料庫時(例如 PHP),要組織很多 SQL 語句。

特別是業務邏輯復雜的時候,一大堆的 SQL 和條件夾雜在 PHP 代碼中,讓人不寒而慄。現在有了 MySQL 存儲過程,業務邏輯可以封裝存儲過程中,這樣不僅容易維護,而且執行效率也高。

一、MySQL 創建存儲過程

"pr_add" 是個簡單的 MySQL 存儲過程,這個MySQL 存儲過程有兩個 int 類型的輸入參數 "a"、"b",返回這兩個參數的和。
復制代碼 代碼如下:
drop procere if exists pr_add;

計算兩個數之和
復制代碼 代碼如下:
create procere pr_add
(
a int,
b int
)
begin
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
/*
return c;
不能在 MySQL 存儲過程中使用。return 只能出現在函數中。

*/
end;

二、調用 MySQL 存儲過程
復制代碼 代碼如下:
call pr_add(10, 20);

執行 MySQL 存儲過程,存儲過程參數為 MySQL 用戶變數。
復制代碼 代碼如下:
set @a = 10;
set @b = 20;
call pr_add(@a, @b);

三、MySQL 存儲過程特點

創建 MySQL 存儲過程的簡單語法為:
復制代碼 代碼如下:
create procere 存儲過程名字()
(
[in|out|inout] 參數 datatype
)
begin
MySQL 語句;
end;

MySQL 存儲過程參數如果不顯式指定"in"、"out"、"inout",則默認為"in"。習慣上,對於是"in" 的參數,我們都不會顯式指定。

1. MySQL 存儲過程名字後面的"()"是必須的,即使沒有一個參數,也需要"()"

2. MySQL 存儲過程參數,不能在參數名稱前加"@",如:"@a int"。下面的創建存儲過程語法在 MySQL 中是錯誤的(在 SQL Server 中是正確的)。 MySQL 存儲過程中的變數,不需要在變數名字前加"@",雖然 MySQL 客戶端用戶變數要加個"@"。
復制代碼 代碼如下:
create procere pr_add
(
@a int, -- 錯誤
b int -- 正確
)

3. MySQL 存儲過程的參數不能指定默認值。

4. MySQL 存儲過程不需要在 procere body 前面加 "as"。而 SQL Server 存儲過程必須加 "as" 關鍵字。
復制代碼 代碼如下:
create procere pr_add
(
a int,
b int
)
as -- 錯誤,MySQL 不需要 "as"
begin
mysql statement ...;
end;

5. 如果 MySQL 存儲過程中包含多條 MySQL 語句,則需要 begin end 關鍵字。
復制代碼 代碼如下:
create procere pr_add
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;

6. MySQL 存儲過程中的每條語句的末尾,都要加上分號 ";"
復制代碼 代碼如下:
...
declare c int;
if a is null then
set a = 0;
end if;
...
end;

7. MySQL 存儲過程中的注釋。
復制代碼 代碼如下:
/*
這是個
多行 MySQL 注釋。
*/
declare c int; -- 這是單行 MySQL 注釋 (注意 -- 後至少要有一個空格)
if a is null then # 這也是個單行 MySQL 注釋
set a = 0;
end if;

熱點內容
Ftp打開文件是只讀模式 發布:2025-02-09 07:40:55 瀏覽:504
androidlistview點擊事件 發布:2025-02-09 07:25:52 瀏覽:171
targz解壓縮 發布:2025-02-09 06:59:19 瀏覽:311
wpsphp 發布:2025-02-09 06:58:41 瀏覽:961
視易鋒雲系統如何架設輔助伺服器 發布:2025-02-09 06:47:08 瀏覽:770
mysql備份腳本shell 發布:2025-02-09 06:46:33 瀏覽:15
騰訊雲伺服器怎樣調整解析度 發布:2025-02-09 06:46:30 瀏覽:369
php上一個頁面 發布:2025-02-09 06:41:25 瀏覽:489
改裝配置後不想重啟怎麼辦 發布:2025-02-09 06:36:40 瀏覽:446
演算法復雜度定義 發布:2025-02-09 06:30:46 瀏覽:587