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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:14:19+00:00 2026-05-27T13:14:19+00:00

I want to execute a dynamic SQL statement, with its returned value being the

  • 0

I want to execute a dynamic SQL statement, with its returned value being the conditional for an IF statement:

IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN

This generates the error ERROR: type "execute" does not exist.

Is it possible to do this, or is it necessary to execute the SQL before the IF statement into a variable, and then check the variable as the conditional?

  • 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-27T13:14:20+00:00Added an answer on May 27, 2026 at 1:14 pm

    This construct is not possible:

    IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN ...

    You can simplify to:

    IF EXISTS (SELECT 1 FROM mytable) THEN ...
    

    But your example is probably simplified. For dynamic SQL executed with EXECUTE, read the manual here. You can check the special variable FOUND immediately after executing any DML command to see whether any rows here affected:

    IF FOUND THEN ...
    

    However:

    Note in particular that EXECUTE changes the output of GET DIAGNOSTICS, but does not change FOUND.

    Bold emphasis mine. For a plain EXECUTE do this instead:

    ...
    DECLARE
       i int;
    BEGIN
       EXECUTE 'SELECT 1 FROM mytable';  -- something dynamic here
    
       GET DIAGNOSTICS i = ROW_COUNT;
    
       IF i > 0 THEN ...
    

    Or if opportune – in particular with only single-row results – use the INTO clause with EXECUTE to get a result from the dynamic query directly. I quote the manual here:

    If a row or variable list is provided, it must exactly match the
    structure of the query’s results (when a record variable is used, it
    will configure itself to match the result structure automatically). If
    multiple rows are returned, only the first will be assigned to the
    INTO variable. If no rows are returned, NULL is assigned to the INTO
    variable(s).

    ...
    DECLARE
       _var1 int;  -- init value is NULL unless instructed otherwise
    BEGIN
    
    EXECUTE format('SELECT var1 FROM %I WHERE x=y LIMIT 1', 'my_Table')
    INTO    _var1;
    
    IF _var1 IS NOT NULL THEN ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to execute an SQL query like: select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2'; Now the
So I have a dynamic object and I want to execute a method from
I want to execute a php-script from php that will use different constants and
I want to execute a batch file from a java program. I am using
It looks like #temptables created using dynamic SQL via the EXECUTE string method have
I have a Stored Procedure which executes some dynamic SQL. I want to use
By dynamic I mean I want to be able to change the sql query
I'm using some code to execute a SQL and return a IEnumerable of dynamic
Friends, I have a dynamic sql query that I would like to execute and
I want to make a dynamic populated treeview from mysql database in PHP. I

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.