简体   繁体   中英

Passing a string parameter with many commas in SQL through Excel ADO

I would like to execute stored procedure through Excel ADO. Is there any way to pass parameter with many commas?

In VBA I have:

par1="'1','2','3'"
par2="'somthing else'"

EXEC MyProcedure par1, par2

It cannot be done that way because SQL reads it as four parameters (not two):

EXEC MyProcedure '1','2','3','somthing else'

What I am doing now is to change commas in par1 to any sets of characters which will become delimiter ie @@@ and then send it as a one string:

par1="'1@@@2@@@3'"

Then I can use:

EXEC MyProcedure par1, par2

Then in SQL I regain the original delimiter with

par1=REPLACE(par1,'@@@',',')

Is there any other simpler way which allows to keep commas in par1? Something like:

EXEC MyProcedure par1='1','2','3', par2='somthing else'

Use a 2x single quotes to include them in the string... '''1'',''2'',''3'',''Something Else'''

A breakdown:

' -- Opens a string
'' -- a single quote in a string
''' -- opens a string and places a single quote inside it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM