Solved

How do i Aliases sql query in vba excel to query oracle database

Posted on 2013-06-14
6
1,073 Views
Last Modified: 2013-06-24
Hi
i'm currently using the excel vba to query data from oracle database, how can i use the  Aliases in sql query with inner join statement.

1. How do i query using aliase?
2. if i use ADODB.Connection and  ADODB.Recordset object to establish the connection with oracle database, does it means i need to use oracle syntax query to query data or i can use Ms Access sql syntax to do the query?

select workorder.custpo, shipinfo.lotno shipDetails.shipdate, from (workorder INNER JOIN shipinfo ON workorder.workOrderNo = shipinfo.lotno) inner join  shipDetails on shipDetails.shipID = shipInfo.shipID"
 

Open in new window


Thanks
0
Comment
Question by:meniyk
  • 5
6 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39247541
there is a syntax error in that sql, remove the comma just prior to the word FROM
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39247592
here are some stages that I hope help show how to apply aliases in the query
-- as is
SELECT
      workorder.custpo
    , shipinfo.lotno shipDetails.shipdate
    ,
FROM (
    workorder INNER JOIN shipinfo ON workorder.workOrderNo = shipinfo.lotno
    )
INNER JOIN shipDetails ON shipDetails.shipID = shipInfo.shipID

-- remove bad comma, fix a missing comma
SELECT
      workorder.custpo
    , shipinfo.lotno
    , shipDetails.shipdate
FROM (
    workorder INNER JOIN shipinfo ON workorder.workOrderNo = shipinfo.lotno
    )
INNER JOIN shipDetails ON shipDetails.shipID = shipInfo.shipID

-- remove unwanted parenthese, introduce aliases (wo, si, sd )
SELECT
      workorder.custpo
    , shipinfo.lotno
    , shipDetails.shipdate
FROM workorder AS wo
INNER JOIN shipinfo  AS si   ON workorder.workOrderNo = shipinfo.lotno
INNER JOIN shipDetails AS sd ON shipDetails.shipID = shipInfo.shipID


-- apply aliases (wo, si, sd ) to all other references
SELECT
      wo.custpo
    , si.lotno
    , sd.shipdate
FROM workorder AS wo
INNER JOIN shipinfo  AS si   ON wo.workOrderNo = si.lotno
INNER JOIN shipDetails AS sd ON sd.shipID = si.shipID

-- make ON conditions consistent in sequence left/right
-- prior table first in sequence
SELECT
      wo.custpo
    , si.lotno
    , sd.shipdate
FROM workorder AS wo
INNER JOIN shipinfo  AS si   ON wo.workOrderNo = si.lotno
INNER JOIN shipDetails AS sd ON si.shipID = sd.shipID -- changed here

-- finished query with aliases
SELECT wo.custpo, si.lotno, shipDetails.shipdate FROM workorder AS wo INNER JOIN shipinfo AS si ON wo.workOrderNo = si.lotno INNER JOIN shipDetails AS sd ON si.shipID = sd.shipID

Open in new window

useful website to "see" sql code in formatted fashion www.poorsql.com
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39247598
2. does it means i need to use oracle syntax query
YES, if sending sql to Oracle it must be in Oracle compatible syntax
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39247607
darn error in last line (still has shipdetail in it) the corrected finished query:
SELECT wo.custpo, si.lotno, sd.shipdate FROM workorder AS wo INNER JOIN shipinfo AS si ON wo.workOrderNo = si.lotno INNER JOIN shipDetails AS sd ON si.shipID = sd.shipID

Open in new window

0
 

Author Comment

by:meniyk
ID: 39270116
Hi PortletPaul,
Again thank you for your response.

It's does not work if the query with the alias. could it be the ADODB.Recordset object I used? Or the oracle sql query does not support alias syntax

even i have tried the simplest queries, it does not work if with alias syntax.. if without alias, it work...

sqlStr = "SELECT WO.WORKORDERNO FROM WORKORDER AS WO"  

I'm not sure is there anything to do with the add-in reference in excel VBA? it is kind of strange that the simplest database queries with alias on it does not work..


thanks
regards
YK
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39270152
I introduced aliases to shorten the string by the way.

Oracle does support aliases, but oracle doesn't like the 'as ' on table aliases, my bad, try this as a test

sqlStr = "SELECT WO.WORKORDERNO FROM WORKORDER WO"  

Some Oracle versions  however don't support ANSI join syntax, maybe that is the problem. Do you know what the Oracle version is? Anyway, this removes the ANSI join syntax:

sqlStr = "SELECT wo.custpo, si.lotno, sd.shipdate FROM workorder wo, shipinfo si, shipDetails sd WHERE wo.workOrderNo = si.lotno AND si.shipID = sd.shipID"

if you prefer not to use table aliases, then:

sqlStr = "SELECT workorder.custpo, shipinfo.lotno, shipDetails.shipdate FROM workorder, shipinfo, shipDetails WHERE workorder.workOrderNo = shipinfo.lotno AND shipinfo.shipID = shipDetails.shipID"

If none of these work then it maybe it is related to the connection method.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now