执行oracle查询,其中包含c#

时间:2017-04-12 10:50:55

标签: c# sql oracle

这里的查询类似于我想要执行的查询:

with x as(
    select ds.*, row_number() over (partition by FOO order by BAR) rn
    from datasource ds
    where datasupplierid = 3)
select column1, column2, rn from x where rn <= 2

我正在执行它(来自c#):

using (var con = new OracleConnection(this.connectionString)) {
    con.Open();

    OracleCommand cmd = con.CreateCommand();
    cmd.CommandText = this.command;//my query
    cmd.CommandType = CommandType.Text;

    var reader = cmd.ExecuteReader();
}

我从中获取的是 ORA-00933:SQL命令未正确结束错误。

我试图将查询重写为此表单:

select column1, column2, rn from (
    select ds.*, row_number() over (partition by FOO order by BAR) rn
    from datasource ds
    where datasupplierid = 3)
where rn <= 2

但这并没有解决我的问题。有没有办法执行这样的查询?

1 个答案:

答案 0 :(得分:0)

将您的第一个查询更改为

;with x as(
    select ds.*, row_number() over (partition by FOO order by BAR) rn
    from datasource ds
    where datasupplierid = 3)
select column1, column2, rn from x where rn <= 2

要重新编写查询,您需要为内联视图添加别名,如

select column1, column2, rn from (
    select ds.*, row_number() over (partition by FOO order by BAR) rn
    from datasource ds
    where datasupplierid = 3) xxx
where rn <= 2