2

I have the following SQL to be queried. It is a valid SQL. Unfortunately, it is too long for a string in VBA. Anyone knows of a workaround to run this query?

SQL = "SELECT A.cust_ky, A.incid_id, A.OPEN_TS, A.CLOSE_TS, A.REC_UPD_TS, B.wrkgp_id, A.CURR_AGNT_KY, A.incid_ttl_dn " _
    & "FROM (MAINTBLS.INCID_FAB A INNER JOIN MAINTBLS.DEPTMNT B ON A.curr_wrkgp_ky=B.wrkgp_ky) " _
    & "WHERE B.wrkgp_id='" & wrkgp & "' And (A.open_fg = 1 OR A.pend_fg = 1)" _
    & "ORDER BY A.cust_ky, A.curr_agnt_ky ASC"

rs.Open SQL, con, adOpenKeyset
8
  • Please don't prefix your titles with "VBA - " and such. That's what the tags are for. Mar 19, 2012 at 2:26
  • 1
    A string in VBA can be more than 2 billion characters. Is your string really longer than that?
    – Guffa
    Mar 19, 2012 at 2:51
  • This is the value on the watchlist after executing SQL = . "SELECT A.cust_ky, A.incid_id, A.OPEN_TS, A.CLOSE_TS, A.REC_UPD_TS, B.wrkgp_id, A.CURR_AGNT_KY, A.incid_ttl_dn FROM (MAINTBLS.WF_INCID_F A INNER JOIN MAINTBLS.DEPTMNT B ON A.curr_wrkgp_ky=B.wrkgp_ky) WHERE B.wrkgp_id='POWEBSTE-MTE-PORTAL' And (A.ope
    – Kyle
    Mar 19, 2012 at 2:53
  • 3
    FYI, your SQL string is not actually being truncated, it just apears to be because the Value field of the watch window is limited to 255 characters. To verify your string add Debug.Print SQL to your code and view the result in the Immediate window. FWIW try adding an ASC to each field in the ORDER BY clause - ie ORDER BY A.cust_ky ASC, A.curr_agnt_ky ASC Mar 19, 2012 at 3:50
  • 4
    You are missing a space before ORDER BY " OR A.pend_fg = 1)" _ & "ORDER BY A.cust_ky, A.curr_agnt_ky ASC", if this is a cut and paste of your SQL, it will not run in VBA.
    – Fionnuala
    Mar 19, 2012 at 10:12

4 Answers 4

5

Since you use Oracle, you should use a bind variable instead of dynamic SQL and then set the value in the parameter collection of the command object. Not only will it prevent SQL Injection, but it will better optimize your query.

Also, it looks like your SQL Statement is missing a space before the order by clause. That could easily cause your error. See below - untested, but should give you the idea.

SQL = "SELECT A.cust_ky, A.incid_id, A.OPEN_TS, A.CLOSE_TS, A.REC_UPD_TS, B.wrkgp_id, A.CURR_AGNT_KY, A.incid_ttl_dn " _
    & "FROM (MAINTBLS.INCID_FAB A INNER JOIN MAINTBLS.DEPTMNT B ON A.curr_wrkgp_ky=B.wrkgp_ky) " _
    & "WHERE B.wrkgp_id= :wrkgp And (A.open_fg = 1 OR A.pend_fg = 1) " _
    & "ORDER BY A.cust_ky, A.curr_agnt_ky ASC"

   With cmd
     .ActiveConnection = conn
     .CommandText = SQL
     .CommandType = adCmdText
     .Parameters.Append .CreateParameter(, adVarChar, adParamInput, wrkgp)
   End With
2

Create a view for the query, something like this

create view fix_for_broken_vba as
SELECT A.cust_ky, A.incid_id, A.OPEN_TS, A.CLOSE_TS, A.REC_UPD_TS, B.wrkgp_id, 
        A.CURR_AGNT_KY, A.incid_ttl_dn FROM (MAINTBLS.INCID_FAB A INNER JOIN MAINTBLS.DEPTMNT B ON A.curr_wrkgp_ky=B.wrkgp_ky)
    WHERE (A.open_fg = 1 OR A.pend_fg = 1)

and then rewrite the query accordingly.

1
  • Don't have enough privileges for Create View :( But Thanks tho. I'm know that would work
    – Kyle
    Mar 19, 2012 at 4:17
0

If you are using VBA in an Excel sheet, why don't you consider putting the SQL query in a protected cell of your document? You could even put it in an hidden sheet of your document.

Something like:

Cells(6, 2).Select
sqlString = Cells(6, 2).Value

I did it and it works like a charm in my case.

0

I've used the following steps for a 100+ line query

Use a long Query with Excel VBA

  1. Initially create the query in SSMS
  2. Create a sheet called SQL Query
  3. Copy and paste the query in to the SQL Query sheet and delete all rows that are blank or commented row
  4. Incorporate any variables in to the declare statement
  5. Use the following macro to loop through the SQL Query sheet

    LastRowQ = ThisWorkbook.Sheets("SQL Query").Cells(ThisWorkbook.Sheets("SQL Query").Rows.Count, "B").End(xlUp).Row
    
    Count = 2
    Do Until Count > LastRowQ
    
        SQLQTemp = ThisWorkbook.Sheets("SQL Query").Range("B" & Count)
        SQLQuery = SQLQuery & " " & SQLQTemp
    
        Count = Count + 1
    
    Loop
    
    SQLDatabaseRS.Open SQLQuery
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.