Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8792031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:55:26+00:00 2026-06-13T22:55:26+00:00

Suppose two tables test1 and test2 on which I select values from both tables

  • 0

Suppose two tables test1 and test2 on which I select values from both tables using a join.

My requirement is this: when I join the two tables, if test1 has data in it but no corresponding data is found in test2, then the query should throw exception.

How can this be accomplished?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:55:27+00:00Added an answer on June 13, 2026 at 10:55 pm

    You need RAISEERROR

    Do left join and select where the right side is null. If the count > 0 throw the exception.

    DECLARE @count INT
    
    SELECT 
       @count = COUNT(*)
    FROM
      test1 t1
    LEFT JOIN test2 t2 
      ON t1.t1_join_col = t2.t2_join_col
    WHERE 
      t2.t2_join_col IS NULL -- find where we have test1 data but not test2 data
    
    -- Check if I need to raise an error
    IF @count <> 0 
        RAISERROR (N'<<%7.3s>>', -- Message text.
               10, -- Severity,
               1, -- State,
               N'abcde'); -- First argument supplies the string.
        -- The message text returned is: <<    abc>>.
    

    if you need to return the results where don’t match, then add the following ELSE

    ELSE
    
    SELECT 
      ... cols you want
    FROM
      test1 t1
    LEFT JOIN test2 t2 
      ON t1.t1_join_col = t2.t2_join_col
    

    You could always, put the results into a temp table and count off that, and then return off that too if you prefer to do the query only once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get values from two tables like in the following example: Suppose
Suppose you have two tables in PostgreSQL. Table A has field x , which
Suppose I have two tables, which both have user ids. I want to perform
Suppose I have a database named MyDatabase which has two tables - MyTable1 and
Suppose I have two tables that are linked (one has a foreign key to
I have two tables suppose table 1 has two columns with short names and
Suppose I have two tables, t1 and t2 which are identical in layout but
Suppose we have two tables. Post and Comment. Post has many Comments. Pretend they
Suppose there are two entities EntityA and EntityB. Two tables are defined for both
Suppose I have two SQL tables: Customers and PhoneNumbers. Suppose that Customers has the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.