mysql儲存過程 實現乙個表複製另乙個表的字段

2021-09-02 12:52:50 字數 2619 閱讀 6479

1.儲存過程

功能:登入驗證

邏輯:(1)引數說明:

v_name:使用者登入名

v_psd:使用者密碼

v_out:使用者不存在或使用者密碼不正確的是否v_out=1,否則v_out=9

(2)步驟:

a.從使用者表user查詢使用者名為v_name的使用者是否存在

存在則進入下一步,否則v_out=1

b.從使用者表user

查詢使用者名為v_name的使用者的密碼pass

判斷使用者密碼和v_psd是否相等,相等時v_out=9否則v_out=1

**

delimiter $$

create procedure proc_login

(in v_name varchar(16),

in v_psd varchar(8),

out v_out int)

begin

declare v_pass varchar(8) ;

declare v_num int ;

select count(*) into v_num from user where name=v_name;

if v_num<>0 then

select pass into v_pass from user where name=v_name;

if v_pass=v_psd then

set v_out=9;

else

set v_out=1;

end if;

else

set v_out=1;

end if;

end$$

delimiter ;

2.複製第乙個表的字段到第二個表上

功能:複製字段

邏輯:1.用1條sql語句查詢出所有不同的字段的資訊,並存入游標中

select * from information_schema.columns where table_name = 'sp_usercoupon1' and table_schema = 'cpdb_dev'  and column_name not in

(select column_name from information_schema.columns where table_name = 'sp_usercoupon' and table_schema = 'cpdb_dev');

2.遍歷游標,游標的每行分別是rowtype型別的變數。rowtype.欄位名方式獲取字段資料

3.獲取cloumn_name,data_type,character_maximum_length拼接alter table語句。

4.執行一次

alter table語句。

難點:mysql沒有rowtype這樣代表正行的資料型別。

delimiter $$

create procedure print

(out a varchar(50),

out b varchar(50),

out c varchar(50))

begin

declare _kubauserid1 varchar(50);

declare _kubauserid2 varchar(50);

declare _kubauserid3 varchar(50);

declare flag int;

declare update_cursor cursor

for

select column_name,data_type,character_maximum_length from information_schema.columns where table_name = 'sp_usercoupon1' and table_schema = 'cpdb_dev' and column_name not in

(select column_name from information_schema.columns where table_name = 'sp_usercoupon' and table_schema = 'cpdb_dev');

declare continue handler for not found set flag=1;

set flag=0;

open update_cursor;

repeat /*迴圈*/

fetch update_cursor into _kubauserid1,_kubauserid2,_kubauserid3;

set a=_kubauserid1;

set b=_kubauserid2;

set b=_kubauserid3;

select a,b,c;

/*update set where*/

until flag

end repeat;

close update_cursor ;

end$$

delimiter ;

call print(@a,@b,@c);

select @a,@b,@c;

總結:沒有解決問題,只是把不同的字段資訊獲取了,用游標遍歷的是指單個字段,但是程式能執行並且游標能執行沒問題。

MySql建立乙個儲存過程

mysql 儲存過程是從 mysql 5.0 新功能。儲存過程的長處有一籮筐。只是最基本的還是執行效率和sql 封裝。特別是 sql 封裝功能,假設沒有儲存過程,在外部程式訪問資料庫時 比如 php 要組織非常多 sql 語句。特別是業務邏輯複雜的時候,一大堆的 sql 和條件夾雜在 php 中,讓...

乙個儲存過程

create or replace package abc zys is procedure daily census end abc zys 建乙個包,包中有儲存過程daily census。不涉及任何引數。create or replace package body abc zys is pro...

乙個儲存過程

首先是建立儲存過程.drop procedure if exists externalcalltocomplete delimiter create procedure externalcalltocomplete begin drop table if exists temp1 create ta...