Solved

SQL Query error

Posted on 2009-05-08
10
189 Views
Last Modified: 2012-05-06
Attempting to delete rows with duplicate fields using this query:

Delete FROM TEMP_PROP t1 CROSS JOIN TEMP_PROP t2
where t1.RowID <> t2.RowID
and t1.situs_dpbc = t2.situs_dpbc

I received this error:
Line 1: Incorrect syntax near 't1'.
0
Comment
Question by:marknicks
  • 4
  • 3
  • 3
10 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 300 total points
ID: 24339134
Delete TEMP_PROP
FROM TEMP_PROP t1 CROSS JOIN TEMP_PROP t2
where t1.RowID <> t2.RowID
and t1.situs_dpbc = t2.situs_dpbc
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24339149
I presume you want this:
Delete FROM TEMP_PROP t1 
WHERE EXISTS ( SELECT NULL FROM TEMP_PROP t2 
           where t1.RowID <> t2.RowID
             and t1.situs_dpbc = t2.situs_dpbc
              )

Open in new window

0
 

Author Comment

by:marknicks
ID: 24339304
Thanks for a quick response.  Unfortunately we haven't found it yet.

The first answer gave this error:
The table 'temp_prop' is ambiguous.

The second gave this error:
Line 1: Incorrect syntax near 't1'.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 24339437
correction for mine:
Delete TEMP_PROP t1 
WHERE EXISTS ( SELECT NULL FROM TEMP_PROP t2 
           where t1.RowID <> t2.RowID
             and t1.situs_dpbc = t2.situs_dpbc
              )

Open in new window

0
 

Author Comment

by:marknicks
ID: 24339575
still getting the syntax error near t1
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 300 total points
ID: 24339690

Delete TEMP_PROP
from TEMP_PROP t1
WHERE EXISTS ( SELECT NULL FROM TEMP_PROP t2
           where t1.RowID <> t2.RowID
             and t1.situs_dpbc = t2.situs_dpbc
              )
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 200 total points
ID: 24339857
I guess that's still not correct :/
Delete t1
FROM TEMP_PROP t1 
WHERE EXISTS ( SELECT NULL FROM TEMP_PROP t2 
           where t1.RowID <> t2.RowID
             and t1.situs_dpbc = t2.situs_dpbc
              )

Open in new window

0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 300 total points
ID: 24340010
@Angel

I think thta should work, try this example

declare @tab1 table ( i int )
insert into @tab1 select 1
insert into @tab1 select 2
insert into @tab1 select 3
declare @tab2 table ( i int )
insert into @tab2 select 1
insert into @tab2 select 6


delete  @tab1
from @tab1 t1
where exists (SELECT 1 from @tab2 t2 where t1.i = t2.i )

select * from @tab1

 
0
 

Author Comment

by:marknicks
ID: 24340022
well it at least ran without errors.  but i'm still testing.
I think it's pretty close to the right solution.
0
 

Author Closing Comment

by:marknicks
ID: 31579595
This series of queries turned out to be the best approach, elminating the FoxPro routines.

"select * into #Temp_Dups from Temp_Prop
      where (rowid in (select MIN(rowid) as min_id from temp_prop
            group by mailing_zip, mailing_addr_line1
              having count(*) > 1)) AND (Session_ID = '" & strSession_ID & "')"

"Delete temp_prop from TEMP_PROP t1
      WHERE (EXISTS ( SELECT NULL FROM TEMP_PROP t2 where(t1.rowid <> t2.rowid)
        and t1.mailing_zip + t1.mailing_addr_line1 = t2.mailing_zip + t2.mailing_addr_line1))
        AND (Session_ID = '" & strSession_ID & "')"

"alter table #Temp_Dups drop column rowid"

"insert Temp_Prop select * from #Temp_Dups"

I appreciate all your comments as they pointed me in the right direction!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 57
sql server computed columns 11 33
SQL Server group with two values 4 34
Check ALL SP in database make sure there are no errors 17 43
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question