count matched in 2 table

Shambhu Rai 1,411 Reputation points
2023-10-14T09:02:34.7933333+00:00

Hi Expert ,

i wanted to match the count of 2 table if it is not match then it should replied 'not matched'

Create table table1_new(col varchar (20),col1 varchar (20))

Create table table2_new(col varchar(20),col1 varchar (20))

Insert into table2_new values (10,60)

Insert into table2_new values (20) Insert into table2_new values ('test','test3')

Insert into table2_new values ('test1')

select Count(*) from table1_new

select Count(*) from table2_new

expected output: 1 or ' not matched' like in if condition

Azure SQL Database
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 129.1K Reputation points MVP Volunteer Moderator
    2023-10-14T09:53:51.5966667+00:00

    Had you tested your script before you posted, you would have found that it had errors.

    It would also have helped if you had given a background to what you want to achieve. But from the terse description you have given, this meets your requirement:

    IF EXISTS (SELECT * FROM table1_new
               EXCEPT
               SELECT * FROM table2_new)
       PRINT 'Not matched'
    ELSE 
       PRINT 'Matched'
    
    

    By the way, I deleted two tags from your question that were not relevant to the question.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.