question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[PLSQL] SELECT INTO FROM sequence issue

See original GitHub issue

I have a table called THE_TABLE with a sequence configured as next: [Column("MY_SEQUENCE", DataType = LinqToDB.DataType.Int32, Length = 10, CanBeNull = false, IsPrimaryKey = true, IsIdentity = true)] [SequenceName("MY_SEQUENCE")] public new int MY_SEQUENCE{ get; set; } when I try to do something like: await _db.OtherTables.Select(x => new MyTable { FOO = x.FOO, BAR = x.BAR, LOREM_IPSUM = x.LOREM_IPSUM }).Into(_db.TheTables).InsertAsync(cancellationToken);

this code is being generated and I cannot find a way to setup the sequence number in the select,

(
        MY_SEQUENCE,
        FOO,
        BAR,
        LOREM_IPSUM
)
SELECT
        o.FOO,
        o.BAR,
        o.LOREM_IPSUM
FROM
        OTHER_TABLE o`

Exception message: Stack trace: Exception: Oracle.ManagedDataAccess.Client.OracleException Message : ORA-00947: not enough values


I tried using Sql.Default<int>() but the ORM is omitting it. For instance:
`await _db.OtherTables.Select(x => new MyTable
                {
                    MY_SEQUENCE = Sql.Default<int>(),
                    FOO = x.FOO,
                    BAR = x.BAR,
                    LOREM_IPSUM = x.LOREM_IPSUM
                }).Into(_db.TheTables)
                .InsertAsync(cancellationToken);`
will generate the same code in raw sql as above.

I already tried using **InsertWithInt32IdentityAsync** as well but is not working, when I try to do it it's failing with:

Exception: Oracle.ManagedDataAccess.Client.OracleException Message : ORA-00933: SQL command not properly ended


Generating this raw sql:


DECLARE @IDENTITY_PARAMETER Decimal
SET     @IDENTITY_PARAMETER = NULL

INSERT INTO THE_TABLE
(
        MY_SEQUENCE,
        FOO,
        BAR,
        LOREM_IPSUM
)
SELECT
        o.FOO,
        o.BAR,
        o.LOREM_IPSUM
FROM
        OTHER_TABLE o`
RETURNING
        MY_SEQUENCE INTO :IDENTITY_PARAMETER

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Sep 29, 2022

Will provide answer tomorrow, if no one has solution right now, Such insert needs defining SQL extension method. We do not do any predictions which SQL to generate for nextval if user used Select. It is fully custom query and needs custom approach.

Anyway, try this simple one:

await _db.OtherTables
      .Into(_db.TheTables, x => new MyTable {
        MY_SEQUENCE = Sql.Expr<int>("MY_SEQUENCE.nextval"), 
        FOO = x.FOO, 
        BAR = x.BAR, 
        LOREM_IPSUM = x.LOREM_IPSUM }
      ))
      .InsertAsync(cancellationToken);
0reactions
sdanylivcommented, Apr 19, 2023

Closing as there is no response.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Oracle SQL: Use sequence in insert with Select Statement
Assuming that you want to group the data before you generate the key with the sequence, it sounds like you want something like....
Read more >
Select from sequence and then insert into table intermittently ...
Thus before every insert into a table, a select to sequence is fired to get the next sequence value and then the insertion...
Read more >
Sequence numbers in "insert into select from order by"
Hi Tom, Recently we solved following problem. We wanted to insert rows into table from specific select statement with order by clause. To...
Read more >
PL/SQL SELECT INTO Statement By Practice Examples
If the SELECT statement does not return any row, Oracle will raise the NO_DATA_FOUND exception. PL/SQL SELECT INTO examples. Let's use the customers...
Read more >
Using SEQUENCE in an INSERT SELECT - Oracle Forum
I have 2 tables: an old_table that contains 'PartNumber,CompName' (not all PartNumber's are distinct) a new_table with 3 columns; RECID.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found