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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:23:23+00:00 2026-06-04T16:23:23+00:00

I have a dynamic sql statement that may or may not return any results.

  • 0

I have a dynamic sql statement that may or may not return any results. For simplicity sake:

--other SQL statements defining @a
DECLARE @sql varchar(128)
SELECT @sql = 'SELECT a, b FROM c WHERE a = ' + @a
EXEC(@sql)

I want the results if this statement returns any but if EXEC(@sql) does not return any results how can I go about making it return a single null instead?

I am working with MS SQLServer 2000.

EXPLANATION FOR X-ZERO

This is for use in a webapp ajax response. I am not able to alter the backend java framework which requires a resultset of some kind to be returned by the query to the webservice.

SOLUTION

In case anyone else has this question and because it wasn’t totally clear in the answers, this worked:

--other SQL statements defining @a
DECLARE @sql varchar(128)
SELECT @sql = 'SELECT a, b FROM c WHERE a = ' + @a
EXEC(@sql)
IF @@ROWCOUNT = 0 SELECT null
  • 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-04T16:23:24+00:00Added an answer on June 4, 2026 at 4:23 pm

    You can get the result set record count of the statement with the same WHERE clause, and it it is zero, then you can just return one record with null value.

    Example:

    declare @sql varchar(128)
    declare @a varchar(10);
    set @a = '''Some @a value''';
    
    declare @rowcount int;
    exec sp_executesql N'select @rowcount=count(*) FROM c WHERE a = ''Some @a value''', 
                        N'@rowcount int output', @rowcount output;
    
    if @rowcount <> 0
    begin
      select @sql = 'SELECT a, b FROM c WHERE a = ' + @a
    end
    else
    begin
      select @sql = 'select null';
    end
    
    exec(@sql)                    ;
    

    Hope this helps you!

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

Sidebar

Related Questions

I have an application that needs to return search results from a SQL Server
I've been refactoring a huge dynamic SQL statement, and think I may have hit
I have a dynamic SQL statement I've created in a stored procedure. I need
i need to create a dynamic SQL statement that selects fields based on an
I have a messy stored procedure which uses dynamic sql. I can debug it
I have some divs that have dynamic heights controlled by a 'click' function as
I have some divs that have dynamic heights controlled by a 'click' function as
I have a dynamic text file that picks content from a database according to
I have a dynamic list of items that will be used to POST information
It looks like #temptables created using dynamic SQL via the EXECUTE string method have

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.