當前位置:首頁 » 存儲配置 » mysql存儲過程字元集

mysql存儲過程字元集

發布時間: 2023-05-15 11:10:34

① Mysql存儲過程中文亂碼問題

三處編碼一致,mysql數據編碼,欄位編碼
php程序編碼
mysql存儲過程編碼
這幾處的編碼都要一致才可以

查看下你的資料庫編碼,還有欄位編碼和存儲過程,php程序是否一致

② mysql 存儲過程總結(一)

1、存儲過程定義:

存儲過程是事先經過編譯並存儲在資料庫中的一段 SQL 語句的集合,調用存儲過程可以簡化應用開發 人員的很多工作,減少數據在資料庫和應用伺服器之間的傳輸,對於提高數據處理的效率是有好處的。 存儲過程思想上很簡單,就是資料庫 SQL 語言層面的代碼封裝與重用。

2、特點:

封裝,復用 : 可以把某一業務SQL封裝在存儲過程中,需要用到 的時候直接調用即可。

可以接收參數,也可以返回數據 :再存儲過程中,可以傳遞參數,也可以接收返回 值。

減少網路交互,效率提升 : 如果涉及到多條SQL,每執行一次都是一次網路傳 輸。 而如果封裝在存儲過程中,我們只需要網路交互一次可能就可以了。

3、基本語法

(1)創建:

(2)調用:

(3)查看:

(4)刪除

注意: 在命令行中,執行創建存儲過程的SQL時,需要通過關鍵字 delimiter 指定SQL語句的 結束符。

③ 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 存儲過程 UPDATE語句 欄位=傳遞過來的參數字元串參數

mysql存儲過程支持用表作為參數。
這個問題在MySQL 5.0以前非常麻煩,但是在MySQL 5.0.13版之後,由於引入了PREPARE語句,一切變得簡單了。
例子如下(已驗證):
DROP PROCEDURE IF EXISTS `newtable`;
CREATE PROCEDURE `newtable`(IN tname varchar(64))
BEGIN
SET @sqlcmd = CONCAT('CREATE TABLE ', tname, ' (id int NOT NULL AUTO_INCREMENT, name varchar(64) DEFAULT NULL, PRIMARY KEY (`id`))');
PREPARE stmt FROM @sqlcmd;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
call newtable('abc');

⑤ 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的存儲過程的書寫這么麻煩DELIMITER 這種字元有什麼作用

其實就是告訴mysql解釋器,該段命令是否已經結束了,mysql是否可以執行了。
默認情況下,delimiter是分號;。在命令行客戶端中,如果有一行命令以分號結束,
那麼回車後,mysql將會執行該命令。如輸入下面的語句
mysql> select * from test_table;
然後回車,那麼MySQL將立即執行該語句。

但有時候,不希望MySQL這么做。在為可能輸入較多的語句,且語句中包含有分號。
如試圖在命令行客戶端中輸入如下語句
mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)
mysql> RETURNS varchar(255)
mysql> BEGIN
mysql> IF ISNULL(S) THEN
mysql> RETURN '';
mysql> ELSEIF N<15 THEN
mysql> RETURN LEFT(S, N);
mysql> ELSE
mysql> IF CHAR_LENGTH(S) <=N THEN
mysql> RETURN S;
mysql> ELSE
mysql> RETURN CONCAT(LEFT(S, N-10), '...', RIGHT(S, 5));
mysql> END IF;
mysql> END IF;
mysql> END;
默認情況下,不可能等到用戶把這些語句全部輸入完之後,再執行整段語句。
因為mysql一遇到分號,它就要自動執行。
即,在語句RETURN '';時,mysql解釋器就要執行了。
這種情況下,就需要事先把delimiter換成其它符號,如//或$$。
mysql> delimiter //
mysql> CREATE FUNCTION `SHORTEN`(S VARCHAR(255), N INT)
mysql> RETURNS varchar(255)
mysql> BEGIN
mysql> IF ISNULL(S) THEN
mysql> RETURN '';
mysql> ELSEIF N<15 THEN
mysql> RETURN LEFT(S, N);
mysql> ELSE
mysql> IF CHAR_LENGTH(S) <=N THEN
mysql> RETURN S;
mysql> ELSE
mysql> RETURN CONCAT(LEFT(S, N-10), '...', RIGHT(S, 5));
mysql> END IF;
mysql> END IF;
mysql> END;//
這樣只有當//出現之後,mysql解釋器才會執行這段語句

⑦ 為什麼MYSQL的儲存語句出現這樣的錯誤

基本判定為字元集的問題

通過以下代碼可以復現:

dropprocereifexistsdelete_employees;
delimiter$$
--bysleest,2019/05/23此處為復現問題,特意指定入參字元集為mysql默認字元集latin1
createproceredelete_employees(inxmchar(6)CHARSETlatin1COLLATElatin1_bin)
begin
--begin測試用
;
createtemporarytableemployees(`姓名`char(6));
--end測試用
deletefromemployeeswhere姓名=xm;
end$$

calldelete_employees('李麗');

在終端或者連接工具中執行會得到一致的錯誤

calldelete_employees('李麗');	ErrorCode:1366.Incorrectstringvalue:'xE6x9Dx8ExE4xB8xBD'forcolumn'xm'atrow1

這里建議在建立完存儲過程後, 查下存儲過程的字元集:

showcreateproceredelete_employees;

主要看結果中的這三列

#character_set_client,collation_connection,DatabaseCollation
'utf8','utf8_general_ci','utf8mb4_general_ci'

本人使用的環境是utf8mb4字元集, 你看看你的會不會是latin1, 如果是的話兩種方案

  1. 改資料庫字元集, 通用些utf8或者支持emjio的utf8mb4, 重啟mysql服務再重連試驗

  2. 建立的每個存儲過程手動指定字元集

    如:

createproceredelete_employees(inxmchar(6)CHARSETutf8COLLATEutf8_bin)...

這樣再試試

⑧ mysql中存儲過程是什麼意思

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

熱點內容
抽獎源碼帶後台 發布:2025-02-08 20:33:54 瀏覽:225
歐博中央空調原始密碼是多少 發布:2025-02-08 20:33:47 瀏覽:335
運動使人快樂緩解壓力 發布:2025-02-08 20:27:01 瀏覽:98
linux命令大文件 發布:2025-02-08 20:25:06 瀏覽:897
C蟻群演算法 發布:2025-02-08 20:21:25 瀏覽:513
私人搭建伺服器能幹嘛 發布:2025-02-08 20:21:24 瀏覽:596
網吧怎麼通過伺服器玩網路游戲 發布:2025-02-08 19:59:52 瀏覽:914
文檔編輯加密 發布:2025-02-08 19:56:31 瀏覽:392
phpmysql存儲過程實例 發布:2025-02-08 19:54:40 瀏覽:161
淘寶賣的地下城腳本 發布:2025-02-08 19:41:40 瀏覽:63