2017-08-30 155 views
0

如果存在,我需要刪除一個表列,但我有一些問題..我不斷收到此錯誤,我不能圖它爲什麼刪除表列如果存在(SQLSTATE [42000]語法錯誤或訪問衝突)

An error occurred when processing the migration: 
    Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_d' at line 1 

這是我的查詢

IF (EXISTS (SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_db_Name' AND TABLE_NAME = 'my__table' AND COLUMN_NAME = 'currency_format')) 
BEGIN 
    ALTER TABLE 'my__table' DROP COLUMN currency_format; 
END 

phpMyAdmin的SQL控制檯還報告錯誤 enter image description here

那麼,該怎麼做,所以我將是AB le執行這個查詢? 謝謝!

回答

0

我提出的解決方法用下面的代碼和它現在的工作

set @exist_Check := (
select count(*) from information_schema.columns 
where TABLE_NAME='my__table' 
and COLUMN_NAME='currency_format' 
and TABLE_SCHEMA='my_db_Name' 
); 
set @sqlstmt := if(@exist_Check>0,'alter table my__table drop column currency_format', 'select ''''') ; 
prepare stmt from @sqlstmt ; 
execute stmt ; 
0

嘗試解除引用的表名在ALTER TABLE

0

你可以使用:

IF (SELECT 1=1 FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'my_db_Name' AND TABLE_NAME = 'my__table' AND COLUMN_NAME = 'currency_format') THEN 
BEGIN 
    ALTER TABLE my__table DROP COLUMN currency_format; 
END IF; 
相關問題