Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql uses stored procedures to determine whether a column (field) exists as a tutorial

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Below to bring you mysql through stored procedures to determine whether the column (field) exists tutorial-related content, I believe you must have seen similar articles. What's the difference between what we bring to everyone? Let's take a look at the body. I'm sure you'll get something after reading mysql to determine whether a column (field) exists through a stored procedure.

Determine whether the field exists:

DROP PROCEDURE IF EXISTS schema_change; DELIMITER / / CREATE PROCEDURE schema_change () BEGIN DECLARE CurrentDatabase VARCHAR (100); SELECT DATABASE () INTO CurrentDatabase;IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=CurrentDatabase AND table_name = 'rtc_order' AND column_name =' IfUpSend') THEN ALTER TABLE rtc_order ADD COLUMN `IfUpSend `BIT NOT NULL DEFAULT 0 COMMENT 'whether to upload or not'; END IF; END// DELIMITER; CALL schema_change ()

Mysql determines whether the field exists, and if so, modify the field:

DROP PROCEDURE IF EXISTS proc_tempPro;if (@ count > 0) THEN alter table table name change column `old column name ``new column name `varchar (30) comment 'field description'; end if;end;call proc_tempPro;DROP PROCEDURE IF EXISTS proc_tempPro

Determine whether the field exists through the stored procedure. If it does not exist, it will be added:

DROP PROCEDURE IF EXISTS pro_AddColumn;CREATE PROCEDURE pro_AddColumn () BEGINIF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='component' AND COLUMN_NAME='PRINT_CHECK_STATUS') THENALTER TABLE component ADD PRINT_CHECK_STATUS int (10) default 0 there end IF;IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='component' AND COLUMN_NAME='PRINT_CHECK_TIME') THENALTER TABLE component ADD PRINT_CHECK_TIME datetime NULL;END IF IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema=podcloud AND table_name='component' AND COLUMN_NAME='PRINT_CHECK_BACK_REASON') THENALTER TABLE component ADD PRINT_CHECK_BACK_REASON varchar (500) default null;END IF;END;CALL pro_AddColumn;DROP PROCEDURE pro_AddColumn -DROP PROCEDURE IF EXISTS pro_AddIndex; DELIMITER CREATE PROCEDURE pro_AddIndex () BEGIN IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = 'rtc_phototype' AND index_name =' index_name') THEN ALTER TABLE `rtc_ Phototype` ADD INDEX index_name (`imgtype`); END IF; END; DELIMITER; CALL pro_AddIndex (); Drop procedure pro_AddIndex

For the above tutorial on mysql using stored procedures to determine whether a column (field) exists, do you think it is what you want? If you want to know more about it, you can continue to follow our industry information section.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 295

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report