SQLException:' bit'附近的语法不正确

时间:2016-01-09 14:46:58

标签: java sql-server jdbc

我已经使用JDBC连接到SQL Server。我运行的一个查询有数据类型位,当我运行程序时,我收到此错误:SQLException:' bit'附近的语法不正确。

这是我的代码:

            String sql3 = "SELECT DISTINCT customCategoryDescription FROM product_categories ORDER BY customCategoryDescription";
        rs2 = stmt3.executeQuery(sql3);

        while (rs2.next())
        {
            String customCategoryDescription = rs2.getString("customCategoryDescription");
            columns.add(customCategoryDescription);
        }
        rs2.close();
        stmt3.close();

        for(int i = 0; i < columns.size(); i++)
        {
            String sql4 = "ALTER TABLE transformed_table ADD "+columns.get(i)+" bit";
            stmt4.executeUpdate(sql4);
            stmt4.close();
        }

我在SQL Server中尝试了相同的查询,并且成功添加了该列。

问题出在哪里?

1 个答案:

答案 0 :(得分:2)

我的猜测是你在customCategoryDe​​scription中有一个无效列名的值 - 比如“NO SPACES”,这意味着:

ALTER TABLE transformed_table ADD NO SPACE bit;

会生成这个:

String sql4 = "ALTER TABLE transformed_table ADD ["+columns.get(i)+"] bit";

ALTER TABLE transformed_table ADD [NO SPACE] bit;

如果你这样做(注意[和]):

aws ecr get-login --region us-east-1
相关问题