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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:59:30+00:00 2026-06-10T20:59:30+00:00

Suppose I have an SQL file commands.sql full of SQL commands. The intention is

  • 0

Suppose I have an SQL file commands.sql full of SQL commands. The intention is that the user may run the commands in the file by using the SOURCE command as described here. But it is assumed that the user has selected a database. If the user has not selected a database, then they will see a large number of “No database selected” errors.

Is there a way to use the IF statement to check whether a database is selected, as in

IF a database is selected THEN
    (statements)
ELSE
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You need to select a database';
END IF;
  • 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-10T20:59:31+00:00Added an answer on June 10, 2026 at 8:59 pm

    Since scripts abort upon the first error that they encounter (unless invoked with the force option), one can achieve a certain degree of flow-control using prepared statements:

    SET @sql := IF(
      DATABASE() IS NULL,
      "SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'You need to select a database'",
      "DO SELECT NULL"
    );
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    (However, I don’t think the SIGNAL command can be executed via a prepared statement).

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

Sidebar

Related Questions

Suppose I have a SQL Query that isn't really nice. The SQL Query has
Long story short, I have a SQL file that I want to import as
I have a sql file that I want to be dumped into a MySQL
I have a .sql file, which is a bunch of oracle pl/sql commands and
Suppose I have a website that processes files uploaded by the user (e.g. storing
Suppose I have a PL/SQL stored procedure as follows: PROCEDURE do_something(foo VARCHAR2 DEFAULT NULL)
Suppose you have a table in SQL: Prices ------ 13.99 14.00 52.00 52.00 52.00
I am having trouble mixing c# functions with conditions in Linq-To-SQL suppose i have
I have an sql statement that is supposed to return 2 rows. the first
Suppose I have a pure virtual method in the base interface that returns to

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.