Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Nested SubQuery Problem

Author  Topic 

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-14 : 11:00:04
I am getting this error from the following code:

Server Database Error: Invalid object name 'AI'.

SELECT AM.fcinvoice AS InvoiceNumber,
AM.finvdate AS InvoiceDate,
left(AI.fsokey, 6) AS SalesNumber,
AM.fpono AS ContractNumber,
AI.FPRODCL AS ProductType,
AI.fpartno AS SKUNumber,
AI.frev AS ProductDescription,
SL.fcustno AS CustomerID,
AM.fccompany AS CustomerName,
SL.fcusrchr1 AS CustomerClass,
(SELECT SM.fcstate
FROM dbo.shmast SM
INNER JOIN
AI
ON left(AI.fshipkey, 6) = SM.fshipno)
AS DeliveryState
FROM dbo.slcdpm SL
INNER JOIN
dbo.armast AM
ON AM.fcustno = SL.fcustno
INNER JOIN
dbo.aritem AI
ON AM.fcinvoice = AI.fcinvoice
WHERE SL.ftype = 'C' AND year(AM.finvdate) > 2007


Am I not tying the nested query to the main query properly?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 11:02:59
Do you have a table or view with name AI in your databse?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 11:05:11
got it...you missed table name inside subquery

SELECT AM.fcinvoice AS InvoiceNumber,
AM.finvdate AS InvoiceDate,
left(AI.fsokey, 6) AS SalesNumber,
AM.fpono AS ContractNumber,
AI.FPRODCL AS ProductType,
AI.fpartno AS SKUNumber,
AI.frev AS ProductDescription,
SL.fcustno AS CustomerID,
AM.fccompany AS CustomerName,
SL.fcusrchr1 AS CustomerClass,
(SELECT SM.fcstate
FROM dbo.shmast SM
INNER JOIN dbo.aritem
AI1
ON left(AI1.fshipkey, 6) = SM.fshipno)
AS DeliveryState
FROM dbo.slcdpm SL
INNER JOIN
dbo.armast AM
ON AM.fcustno = SL.fcustno
INNER JOIN
dbo.aritem AI
ON AM.fcinvoice = AI.fcinvoice
WHERE SL.ftype = 'C' AND year(AM.finvdate) > 2007


also make sure subquery returns a single row else it will error. I didnt understand the purpose of subquery thoiugh. you are not linking it in any way to main query
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-14 : 11:12:40
Let me explain.

I have to create this huge "data sheet" with all this information about invoices. Some of these fields will have to be pulled from other modules in our database and not all of them will have answers.

So, in this case I was querying our shipping tables SM as a subquery from within the invoicing main query and AI is the alias for aritem. For each record pulled from invoicing I want the corresponding shipping record pulled. The join is left(AI.fshipkey, 6) = SM.fshipno) because on those invoices generated from sales orders the first 6 characters in the AI (Aritem invoicing table) corresponds to the shipper number.

Make sense?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 11:19:12
quote:
Originally posted by DavidChel

Let me explain.

I have to create this huge "data sheet" with all this information about invoices. Some of these fields will have to be pulled from other modules in our database and not all of them will have answers.

So, in this case I was querying our shipping tables SM as a subquery from within the invoicing main query and AI is the alias for aritem. For each record pulled from invoicing I want the corresponding shipping record pulled. The join is left(AI.fshipkey, 6) = SM.fshipno) because on those invoices generated from sales orders the first 6 characters in the AI (Aritem invoicing table) corresponds to the shipper number.

Make sense?



ok. in that case why cant you join it to main query itself ?

like


SELECT AM.fcinvoice AS InvoiceNumber,
AM.finvdate AS InvoiceDate,
left(AI.fsokey, 6) AS SalesNumber,
AM.fpono AS ContractNumber,
AI.FPRODCL AS ProductType,
AI.fpartno AS SKUNumber,
AI.frev AS ProductDescription,
SL.fcustno AS CustomerID,
AM.fccompany AS CustomerName,
SL.fcusrchr1 AS CustomerClass,
SM.fcstate AS DeliveryState
FROM dbo.slcdpm SL
INNER JOIN
dbo.armast AM
ON AM.fcustno = SL.fcustno
INNER JOIN
dbo.aritem AI
ON AM.fcinvoice = AI.fcinvoice
INNER JOIN dbo.shmast SM
ON left(AI.fshipkey, 6) = SM.fshipno
WHERE SL.ftype = 'C' AND year(AM.finvdate) > 2007
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-14 : 11:24:44
Because this one query is going to attach to tons of different areas. I will literally have around 9 different joins and many of them will be left outer. I know from experience that if I try to do this without nested subqueries I will miss records.

This is why I am using nested sub queries.

Is there something wrong with the way I was trying to do it?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 11:28:45
quote:
Originally posted by DavidChel

Because this one query is going to attach to tons of different areas. I will literally have around 9 different joins and many of them will be left outer. I know from experience that if I try to do this without nested subqueries I will miss records.

This is why I am using nested sub queries.

Is there something wrong with the way I was trying to do it?


yes. you were not relating it in any way to main query.
I still didnt understand what difference it makes when you attach this to main query via a join and then join , be it inner or left, the full query to other tables.
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-11-14 : 11:39:34
So, how do I relate it to the main query? I can't do so by using the Alias from the main query?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-14 : 11:45:30
quote:
Originally posted by DavidChel

So, how do I relate it to the main query? I can't do so by using the Alias from the main query?


nope. you need it to link via one or more columns in subquery to main query columns.
Go to Top of Page
   

- Advertisement -