How to pass multiple values to stored procedure pa

2019-09-07 14:53发布

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条回答
聊天终结者
2楼-- · 2019-09-07 15:20

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

查看更多
登录 后发表回答