The Wayback Machine - https://web.archive.org/web/20200918102806/https://github.com/sqlkata/querybuilder/pull/304
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for ad-hoc table CTEs with parameterized data #304

Conversation

@freakingawesome
Copy link

freakingawesome commented Oct 8, 2019

This addresses the request in #152 by adding a With(...) variation that takes a list of column names and a list of list of values to create a CTE representation of that data. That CTE can then be used in subsequent SELECT statements to, for example, query efficiently against a number of composite keys.

Here is an example for creating a CTE with two rows of parameterized data:

var query = new Query("rows").With("rows",
    new[] { "a", "b", "c" },
    new object[][] {
        new object[] { 1, 2, 3 },
        new object[] { 4, 5, 6 },
    });

This conceptually results in a CTE whose contents are:

a b c
1 2 3
4 5 6

The default SQL implementation uses a number of SELECT statements joined by UNION ALL:

Postgres Output:

WITH "rows" AS (
    SELECT 1 AS "a", 2 AS "b", 3 AS "c"
    UNION ALL
    SELECT 4 AS "a", 5 AS "b", 6 AS "c"
)
SELECT *
FROM "rows"

SQL Server output uses its own VALUES (...), (...) syntax:

WITH [rows] AS (
    SELECT [a], [b], [c]
    FROM (
        VALUES
            (1, 2, 3),
            (4, 5, 6)
    ) AS tbl ([a], [b], [c])
)
SELECT *
FROM [rows]

Note: The initial issue #152 showed that Postgres has its own syntax for ad-hoc tables in a CTE, but I did not implement that specific flavor because it would involve more substantial changes to the actual WITH portion of the CTE, e.g. WITH cte (a, b, c) AS (...). Postgres instead uses the UNION ALL flavor

@mathijs-dumon
Copy link
Contributor

mathijs-dumon commented Dec 20, 2019

just a +1
I'd really like to see this land as well!

@ahmad-moussawi ahmad-moussawi force-pushed the sqlkata:master branch 6 times, most recently from f9a6417 to a30bb49 Jan 1, 2020
@eyaldar
Copy link

eyaldar commented Jul 14, 2020

Any plans to push this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.