我相信MySQL目前没有任何东西可以访问SQLSTATEMySQL存储过程中最后执行的语句.这意味着当SQLException在存储过程中引发泛型时,很难/不可能导出错误的确切性质.
有没有人有一个解决方法来导出SQLSTATEMySQL存储过程中的错误,该错误不涉及为每个可能的SQLSTATE声明一个处理程序?
例如 - 假设我试图返回一个error_status,它超出了通用的"SQLException发生在这个BEGIN....END块的某个地方",如下所示:
DELIMITER $$
CREATE PROCEDURE `myProcedure`(OUT o_error_status varchar(50))
MY_BLOCK: BEGIN
DECLARE EXIT handler for 1062 set o_error_status := "Duplicate entry in table";
DECLARE EXIT handler for 1048 set o_error_status := "Trying to populate a non-null column with null value";
-- declare handlers ad nauseum here....
DECLARE EXIT handler for sqlexception set o_error_status:= "Generic SQLException. You'll just have to figure out the SQLSTATE yourself...." ;
-- Procedure logic that …Run Code Online (Sandbox Code Playgroud) 我正在构建一个论坛软件,我试图通过特定的"个人资料"获取所有帖子,抓住所有已经"喜欢"每个帖子("喜欢")的个人资料,然后第三次获取个人资料每个帖子都喜欢每个帖子(profile_name等等).
Table 1 - posts
---------------
post_id
profile_id (original poster)
content
...
Table 2 - like
--------------
profile_id
post_id
Table 3 - profiles
------------------
profile_id
profile_name
...
Run Code Online (Sandbox Code Playgroud)
一些规则:
post_id和profile_id如果使用PHP获取数据,我会坚持将所有类似profile_ids("likers")存储到数组中的步骤.这是我到目前为止:
SELECT post.*, COUNT(`like`.profile_id) AS like_count
FROM post LEFT OUTER JOIN `like` ON post.post_id = `like`.post_id
WHERE post.profile_id = "'.$this->profile_id.'" GROUP BY post.post_id
Run Code Online (Sandbox Code Playgroud)
提前致谢.如果我发现任何错误,或者有任何错误指出,我会尝试更新.