Spring-boot to perform a modified table structure in the Startup run script

Source: Internet
Author: User

Spring-boot can create a table directly from the SQL file by simply configuring it. However, the file defaults to ";" As a conclusion, a stored procedure is required in the modification of the table structure. A ";" is required in the stored procedure. An execution error will occur. Good pit!

Solution:

Add the configuration of the end symbol to change the SQL execution of Spring-boot, and later at the end of the SQL file is "$$"

spring.datasource.separator=$$

SQL file, the function verifies that the table has a field and creates the field if it does not exist.

/*charge Audit-Main Table add field*/Drop procedure if existsadd_col_homework$$Create procedureAdd_col_homework ()BEGINIF  not EXISTS(SELECTcolumn_name fromInformation_schema.columnsWHEREtable_name= 'chargeopaudition'  andcolumn_name= 'remarks') Then   ALTER TABLE' Chargeopaudition 'ADD COLUMN' Remarks 'varchar( -)CHARACTER   SETUTF8 COLLATE utf8_general_ciNULL DEFAULT NULL ;END IF; IF  not EXISTS(SELECTcolumn_name fromInformation_schema. COLUMNSWHEREcolumn_name='Transactionidstart'  andtable_name='chargeopaudition'  andTable_schema='ztev_db') Then        ALTER TABLE' Chargeopaudition 'ADD COLUMN' Transactionidstart 'varchar( -)DEFAULT NULLCOMMENT'Start Trading Number' ; END IF; IF  not EXISTS(SELECTcolumn_name fromInformation_schema. COLUMNSWHEREcolumn_name=' State'  andtable_name='chargeopaudition'  andTable_schema='ztev_db') Then        ALTER TABLE' Chargeopaudition 'ADD COLUMN' State 'int(2)DEFAULT '1'COMMENT'1: Not reviewed, 2: Reviewed' ; END IF; IF  not EXISTS(SELECTcolumn_name fromInformation_schema. COLUMNSWHEREcolumn_name='Transactionidend'  andtable_name='chargeopaudition'  andTable_schema='ztev_db') Then        ALTER TABLE' Chargeopaudition 'ADD COLUMN' Transactionidend 'varchar( -)DEFAULT NULLCOMMENT'End Transaction Number' ; END IF;END$ $CALL add_col_homework () $$Drop procedure if existsadd_col_homework$$

Often write blogs, as notes

Spring-boot to perform a modified table structure in the Startup run script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.