数据库
首页 > 数据库> > MySQL存储过程中的参数

MySQL存储过程中的参数

作者:互联网

我想用输入参数创建一个MySQL存储过程(SP).

但是,在编写SP时无法确定参数的数量.
(方案是用户将有多个选项可供选择.所选的选项将构成搜索条件:

select ... 
where prod_category = option1 && option2 && option3 &&...

因此,如果某人仅选择option1和option2,则仅发送2个参数.有时可能选择了50个选项,因此必须发送50个参数.)

因此,我有3个问题:
1.我可以使用MySQL存储过程(SP)处理这种情况吗?
2. SP是处理这种情况的专业方法吗?
3.如果SP不是处理这些情况的专业方法,那么还有其他方法可以有效地处理这些搜索吗?搜索是我的应用程序的核心功能.

在此先感谢您的帮助!

解决方法:

MySQL存储过程仅接受固定数量的参数.您可以构建由单个字符串参数定界的参数和值的列表,然后在过程中对其进行处理,或者使用应用程序语言来构建查询.

http://forums.mysql.com/read.php?98,154749,155001#msg-155001

No, MySQL sprocs accept only a fixed number of arguments. ISO SQL is
somewhat optimised for correct RDBMS logic (unless you were to ask EF
Codd, CJ Date or Fabian Pascal), but in many ways SQL is quite
literal, so if SQL seems to make what you are trying to do very
difficult, it could be that your design needs another look, in this
case aspects of the design that require repeated multiple ad hoc
deletions.

If such deletions are unavoidable, here are three options. First, in
an application language build the delete query to include
comma-delimited string of IDs. Second, pass such a string to an sproc
that PREPAREs the query using such a string. Third, populate a temp
table with the target IDs and write a simple join query that deletes
the joined IDs.

标签:mysql,stored-procedures
来源: https://codeday.me/bug/20191127/2075677.html