检查SQL语句是否正确形成

时间:2013-03-13 18:38:41

标签: sql oracle plsql

问题:我在.SQL文件中有几百个sql insert语句,它将针对oracle DB运行。我想检查插入语句是否正确形成,就像我想检测我是否错过任何逗号等等。我只是想确保语句运行时没有任何错误。所以我想写一个小的java应用程序来检查脚本运行之前是否可能?

示例查询:

INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(24553, 'IBM');

我想在运行数据库之前检查一下。让我们假设我无法访问任何数据库。

4 个答案:

答案 0 :(得分:0)

您可以尝试使用'explain plan for'为每个SQL语句添加前缀。如果SQL无效,则解释将失败。

答案 1 :(得分:0)

是的,这是可能的。

伪代码可以是

loop through lines
{
 Take the string between first '(' and ')' position;
 split the string into first array with separator ',' using split function.

 Take the string between second '(' and ')' position;
 split the string into  second array with separator ',' using split function.

 compare the no of size for first and second array.
 if they are not equal 
 print the statement for correction;
}

希望这能帮到你

答案 2 :(得分:0)

我之前从未使用过Oracle,因此我不确定是否可以启用某种DUMMY / Test模式。

如果您要做的就是检查拼写错误之类的内容,那么我会将所有表名替换为双重并运行插入。 这不会通过...

验证数据类型的列数

答案 3 :(得分:0)

如果你了解java,你可以使用这个正则表达式模式,它不会拿起(或),我为我的生活无法弄清楚如何匹配一个括号。 这仅适用于您的示例,并且您的整个插入需要位于同一行中,每行只有一个插入。

Pattern.compile("^\\s*INSERT\\s+INTO\\s+[a-zA-Z]\\w*\\s*.\\s*[a-zA-Z]\\w*\\s*,\\s*[a-zA-Z]\\w*\\s*.\\s*VALUES\\s*.\\s*\\d+\\s*,\\s*'\\w*\\'\\s*.\\s*;\\s*$");