尝试在存储过程中运行以下代码:
CASE
WHEN recordNotes=null AND sortNumber=null
THEN INSERT INTO tempRecordHolder (union_ID,union_Name) VALUES (recordID, recordName);
ELSE SET @blank="null";
END CASE;
" recordNotes"," sortNumber"," recordID"," recordName"是传递给存储过程的变量。我的目标是让它将INSERT语句运行到我在函数开头创建的tempRecordHolder表中。但出于某种原因,它表示"在CASE之后意外发生。"有任何想法吗?谢谢。
完整代码,请忽略评论并注释掉代码。这是所有正在研究的东西。案例在第34-41行:
CREATE DEFINER=`apf15102`@`%` PROCEDURE `sp_cu_LookUpTbls`(
IN tbl_Name varchar(50),
recordID int,
recordName varChar(500),
# REMEMBER THERE IS A DATE BETWEEN THESE TWO FIELDS IN THE TABLE STRUCTURES
recordNotes longtext,
sortNumber int,
recordAlreadyExists bit
)
BEGIN
#-------------------------------#
# Begin preliminary table setup #
#-------------------------------#
# Begin by dropping my temp table
DROP TEMPORARY TABLE IF EXISTS tempRecordHolder;
# set my session variable equal to my passed in table name
SET @tblName = tbl_Name;
# create a session variable string that is the create table for the table that was passed in
SET @tbl_Create_Command = concat("CREATE TEMPORARY TABLE tempRecordHolder LIKE " , @tblName);
# prepare the statement making it one string that can be executed then execute it
prepare stmtCreate FROM @tbl_Create_Command;
execute stmtCreate;
#-----------------------------#
# End preliminary table setup #
#-----------------------------#
# This code right here gives a "Syntax Error: Unexpected WHEN (when)" if it is un-commented
/*
CASE
WHEN recordNotes IS NULL AND sortNumber IS NULL
THEN INSERT INTO tempRecordHolder (union_ID,union_Name) VALUES (recordID, recordName);
ELSE SET @blank="null";
END CASE;
*/
IF recordAlreadyExists = FALSE
THEN
#-----------------------------------------------------------------#
# Begin procedure for if the record DOES NOT exist in our records #
#-----------------------------------------------------------------#
IF sortNumber IS NULL
THEN
INSERT INTO tempRecordHolder VALUES (recordID, recordName, curdate(), recordNotes);
END IF;
IF sortNumber IS NOT NUll
THEN
INSERT INTO tempRecordHolder VALUES (recordID, recordName, curdate(), recordNotes, sortNumber);
END IF;
#---------------------------------------------------------------#
# End procedure for if the record DOES NOT exist in our records #
#---------------------------------------------------------------#
ELSE
#-------------------------------------------------------------#
# Begin procedure for if the record DOES exist in our records #
#-------------------------------------------------------------#
IF recordAlreadyExists = TRUE
THEN
SET @elsevar = null;
END IF;
#-----------------------------------------------------------#
# END procedure for if the record DOES exist in our records #
#-----------------------------------------------------------#
END IF;
/*
#-----------------------------#
# Begin data processing setup #
#-----------------------------#
# Declare all the necessary variables for making a cursor
DECLARE finished BOOLEAN DEFAULT FALSE;
DECLARE colNameForInsert varChar(50);
DECLARE columnName CURSOR FOR
(
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME`='tempRecordHolder';
);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = TRUE;
#---------------------------#
# END data processing setup #
#---------------------------#
#--------------------------------#
# Begin main loop and processing #
#--------------------------------#
OPEN columnName;
manFunc: loop
# check to see if we are done before running the rest of the loop
IF finished = TRUE THEN
LEAVE mainFunc;
END IF;
FETCH columnName INTO colNameForInsert;
IF
END LOOP mainFunc;
CLOSE columnName;
#------------------------------#
# END main loop and processing #
#------------------------------#
*/
END
答案 0 :(得分:2)
case
表达式返回一个值。您无法使用它将值插入表中。我认为你需要IF/ELSE
。
IF recordNotes is null AND sortNumber is null
THEN INSERT INTO tempRecordHolder (union_ID,union_Name) VALUES (recordID, recordName);
ELSE SET @blank='null';
END IF;
与null
(= null或<> null)的比较也总是返回unknown。使用is null
。