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
 If condition + subquery

Author  Topic 

yanick
Starting Member

3 Posts

Posted - 2008-07-28 : 11:47:14
Hello all,

I'm trying to retrieve a result set from a logical test.

I have 3 tables - Orders, Customers and ShipTo.

In the table Orders I store a Customer Id as well as a ShipTo Id
(for the case when a company has multiple branches and the order
is going to be shipped to a different location. When Shipto is the same
as the BillTo I insert the value 0 in the ShipTo.ShipToID and use the address
from the Customers table instead.

I'm currently able to retrieve a single record when I specify an Order ID
but I had no luck when trying to retreive the Shipto addresses for multiple orders.

Here's the code for the single order (Working):

[code}
ALTER PROCEDURE [dbo].[OrderSelect]
(
@Id Int
)
AS

IF (SELECT ShipToId FROM Orders WHERE id=@ID)=0
BEGIN
SELECT Orders.Id, Orders.PO, Orders.EntryDate, Customers.Company, Customers.Contact, Customers.Address, Customers.Address2, Customers.Suite,
Customers.City, Customers.Province, Customers.PostCode, Customers.Country, Customers.Phone, Customers.Ext, Customers.Fax, Customers.Email,
Customers.Contact AS ShiptoContact, Customers.Address AS ShiptoAddress, Customers.Address2 AS ShiptoAddress2, Customers.Suite AS ShiptoSuite,
Customers.City AS ShiptoCity, Customers.Province AS ShiptoProvince, Customers.PostCode AS ShiptoPostCode, Customers.Country AS ShiptoCountry,
Customers.Phone AS ShiptoPhone, Customers.Ext AS ShiptoExt, Customers.Fax AS ShiptoFax, Customers.Email AS ShiptoEmail
FROM Orders INNER JOIN Customers on customers.Id = Orders.CustomerId

WHERE (Orders.Id = @Id)
END
ELSE
BEGIN
SELECT Orders.Id, Orders.PO, Orders.EntryDate, Customers.Company, Customers.Contact, Customers.Address, Customers.Address2, Customers.Suite,
Customers.City, Customers.Province, Customers.PostCode, Customers.Country, Customers.Phone, Customers.Ext, Customers.Fax, Customers.Email,
ShipTo.Contact AS ShiptoContact, ShipTo.Address AS ShiptoAddress, ShipTo.Address2 AS ShiptoAddress2, ShipTo.Suite AS ShiptoSuite,
ShipTo.City AS ShiptoCity, ShipTo.Province AS ShiptoProvince, ShipTo.PostCode AS ShiptoPostCode, ShipTo.Country AS ShiptoCountry,
ShipTo.Phone AS ShiptoPhone, ShipTo.Ext AS ShiptoExt, ShipTo.Fax AS ShiptoFax, ShipTo.Email AS ShiptoEmail
FROM ShipTo INNER JOIN
Customers ON ShipTo.CustomerID = Customers.Id INNER JOIN
Orders ON Customers.Id = Orders.CustomerId
WHERE (Orders.Id = @Id);
END




And here is my poor attempt for multiple records:

ALTER PROCEDURE dbo.GetAddresses
(
@StartRange int,/*First Order */
@EndRange int /*Last*/
)
AS


IF (SELECT ShipToId FROM Orders WHERE orders.id >=1000 AND Orders.Id <=1003)=0 /*just replaced @StartRange and @EndtRange for debuging*/
BEGIN
SELECT Customers.Contact AS ShiptoContact, Customers.Address AS ShiptoAddress, Customers.Address2 AS ShiptoAddress2, Customers.Suite AS ShiptoSuite,
Customers.City AS ShiptoCity, Customers.Province AS ShiptoProvince, Customers.PostCode AS ShiptoPostCode, Customers.Country AS ShiptoCountry,
Customers.Phone AS ShiptoPhone, Customers.Ext AS ShiptoExt, Customers.Fax AS ShiptoFax, Customers.Email AS ShiptoEmail
FROM Orders INNER JOIN Customers on customers.Id = Orders.CustomerId;


END
ELSE
BEGIN
SELECT
ShipTo.Contact AS ShiptoContact, ShipTo.Address AS ShiptoAddress, ShipTo.Address2 AS ShiptoAddress2, ShipTo.Suite AS ShiptoSuite,
ShipTo.City AS ShiptoCity, ShipTo.Province AS ShiptoProvince, ShipTo.PostCode AS ShiptoPostCode, ShipTo.Country AS ShiptoCountry,
ShipTo.Phone AS ShiptoPhone, ShipTo.Ext AS ShiptoExt, ShipTo.Fax AS ShiptoFax, ShipTo.Email AS ShiptoEmail
FROM Orders INNER JOIN ShipTo ON ShipTo.CustomerId = Orders.CustomerId;
END
[/code]

Here is the error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


Any help would be greatly appreciated.

Thank you.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-07-28 : 12:05:49
Will this work?

select ...
isNull(ShipTo.Contact, Customers.Contact) AS ShiptoContact,
...
FROM Orders
INNER JOIN Customers on customers.Id = Orders.CustomerId
left outer join ShipTo on ShipTo.CustomerID = Customers.Id and ShipTo.ShipToID > 0
WHERE ...


Be One with the Optimizer
TG
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-07-28 : 12:16:49
The problem is probably with the IF statement returning more than one value. What are you trying to accomplish with it

IF(EXISTS SELECT * FROM Orders WHERE orders.id >=1000 AND Orders.Id <=1003 and shiptoid = 0) ...?

Jim
Go to Top of Page

yanick
Starting Member

3 Posts

Posted - 2008-07-28 : 12:29:34
quote:
Originally posted by TG

Will this work?

select ...
isNull(ShipTo.Contact, Customers.Contact) AS ShiptoContact,
...
FROM Orders
INNER JOIN Customers on customers.Id = Orders.CustomerId
left outer join ShipTo on ShipTo.CustomerID = Customers.Id and ShipTo.ShipToID > 0
WHERE ...


Be One with the Optimizer
TG



Hi TG,

If I understand your code sample correctly, records would be fetched only if Shipto.ShiptoID is greater then zero...However, if it is 0, I then need to retrieve from the Customers Table instead...This is where I'm having problems. When the query runs on a specific orders.id, it works flawlessly..


Thank you.
Go to Top of Page

yanick
Starting Member

3 Posts

Posted - 2008-07-28 : 12:37:28
quote:
Originally posted by jimf

The problem is probably with the IF statement returning more than one value. What are you trying to accomplish with it

IF(EXISTS SELECT * FROM Orders WHERE orders.id >=1000 AND Orders.Id <=1003 and shiptoid = 0) ...?

Jim



Hi Jim,

I need to retrieve those records to be exported to a ship system. In pseudo code it would go like this:


If no ShiptoId then select the addresses from Customers table

else select the address fromm the Shipto table. Shipto table contains the Customers.Id


Customers might have several ship to locations, So I keep the shiptoid in the orders table or 0 if Shipto is the same as billto(customers table).



Thank you.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-07-28 : 12:44:55
quote:
records would be fetched only if Shipto.ShiptoID is greater then zero
Not exactly...

The idea behind my sample code is that ISNULL works together with the LEFT OUTER JOIN so that then when the there is a ShipToID of "0" it will use the CUSTOMERS address for shipto since the SHIPTO table row will be NULL. When SHIPTOID is greater than "0" then it will use the SHIPTO table for the ShipTo values.

Be One with the Optimizer
TG
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2008-07-28 : 12:48:48
IF NOT EXISTS ( SELECT * FROM Orders WHERE orders.id >=1000 AND Orders.Id <=1003) ...
which means if SELECT * FROM Orders WHERE orders.id >=1000 AND Orders.Id <=1003 doesn't return any records at all, do something

Jim
Go to Top of Page
   

- Advertisement -