简体   繁体   中英

Executing oracle query which contains sub-select in c#

here is query which is similar to the one which I want to execute:

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

And I'm executing it like this (from 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();
}

What I'm taking from this is ORA-00933: SQL command not properly ended error.

I've tried to rewrite query to this form:

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

But this hadn't fixed my issue. Is there a way to execute such query?

Change your first query to be

;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

For the re-write of query you need to alias the inline view like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM