| Author |
Topic |
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 17:31:30
|
| the following query resides in a cursor. i am eliminating the cursor, and would like to perform this operation in a batch. is there a way to re-write this without the subquery and for all containers? so i want to get rid of all the variables here.-- cursor goes here-- drops value into @CurrentContainerdelete dbo.EDIShipmentStatuses where ShipmentStatusCode not in ( select ShipmentStatusCode from dbo.EDIShipmentStatusesPrep where ContainerId = @CurrentContainer and TerminalId = 7) and ContainerId = @CurrentContainer and TerminalId = 7-- end cursor |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 17:57:06
|
this is the best i've got so far:delete dbo.EDIShipmentStatusesfrom dbo.EDIShipmentStatuseswhere TerminalId = 7 and ShipmentStatusCode not in ( select SS.ShipmentStatusCode from dbo.EDIShipmentStatuses SS inner join dbo.EDIShipmentStatusesPrep SSP on SSP.ContainerId = SS.ContainerId and SS.TerminalId = SSP.TerminalId and SS.ShipmentStatusCode = SSP.ShipmentStatusCode) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 17:57:09
|
DELETE eFROM dbo.EDIShipmentStatuses eLEFT OUTER JOIN ShipmentStatusCode sON e.ShipmentStatusCode = s.ShipmentStatusCode AND e.TerminalId = s.TerminalIdWHERE e.ShipmentStatusCode IS NULL AND e.TerminalId = 7I think I've got the NULL and OUTER JOIN part correct. It's late on a Friday though and my mind isn't working great. Tara Kizer |
 |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 17:58:13
|
| heheh i'll check it out.. that's the problem i have too.. brain... slow |
 |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 18:07:33
|
| nope that didnt' quite work. my previous reply does work, so i think i'll just stick with teh sub query for now. thanks for your help |
 |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 18:10:26
|
| actually, mine doesn't work. the outer delete query won't be in synch with the sub-query. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 18:13:30
|
| If you could show us sample data for both tables and what should be deleted from those tables, we can probably more easily come up with a solution. I was just rewriting your query to how I thought it worked. A data example would make it so much clearer. I'm leaving in a few minutes though, so I may not be able to get to this until next Tuesday (we've got a holiday on Monday). Perhaps someone else will pop in and help out in the meantime though.Tara Kizer |
 |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 18:27:47
|
| ok, i'll give an analogy that hopefully helps. so i've got a table of products, a status table which has multiple statuses for each product, and a new status table that has new statuses for the products.3 tables:ProductsCurrentStatusesNewStatusesSo Product 1, might have statuses A, B, and C in the CurrentStatuses table. Now in the NewStatuses table, Product 1 has statuses A and B. I want to remove C from the CurrentStatuses table. |
 |
|
|
BaggaDonuts
Yak Posting Veteran
52 Posts |
Posted - 2007-02-16 : 18:55:51
|
well in an ugly, twisted way, this works i think... but i'm not enthused about it.delete dbo.EDIShipmentStatusesfrom dbo.EDIShipmentStatuses SS inner join dbo.EDIShipmentStatusesPrep SSP on SSP.ContainerId = SS.ContainerId and SS.TerminalId = SSP.TerminalId where SS.ShipmentStatusCode not in ( select ShipmentStatusCode from dbo.EDIShipmentStatusesPrep SSP2 where SSP2.ContainerId = SS.ContainerId and SS.TerminalId = SSP2.TerminalId ) and SS.TerminalId = 7 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-02-16 : 20:32:07
|
| DELETE cFROM CurrentStatuses cLEFT OUTER JOIN NewStatuses nON c.Status = n.StatusWHERE n.Status IS NULLBased upon your analogy, the above should work. So in my first query, I think just my WHERE clause was incorrect.To get a more accurate answer (if this isn't it), you really need to provide sample data for each table involved in your query. Show us perhaps 5 rows in each table and highlight the row(s) that should get deleted. It is much easier for us to write queries by looking at data in tables rather than reading an explanation.Tara Kizer |
 |
|
|
|