我有一个构造如下的SQL查询:
tmpQUERY = "INSERT INTO vertix_users (user_name, user_email, user_pass) " +
"VALUES (" + mysql.escape(userName) +
", " + mysql.escape(userEmail) +
", " + mysql.escape(userPass) + ")";
tmpQUERY = "MERGE vertix_users WITH (HOLDLOCK) AS oldData " +
"USING (VALUES (@user_name, @user_email)) AS newData (" + mysql.escape(userName) + ", " + mysql.escape(userEmail) + ") " +
"ON newData.user_name = oldData.user_name AND newData.user_email = oldData.user_email " +
"WHEN NOT MATCHED BY TARGET THEN " + tmpQUERY;
当我执行代码时,我得到以下日志:
MERGE vertix_users WITH (HOLDLOCK) AS oldData USING (VALUES (@user_name, @user_email)) AS newData ('asfasfsaff vvv', 'asfasfasfasf') ON newData.user_name = oldData.user_name AND newData.user_email = oldData.user_email WHEN NOT MATCHED BY TARGET THEN INSERT INTO vertix_users (user_name, user_email, user_pass) VALUES ('asfasfsaff vvv', 'asfasfasfasf', 'saqqq')
一个sql语法错误,内容如下:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MERGE vertix_users WITH (HOLDLOCK) AS oldData USING (VALUES (@user_name, @user_e' at line 1]
没有合并的简单插入语句可以正常工作,但只要我添加该代码的第二部分,就会产生该错误。