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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Trying to avoid a cursor here....help.

Author  Topic 

label
Posting Yak Master

197 Posts

Posted - 2004-04-08 : 12:23:58
I'm trying not to use a cursor to accomplish what I want but I'm not seeing any other way.

I have one table (Stock) that has a many to 1 relationship with the VehicleInventory table.

So you could have 1 stockID in the Stock table that matches with 30 different records in the VehicleInventory table. I want to return a result that tells me if a value exists in the InventoryCode field that is equal to "keys".

If there's a value of "keys" that correlates with the StockID I'm grabbing, then I want to return a string 'yes', if no value of 'keys' exists for that stock, I want to return 'no'.

Below was my first attempt at doing it but it never returned any "no"'s which is wrong.

Any thoughts?


SELECT
Stock.StockID, Keys=
Case VehicleInventory.InventoryCode
when null then 'NO'
else 'Yes'
end
ContactDetail.LastName as 'Adjuster',
AssignorStock.ClaimNumber as 'Claim #',
Vehicle.VIN,
Vehicle.Year, Vehicle.Make, Vehicle.Model,
convert(varchar(10), Sale.SaleDate, 101) as SaleDate
FROM
Aims.dbo.Stock Stock INNER JOIN Aims.dbo.Vehicle Vehicle ON
Stock.StockID = Vehicle.StockID
INNER JOIN AIMS.dbo.StockSale StockSale ON
Stock.StockID = StockSale.StockID
INNER JOIN Aims.dbo.AssignorStock AssignorStock ON
Stock.AssignmentID = AssignorStock.AssignmentID
INNER JOIN AIMS.dbo.Sale Sale ON
StockSale.SaleID = Sale.SaleID
INNER JOIN Aims.dbo.Assignor Assignor ON
AssignorStock.AssignorID = Assignor.AssignorID
INNER JOIN AIMS.dbo.Agent Agent ON
AssignorStock.AgentID = Agent.AgentID
left outer JOIN Aims.dbo.VehicleInventory VehicleInventory ON
Vehicle.VehicleID = VehicleInventory.VehicleID
INNER JOIN AIMS.dbo.ContactDetail ContactDetail ON
Agent.ContactID = ContactDetail.ContactID
INNER JOIN Aims.dbo.Company Company ON
Assignor.CompanyID = Company.CompanyID
Where
VehicleInventory.InventoryCode='KEYS'
and
convert(varchar(8), AssignorStock.AssignorID) like
@AssignorID
and
convert(varchar(8), AssignorStock.AgentID) like @AdjusterID
and
convert(varchar(8), company.CompanyID) like @CompanyID
ORDER BY
Vehicle.StockID ASC

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-08 : 12:29:02
DDL and DML are needed in order to help. DDL would be CREATE TABLE statements for all tables involved. DML would be INSERT INTO statements for sample data. We would also need to know what the expected result set should look like using the sample data.

Tara
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2004-04-08 : 12:39:59
quote:
Originally posted by tduggan

DDL and DML are needed in order to help. DDL would be CREATE TABLE statements for all tables involved. DML would be INSERT INTO statements for sample data. We would also need to know what the expected result set should look like using the sample data.

Tara



On all the tables or just the two relevant ones?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-08 : 12:42:39
Just the relevant ones. Also, the data does not have to be real data. We just need some data to work with on our own machines that represents your problem. With all of this information, more people will work on your problem as they can just copy and paste the information into their Query Analyzer and work with it.

Tara
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-04-08 : 12:50:40
It's never going to return any "NO"s with an INNER JOIN...

The statement will have to elvaluate everything to be basically "TRUE"

If you did a LEFT JOIN then perhaps you would get "No"



Brett

8-)
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2004-04-08 : 12:53:34
quote:
Originally posted by X002548

It's never going to return any "NO"s with an INNER JOIN...

The statement will have to elvaluate everything to be basically "TRUE"

If you did a LEFT JOIN then perhaps you would get "No"




On which table? I did do a left join on the vehicle table

     left outer JOIN Aims.dbo.VehicleInventory VehicleInventory ON
Vehicle.VehicleID = VehicleInventory.VehicleID


Are you saying all the tables need left outer joins instead of just the one being an outer?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-04-08 : 13:19:51
My bad...missed it....

Simplify the join to just between the two tables to see what you have....my guess is that the rest of the predicates are limiting your results...



Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-04-08 : 13:25:24
What does



SELECT Stock.StockID
, CASE WHEN vi.InventoryCode IS NULL THEN 'No'
ELSE 'Yes'
END
FROM Vehicle v
LEFT JOIN VehicleInventory vi
ON v.VehicleID = vi.VehicleIDCompanyID
WHERE vi.InventoryCode='KEYS'




give you?



Brett

8-)
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2004-04-09 : 10:39:03
quote:
Originally posted by X002548

What does



SELECT Stock.StockID
, CASE WHEN vi.InventoryCode IS NULL THEN 'No'
ELSE 'Yes'
END
FROM Vehicle v
LEFT JOIN VehicleInventory vi
ON v.VehicleID = vi.VehicleIDCompanyID
WHERE vi.InventoryCode='KEYS'








Sorry I haven't gotten back to this yet. I was pulled onto another project and haven't had a chance to get back to it yet. Hopefully this afternoon.
Go to Top of Page
   

- Advertisement -