Search
 
SCRIPT & CODE EXAMPLE
 

SQL

drop column from table if exist on live server mysql

-- column_exists:

DROP FUNCTION IF EXISTS column_exists;

DELIMITER $$
CREATE FUNCTION column_exists(
  tname VARCHAR(64),
  cname VARCHAR(64)
)
  RETURNS BOOLEAN
  READS SQL DATA
  BEGIN
    RETURN 0 < (SELECT COUNT(*)
                FROM `INFORMATION_SCHEMA`.`COLUMNS`
                WHERE `TABLE_SCHEMA` = SCHEMA()
                      AND `TABLE_NAME` = tname
                      AND `COLUMN_NAME` = cname);
  END $$
DELIMITER ;

-- drop_column_if_exists:

DROP PROCEDURE IF EXISTS drop_column_if_exists;

DELIMITER $$
CREATE PROCEDURE drop_column_if_exists(
  tname VARCHAR(64),
  cname VARCHAR(64)
)
  BEGIN
    IF column_exists(tname, cname)
    THEN
      SET @drop_column_if_exists = CONCAT('ALTER TABLE `', tname, '` DROP COLUMN `', cname, '`');
      PREPARE drop_query FROM @drop_column_if_exists;
      EXECUTE drop_query;
    END IF;
  END $$
DELIMITER ;
Comment

PREVIOUS NEXT
Code Example
Sql :: no query unable to fetch row sqlite 
Sql :: SQLSTATE[HY000] [1298] Unknown or incorrect time zone 
Sql :: How to Search in all Columns for all tables in a database for Date Value in SQL Server - SQL Server 
Sql :: sql server convert string list integers list 
Sql :: rollback to name in sql 
Sql :: sqlc yml settings version 1.14 
Sql :: rails sql query converstion 
Sql :: mysql where sum 0 
Sql :: how to make letter id primary key in mysql 
Sql :: how to add mysql to path on termin after installation 
Sql :: conditional index in postgres 
Sql :: SQL AND, OR and NOT Operators 
Sql :: how to make full text search dynamic in mysql 
Sql :: basic sql queries interview questions 
Sql :: create user faunadb 
Sql :: could not find driver (SQL: PRAGMA foreign_keys = ON;) larave 
Sql :: error database connection 
Sql :: SQL IN Operator With Columns 
Sql :: mysql sum per week 
Sql :: how to restart postgres server on windows 
Sql :: create tables from xsd to sql server database c# 
Sql :: azure sql-datenbank 
Sql :: sqlite send a query to a Sqlite DB with Ruby 
Sql :: update having mysql 
Sql :: including parameters in OPENQUERY 
Sql :: mysql create user if not exists 
Sql :: fast sql column count 
Sql :: datatype for phone number in sql 
Sql :: partitioning in oracle-base 
Sql :: mysql faster insert 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =