Mysql drop column using procedures

Mysql drop column using store procedure like.

Example.
CREATE PROCEDURE SPDROPCOLUMNPROC(IN schemaName varchar(500),
IN tableName varchar(500), IN columnName varchar(500))
BEGIN
  DECLARE count int DEFAULT 0;
  SELECT
    COUNT(*) INTO count
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = schemaName
  AND TABLE_NAME = tableName
  AND COLUMN_NAME = columnName;
  IF count > 0
  THEN

    SET @query = CONCAT('ALTER TABLE ', schemaName, 
      '.', tableName, ' DROP COLUMN ', columnName);
    PREPARE qry FROM @query;
    EXECUTE qry;
    DEALLOCATE PREPARE qry;
  END IF;
END


The table result Before.

Execute procedure.
Call SPDROPCOLUMNPROC(@schemaName:='demo',@tableName:='ssinv',@columnName:='Column2');
Result.

Post a Comment

0 Comments