Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert with multiple conditions

Status
Not open for further replies.

HomerJS

Programmer
Jun 25, 2001
86
0
0
US
I have three tables (Order, City, Company) which contain the fields:

Order
OrderNo
ShipperName
ShipperCity
ShipperState

City
ID = numeric field
Name = actual name of the city

Company
Name = Order.ShipperName
Code = left(Order.ShipperName) + left(City.Name,3)
City = City.ID


I am trying to add new Shippers from the Order table into the Company table. However, some shippers already exist in the Company table.

Also, the Order.ShipperCity field can contain either an actual city, or a numeric value. I want to only select those records that contain a numeric value.

If I need to clarify more let me know and I'll do my best.
 
How does this work for you:

Code:
INSERT INTO Company (Name, Code, City)
SELECT o.ShipperName AS Name
  , Left(o.ShipperName,3) + Left(c.Name,3) AS Code
  , c.ID
FROM Order o
   INNER JOIN City c ON c.ID = o.ShipperCity
WHERE IsNumeric(o.ShipperCity) = 1
AND NOT EXISTS (SELECT 1 FROM Company
   WHERE Code = Left(o.ShipperName,3) + Left(c.Name,3))



--Angel [rainbow]
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Thanks Angel!! I was pretty close but didn't quite have it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top