1、建立存儲過程sql
建立存儲過程(一)數據庫
create PROCEDURE pro1() BEGIN select 'Hello World'; END; //執行存儲過程 call proc1();
建立存儲過程的基本形式spa
CREATE PROCEDURE sp_name([proc_parameter[,…]]) [characteristic …] routine_body 其中: sp_name:存儲過程名稱 proc_parameter:存儲過程參數,能夠爲in,out,inout參數,其形式以下 [in | out | inout ] param_name type characteristic參數有多個取值: LANGUAGE SQL:說明routine_body部分是由SQL語言的語句組成,這也是數據庫系統默認的語言。 [NOT] DETERMINISTIC:指明存儲過程的執行結果是不是肯定的。DETERMINISTIC表示結果是肯定的。每次執行存儲過程時,相同的輸入會獲得相同的輸出。NOT DETERMINISTIC表示結果是非肯定的,相同的輸入可能獲得不一樣的輸出。默認狀況下是非肯定的。 …
建立存儲過程(二)code
create PROCEDURE proc2() COMMENT 'This is Hello World Procedure!' BEGIN select 'Hello World'; END; //查看存儲過程 select * from information_schema.ROUTINES t where t.ROUTINE_SCHEMA = ‘your_db_name’; //結果表中的ROUTINE_COMMENT顯示了proc2的說明。
2、刪除存儲過程orm
刪除存儲過程(一)xml
drop PROCEDURE pro1; //查看存儲過程 select * from information_schema.ROUTINES t where t.ROUTINE_SCHEMA = ‘your_db_name’;
刪除存儲過程(二)blog
drop PROCEDURE if exists vehicle.proc2; //查看存儲過程 select * from information_schema.ROUTINES t where t.ROUTINE_SCHEMA = ‘your_db_name’;
3、 帶參數的存儲過程get
帶參數的存儲過程io
//功能是 查看指定名稱的存儲過程是否存在 drop PROCEDURE if EXISTS isExistsProc; create PROCEDURE isExistsProc(dbname varchar(10),procname varchar(10)) begin select count(*) from information_schema.ROUTINES t where t.ROUTINE_SCHEMA = dbname and t.ROUTINE_NAME = procname into @pro_count; select @pro_count as 'procedure count'; end; //調用存儲過程 call vehicle.isExistsProc('vehicle','proc2');
帶out參數的存儲過程table
drop PROCEDURE if EXISTS proc_add; create PROCEDURE proc_add(a int,b int,out sum int) BEGIN set sum = a + b; END; call proc_add(1,2,@sum); select @sum;
4、變量的使用
drop PROCEDURE if EXISTS proc3; create PROCEDURE proc3() BEGIN declare res int default 10; select res; END; call proc3();
5、遊標的使用
drop PROCEDURE if exists proc4; create PROCEDURE proc4() BEGIN DECLARE user_name varchar(10); DECLARE v_password varchar(10); DECLARE cur_user CURSOR for select name,password from t_user; OPEN cur_user; FETCH cur_user into user_name,v_password; # FETCH cur_user into user_name,v_password; select user_name as 'name',v_password as 'password'; CLOSE cur_user; END; call proc4();
6、一道綜合題
//寫一個存儲過程,輸入表名,若是表存在,則返回1,不然返回0.若是表存在,則經過out參數將表的總行數返回
DROP PROCEDURE IF EXISTS getRowCountOfTable; create PROCEDURE getRowCountOfTable(tablename VARCHAR(10),OUT r_count int) BEGIN declare t int default 0; select count(*) from information_schema.`TABLES` t where TABLE_SCHEMA = 'vehicle' and TABLE_NAME = tablename into t; if( t > 0) then set @s = CONCAT('select count(*) from ',tablename,' into @rcount'); PREPARE stmt from @s; set @tname = tablename; set @rcount = 0; EXECUTE stmt; DEALLOCATE PREPARE stmt; set r_count = @rcount; select 1; ELSE select 0; end if; END;
7、Jdbc調用sql
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/xml/DbConfig.xml"); BasicDataSource dataSource = (BasicDataSource)ctx.getBean("dataSource"); Connection conn = null; CallableStatement callStmt = null; try { conn = dataSource.getConnection(); callStmt = conn.prepareCall("{call getRowCountOfTable(?,?)}"); callStmt.setString(1, "t_user"); callStmt.registerOutParameter(2, Types.INTEGER); callStmt.execute(); ResultSet rs = callStmt.getResultSet(); int res = 0; if(rs.next()) res = rs.getInt(1); if(res > 0) { System.out.println("table \"t_user\" is exists!"); int rowCount = callStmt.getInt(2); System.out.println("the count of rows in the table is " + rowCount); } else { System.out.println("table \"t_user\" is not exists!"); } } catch(SQLException e) { e.printStackTrace(); }