Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

compare count(*) of two tables and generate a command if the count(*) is different

Posted on 2014-09-03
10
90 Views
Last Modified: 2014-09-03
Hello,

I search how I can compare count(*) of two tables and generate insert command if the count(*) is different.

Thanks
0
Comment
Question by:bibi92
10 Comments
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40300925
A IF statement will do that:
IF (select COUNT(1) from Table1) <> (select COUNT(1) from Table2)
	INSERT INTO ....
ELSE
	PRINT 'Equal number of records'

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40300926
Without knowing specifics about how your tables are structures, this would be the general idea.
IF (select count(*) from <target table>) <> (select count(*) from <source table>)
insert into <target table>
(<field list>)
select <field list>
from <source table>
where not exists (select 1 from <target table> where <target table>.<PK column> = <source table>.<PK column>)

Open in new window

0
 

Author Comment

by:bibi92
ID: 40300942
Ok, thanks is it possible to replace ELSE PRINT 'Equal number of records' by go to the next step of a job.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40300956
Sure. I just put there something so you could see that you can add a different operation in case of the values being equal.
0
 

Author Comment

by:bibi92
ID: 40300983
I have tested :
IF (select COUNT(1) from Table1) <> (select COUNT(1) from Table2)
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40300995
Hi,

Try this

IF ((select COUNT(1) from Table1) <> (select COUNT(1) from Table2))
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40301006
Can you post all code?
0
 

Author Comment

by:bibi92
ID: 40301029
IF ((select COUNT(1) from LL.NX_TEST) <> (select COUNT(1) from FT_TEST..LL.NX_TEST))

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '<'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
0
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40301049
Only that?
What about the INSERT statement?
0
 

Author Comment

by:bibi92
ID: 40301066
Insert statement contains 300 columns. I have added       BEGIN TRAN
GO
INSERT INTO and error syntax is resolved.

Regards
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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