How to echo print statements while executing a sql script

前端 未结 6 1781
执笔经年
执笔经年 2020-12-09 00:58

We have a simple sql script which needs to be executed against a MySQL database and we would like print log statements on the progress of the script (e.g. Inserted 10 record

相关标签:
6条回答
  • 2020-12-09 01:30

    What about using mysql -v to put mysql client in verbose mode ?

    0 讨论(0)
  • 2020-12-09 01:30

    For mysql you can add \p to the commands to have them print out while they run in the script:

    SELECT COUNT(*) FROM `mysql`.`user`
    \p;
    

    Run it in the MySQL client:

    mysql> source example.sql
    --------------
    SELECT COUNT(*) FROM `mysql`.`user`
    --------------
    
    +----------+
    | COUNT(*) |
    +----------+
    |       24 |
    +----------+
    1 row in set (0.00 sec)
    
    0 讨论(0)
  • 2020-12-09 01:36

    This will give you are simple print within a sql script:

    select 'This is a comment' AS '';
    

    Alternatively, this will add some dynamic data to your status update if used directly after an update, delete, or insert command:

    select concat ("Updated ", row_count(), " rows") as ''; 
    
    0 讨论(0)
  • 2020-12-09 01:43

    I don't know if this helps:

    suppose you want to run a sql script (test.sql) from the command line:

    mysql < test.sql
    

    and the contents of test.sql is something like:

    SELECT * FROM information_schema.SCHEMATA;
    \! echo "I like to party...";
    

    The console will show something like:

    CATALOG_NAME    SCHEMA_NAME            DEFAULT_CHARACTER_SET_NAME      
             def    information_schema     utf8
             def    mysql                  utf8
             def    performance_schema     utf8
             def    sys                    utf8
    I like to party...
    

    So you can execute terminal commands inside an sql statement by just using \!, provided the script is run via a command line.

    \! #terminal_commands
    
    0 讨论(0)
  • 2020-12-09 01:46

    You can use print -p -- in the script to do this example :

    #!/bin/ksh
    mysql -u username -ppassword -D dbname -ss -n -q |&
    print -p -- "select count(*) from some_table;"
    read -p get_row_count1
    print -p -- "select count(*) from some_other_table;"
    read -p get_row_count2
    print -p exit ;
    #
    echo $get_row_count1
    echo $get_row_count2
    #
    exit
    
    0 讨论(0)
  • 2020-12-09 01:53

    Just to make your script more readable, maybe use this proc:

    DELIMITER ;;
    
    DROP PROCEDURE IF EXISTS printf;
    CREATE PROCEDURE printf(thetext TEXT)
    BEGIN
    
      select thetext as ``;
    
     END;
    
    ;;
    
    DELIMITER ;
    

    Now you can just do:

    call printf('Counting products that have missing short description');
    
    0 讨论(0)
提交回复
热议问题