[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

SQL Query error

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
marknicks
Asked:
marknicks
  • 4
  • 3
  • 3
5 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
marknicksAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
marknicksAuthor Commented:
still getting the syntax error near t1
0
 
Aneesh RetnakaranDatabase AdministratorCommented:

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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
Aneesh RetnakaranDatabase AdministratorCommented:
@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
 
marknicksAuthor Commented:
well it at least ran without errors.  but i'm still testing.
I think it's pretty close to the right solution.
0
 
marknicksAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now