Groups | Blog | Home
all groups > sql server programming > july 2003 >

sql server programming : SQL syntax help needed


Buddy G
7/24/2003 11:12:14 PM
I want to delete rows in a table based upon when status_final is true in
another table.
Something like this (using Select to see what the query will pull up)
Running SQL Server 7

select * from startstop_dictation_log s
Join Accession a ON (s.Accession_no = a.Accession_no)
Where a.Status_Final = 'Y'

The problem is that the connecting fields of the join statement have
dissimilar data.
s.Accession_no stores the accession numbers like this: S-03-00001
a.Accession_no stores the accession numbers like this: SCS-03-00001

I tried something like:
select * from startstop_dictation_log s
Join Accession a ON (Select Case Left(s.Accession_no,1)
When 'S' Then 'SC'+ s.Accession_no
When 'H' Then 'SC'+ s.Accession_no
When 'A' Then 'SC'+ s.Accession_no
When 'N' Then 'NCG'+ Right(s.Accession_no,9) = a.Accession_no)
Where a.Status_Final = 'Y'

Syntax is not right.

I will probably put this into a stored proc. What do you guys think?

Buddy G.

Buddy G
7/25/2003 9:37:18 AM
Thanks,
Doesn't "Delete s" refer to the entire table? I wanted to only delete rows
where the accession number has been finalized.

Buddy G

[quoted text, click to view]

Jacco Schalkwijk
7/25/2003 9:55:45 AM
Hi Buddy,

Try the following (untested, may contain typos):

DELETE s
FROM startstop_dictation_log s
INNER JOIN Accession a
ON a.Accession_no =
CASE LEFT(s.Accession_no,1)
When 'S' Then 'SC'+ s.Accession_no
When 'H' Then 'SC'+ s.Accession_no
When 'A' Then 'SC'+ s.Accession_no
When 'N' Then 'NCG'+ Right(s.Accession_no,9)
END
Where a.Status_Final = 'Y'

--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.


[quoted text, click to view]

Joe Celko
7/25/2003 10:18:16 AM
[quoted text, click to view]
in another table. <<

DELETE FROM Startstop_Dictation_Log
WHERE EXISTS
(SELECT *
FROM Accession AS A1
WHERE A1.status_final = 'Y'
AND A1.accession_no =
CASE SUBSTRING(Startstop_Dictation_Log.accession_no, 1, 1)
WHEN 'S' THEN 'SC'+ Startstop_Dictation_Log.accession_no
WHEN 'H' THEN 'SC'+ Startstop_Dictation_Log.accession_no
WHEN 'A' THEN 'SC'+ Startstop_Dictation_Log.accession_no
WHEN 'N' THEN 'NCG'+ RIGHT(Startstop_Dictation_Log.accession_no,
9)
END);

But this is a really bad situation in the data model. One minute you
are claim that the accession_no data element is used in both
startstop_dictation_log and the Accessions table; fine. But then you
have a different syntax for the same value in those two locations. This
needs to be cleaned up.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Buddy G
7/25/2003 11:29:33 AM
Yes, it does need a cleanup. This is the result of blending two different
systems into one, but I can modify the StartStop table schema and some
scripts to fix this - eventually.

I tried your query based on WHERE EXISTS and also the one from Jacco Based
on the INNER JOIN and I get different results.
Also, the case WHEN 'N' THEN 'NGC'+
RIGHT(Startstop_Dictation_Log.accession_no, 9)
does not return anything. It should convert
N-03-00001 to NGC-03-00001 but it does not work.
I checked and there are matching records in the two tables.

Any Ideas?

Thanks for your reply, Buddy G

[quoted text, click to view]

Joe Celko
7/25/2003 12:19:28 PM
[quoted text, click to view]

I jujst copied what you gave. Would this do it? Try shortening the
prefixes on the longer string instead of extending the shorter string.

DELETE FROM Startstop_Dictation_Log
WHERE EXISTS
(SELECT *
FROM Accession AS A1
WHERE A1.status_final = 'Y'
AND A1.accession_no =
REPLACE(
REPLACE(Startstop_Dictation_Log.accession_no, 'SC',''),
'GC-', '-');

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Jacco Schalkwijk
7/25/2003 8:33:56 PM
Hi Buddy,

The number of rows that is deleted is limited by the join, the entire table
won't be deleted.

The difference between Joe's and my results is because Joe looks at the left
most character of Accession.Accession_no and I look at the left most
character of startstop_dictation_log.Accession_no. I think from your
narrative that my solution is correct, but if you can post scripts to create
the tables (easy to do from Enterprise Manager) and to insert sample data
(use this script here: http://vyaskn.tripod.com/code.htm#inserts) and the
results you expect we can test it out.

Jacco



[quoted text, click to view]

Buddy G
7/28/2003 3:09:02 PM
Many thanks to Jacco and Joe!
I was never able to figure out why "When 'N' Then 'NGC'+
Right(s.Accession_no,9)" did not work, but using REPLACE
(s.Accession_no,N,NGC) did work. However, I took Joe's advice and fixed the
tables and code so that both systems use the same case type prefix, making
the original query into a simple join.

Works good now.

Buddy G.

[quoted text, click to view]

AddThis Social Bookmark Button