How to pass multiple values to stored procedure parameter that running from VBA?

跟風遠走 提交于 2020-01-06 14:11:04

问题


I got situation. I have created VBA that calls SP with 2 parameters. How Can I pass 2 values to each parameter?

below code as ex, passing single value to each parameter:

cmd.Parameters.Append cmd.CreateParameter("@Class", adVarChar, adParamInput, 40)
cmd.Parameters("@Class").Value = "H"


cmd.Parameters.Append cmd.CreateParameter("@Color", adVarChar, adParamInput, 40)
cmd.Parameters("@Color").Value = "Black"

Now I want to pass "M","Yellow" also. How can I achieve this? Do I need to do any changes to SP also. Please help.

Thanks in advance


回答1:


The problem is that the stored procedure only expects one value to be passed, not a series of them. If SQL allowed an IN clause to be passed an array of values like this then you'd be in business:

select * from mytable where id in @parameters

But unfortunately it doesn't so that won't work. You could just eliminate the stored procedure and just cobble together a SQL string, which is a SQL injection risk, but if your code is only being used in-house then that won't be as big of a worry. As alluded to in the comments, if you want to use the SP as-is, then you might just have to run the query multiple times and append your results to a local list/array/recordset/whatever each time you run it. There are other ideas but they're all basically the same in that you're cobbling together a custom SQL string, which is more complex and not really using the SP parameters like they're designed.

How to pass string parameter with `IN` operator in stored procedure SQL Server 2008

SQL IN Clause In Stored Procedure



来源:https://stackoverflow.com/questions/29525727/how-to-pass-multiple-values-to-stored-procedure-parameter-that-running-from-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!