mysql dynamic query in stored procedure(存储过程中的mysql动态查询)
问题描述
我正在存储过程中创建动态查询.我的存储过程如下:
i am creating a dynamic query in stored procedure. my stored procedure is as follows:
CREATE PROCEDURE `test1`(IN tab_name VARCHAR(40),IN w_team VARCHAR(40))
BEGIN
SET @t1 =CONCAT("SELECT * FROM ",tab_name," where team=",w_team);
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END
当我尝试使用以下调用运行它时:
when i try to run it with the following call:
call test1 ('Test','SPA');
我收到以下错误消息:
错误代码:1054.where 子句"中的未知列SPA"
Error Code: 1054. Unknown column 'SPA' in 'where clause'
我在没有 where 条件的情况下进行了测试并且它工作正常,但是在 where 条件下它不起作用,我尝试使用带有变量名称的 @ 但它仍然不起作用.
i tested without where condition and it works fine, but with the where condition its not working, i tried using @ with the variable name but it still does not work.
感谢您的帮助.
推荐答案
您没有在 WHERE 子句中包含参数 w_team.
You missed to enclose the parameter w_team in WHERE clause.
试试这个:
SET @t1 =CONCAT("SELECT * FROM ",tab_name," where team='",w_team,"'");
说明:
来自您的代码的查询如下:
Query from your code would be like:
SELECT * FROM Test where team=SPA
它将尝试查找不可用的列 SPA,因此会出现错误.
It will try find a column SPA which is not available, hence the error.
我们将其更改为:
SELECT * FROM Test where team='SPA'
这篇关于存储过程中的mysql动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:存储过程中的mysql动态查询
基础教程推荐
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 在多列上分布任意行 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
