2011-11-16 101 views
3

我想在sqlite3的执行以下类型的代码:SQLite的:条件DDL语句

drop table x if (select y from z where col1 = "a"); 

本质上执行DDL命令IF在同一数据库中当前存在的某些值。

任何想法?

回答

0

正如其他人指出,运行它,这是不可能的,而无需使用外部工具。

我暂时的解决方案是这样的:

PSEUDOCODE: 
// create temp table; 
// insert conditional DDL statement(s) into temp table along with an execute column 
// run condition, and if valid, set temptable's corresponding row to execute 1; 
// after condition is over, run all true statements in temp table 
1

一个SQLite shell脚本:

.bail ON 
SELECT CASE COUNT(*) 
     WHEN 0  THEN 1/0 -- will throw 
     ELSE 1 
     END 
    FROM z 
WHERE col1 = 'a'; 
DROP TABLE x; 

sqlite the_database < the_script

+0

这是一个聪明的做法。它会在控制台上看起来像是失败吗? – seh

0

SQL不给你来执行这个功能作为一个班轮的方式。 你会得到在一个命令这样做的最接近的是:

  1. 问题:BEGIN TRANSACTION
  2. 执行查询:SELECT y在z,其中COL1 = “一个”
  3. DROP TABLE X
  4. 如果(结果从第2步有> 0行) 问题:COMMIT 其他 问题:ROLLBACK