SQL Scenario

Last Post 17 Mar 2008 10:03 PM by SQLUSA. 1 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
kjmcsd
New Member
New Member

--
17 Mar 2008 10:41 AM
Let's say 10 rows a day gets inserted into a table. A field in each record needs to be compared to a field in a record in another database. That part I can handle. But the tricky part for me is that if one record's field does not match, then I cannot insert all 10 into the table.

Any suggestions?

One way I thought of is to do a join between the two table and check to see if the fields don't equal one another. If a rowcount comes back greater than 0 then do not insert into table.

Is this a good way to do it?

Select * from TableA
inner join Table B on a.ID = b.ID
where a.num1 <> b.num1 and a.num2 <> b.num2 and a.num3 <> b.num3 and gdate = getdate()

If @@ROWCOUNT = 0 Then
INSERT INTO TableC
Select * from TableA
where gdate = getdate()
SQLUSA
New Member
New Member

--
17 Mar 2008 10:03 PM
if not exists (Select 1 from TableA
inner join Table B on a.ID = b.ID
where a.num1 <> b.num1 and a.num2 <> b.num2 and a.num3 <> b.num3 and gdate = getdate() )
INSERT INTO TableC
Select * from TableA
where gdate = getdate()


Kalman Toth - Database, Data Warehouse & Business Intelligence Architect
SQLUSA: http://www.sqlusa.com/order2005bitr...008031720/ SQL 2005 BI Workshop NYC - April 21-24! SSRS, SSIS, SSAS
You are not authorized to post a reply.

Acceptable Use Policy