Correct Syntax - VBA to Run SQL in DB2

Dathan Camacho

New Member
Joined
Mar 30, 2010
Messages
37
I'm using an Excel macro to run several SQL queries. Some are Teradata queries, and one query is a DB2 query.
To synch up all the queries to return comparable data, I’m using a column in a worksheet to define parameters for the SQL where statements. This lets me control the inputs in one place.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
This simple code, where Inputs is an Excel worksheet, associates the worksheet values with a variable I'm calling SQL_Where_Parameter:
Code:
[FONT=Calibri][COLOR=black]With Inputs[/COLOR][/FONT]
 
[FONT=Calibri][COLOR=black]LastRow = .Cells(Rows.Count, "A").End(xlUp).Row<o:p></o:p>[/COLOR][/FONT]
 
[FONT=Calibri][COLOR=black]SQL_Where_Parameter = "'" & Join(Application.Transpose(Sheets("Inputs").Range("A" & FIRST_DATA_ROW & ":A" & LastRow).Value), "','") & "'"<o:p></o:p>[/COLOR][/FONT]
[FONT=Calibri][COLOR=black]<o:p></o:p>[/COLOR][/FONT]
[COLOR=black][FONT=Calibri]End With[/FONT][/COLOR]
<o:p></o:p>
<o:p></o:p>
:) Here’s the part I need help with:<o:p></o:p>
<o:p></o:p>
The queries that hit Teradata are working just fine using this method to call the worksheet inputs:<o:p></o:p>
Code:
[FONT=Calibri][COLOR=black]select… [/COLOR][/FONT]
[FONT=Calibri][COLOR=black]from…  <o:p></o:p>[/COLOR][/FONT]
[FONT=Calibri][COLOR=black]where  t.table_column in (" & SQL_Where_Parameter & ") <o:p></o:p>[/COLOR][/FONT]
[COLOR=black][FONT=Calibri]group by…[/FONT][/COLOR]
<o:p></o:p>
<o:p></o:p>
I used the same syntax in the DB2 query, but it doesn’t work. Here is the error returned from DB2:<o:p></o:p>

Run-time error '-2147467259 (80004005)':
[IBM][CLI Driver][DB2] SQL0401N The data types of the operands for the operation "" are not compatible. SQLSTATE=42818
Does anyone know the correct syntax? I haven’t been able to solve it through trial and error.

Any insight appreciated, thanks! :biggrin:
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi

I would check the "SQL_Where Parameter" definition as it seems that you're missing an & and quote after LastRow.

hth

Mike
 
Upvote 0
Dathan

But is is a numeric value that is recognised/valid for use with DB2?

Sometimes the best way to check these things is to try running the query in the database.

Even if it still errors you might get a more descriptive message - might even tell you what operands you need to use.

One thing I can think of is that you generally only need/use single quotes for text, not for numbers.
 
Upvote 0
Hi

Yes, but you are not inserting it as a parameter correctly!

What I'm saying is that you have &'s before and after FIRST_DATA_ROW and I believe it should be repeated for LastRow but at the moment you only have an & before LastRow.

Apologies if I'm on the wrong track.

hth

Mike
 
Upvote 0
I think I understand what you're saying.

It's possible that Teradata is more forgiving when there's an error, so it's running the code despite the error.

But then the same code is used in DB2, which perhaps expects more perfect input, so DB2 is failing.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top