shell 调用mysql 存储过程判断真假

时间:2024-12-04 22:04:56
mysql>  create table TBL_STUDENT(id int,name char(10),CLASSNO  int,BIRTH datetime);
Query OK, 0 rows affected (0.16 sec) mysql> create unique index TBL_STUDENT_idx1 on TBL_STUDENT(id);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0 drop PROCEDURE P_INSERT_STUDENT;
delimiter //
CREATE PROCEDURE P_INSERT_STUDENT(IN ID INT,IN NAME VARCHAR(10),IN CLASSNO INT,IN BIRTH DATETIME)
BEGIN
SET @ID = ID;
SET @NAME = NAME;
SET @CLASSNO = CLASSNO;
SET @BIRTH = BIRTH; SET @insertSql = CONCAT('INSERT INTO TBL_STUDENT VALUES(?,?,?,?)');
PREPARE stmtinsert FROM @insertSql;
EXECUTE stmtinsert USING @ID,@NAME,@CLASSNO,@BIRTH;
DEALLOCATE PREPARE stmtinsert;
END// delimiter //
CREATE PROCEDURE P_GET_CLASS_NAME(IN ID int,OUT NAME VARCHAR(50))
BEGIN
IF(ID = 1) THEN
SET NAME = '一班';
END IF;
IF(ID = 2) THEN
SET NAME = '二班';
END IF;
END// delimiter //
CREATE PROCEDURE P_INSERT_STUDENT(IN ID INT,IN NAME VARCHAR(10),IN CLASSNO INT,IN BIRTH DATETIME)
BEGIN
SET @ID = ID;
SET @NAME = NAME;
SET @CLASSNO = CLASSNO;
SET @BIRTH = BIRTH; SET @insertSql = CONCAT('INSERT INTO TBL_STUDENT VALUES(?,?,?,?)');
PREPARE stmtinsert FROM @insertSql;
EXECUTE stmtinsert USING @ID,@NAME,@CLASSNAME,@BIRTH;
DEALLOCATE PREPARE stmtinsert;
END// demo:/root# cat 1.sh
if mysql -uroot -p1234567 -e "call DEVOPS.P_INSERT_STUDENT(1,'xy',1,'2012-10-01 10:20:01')" ;
then
echo $?
else
echo $? fi demo:/root# sh ./1.sh
Warning: Using a password on the command line interface can be insecure.
0 返回0为真 返回1为假