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 6351969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:06:48+00:00 2026-05-24T22:06:48+00:00

I have 3 queries. If the first one returns empty set, I execute the

  • 0

I have 3 queries. If the first one returns empty set, I execute the second query, and if it returns empty set too, I give to the server the last chance and do the third:

SELECT ... INTO #Query1
IF EXISTS(SELECT * FROM #Query1)
    SELECT * FROM #Query1
ELSE BEGIN
    SELECT ... INTO #Query2
IF EXISTS(SELECT * FROM #Query2)
    SELECT * FROM #Query2
ELSE BEGIN
    SELECT ...
    END
END

It worked well, but I’ve faced a new task – the query should be used as a subquery as well as other queries, within IF EXISTS([subquery]). Doing this, I see

Incorrect syntax near the keyword ‘INTO’.

How should I modify the original query to meet this new condition?

Regards,

  • 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-05-24T22:06:48+00:00Added an answer on May 24, 2026 at 10:06 pm

    Are you looking for something like this:

    create function dbo.Foo()
      returns @Result table ( ThingId Int, Source Int )
    as
    begin
      insert into @Result
        select ThingId, 1 as Source from Things1
      if @@ROWCOUNT = 0
        insert into @Result
          select ThingId, 2 as Source from Things2
      if @@ROWCOUNT = 0
        insert into @Result
          select ThingId, 3 as Source from Things3
      --...
      return
    end
    go
    select * from dbo.Foo()
    select 42 as 'The Answer' where exists ( select * from dbo.Foo() where Source > 9 )
    

    You can pass parameters into the function to use in WHERE clauses and the like. The Source column may be omitted if you don’t care to know where the data originated.

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

Sidebar

Related Questions

I have two queries. The first returns some results and the second one returns
I have a first query that returns a set of entities: var resultSet =
I have two SQL queries, where the first one is: select Activity, SUM(Amount) as
Right now I have one query that returns a list of results and displays
I have a query which returns a result set which looks like this: A
I have 2 queries. First: SELECT * FROM `table` WHERE col='xyz' LIMIT 100 //Time
Wonder if someone could give me a quick hand. I have 2 select queries
I have 14 LINQ queries to resolve in one method. None of them have
Q: I have two queries each one return a DataTable . I wanna to
If I run the following queries each one returns quickly (0.01 sec) and gives

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.