qb icon indicating copy to clipboard operation
qb copied to clipboard

upsert() throws 'more question marks in the SQL than params defined' if parameters are repeated in update

Open michaelborn opened this issue 8 months ago • 0 comments

This query attempts to update the Name column using a passed-in value, but QB seemingly fails to append the binding to the queryExecute call:

getInstance( "QueryBuilder@qb" )
    .table( "myTable" )
    .upsert(
        target = [ "Vendor_Code", "Code" ],
        values = {
            "Vendor_Code"  : variables.vendor_code,
            "Code"         : row.Code,
            "Count"        : 1,
            "Name"         : row.Name
        },
        update = {
            "Count"        : newQueryBuilder().raw( "[qb_target].[Count] + 1" ),
            "Name"         : row.Name
        }
    );

This throws:

there are more question marks in the SQL than params defined

MERGE [myTable] AS [qb_target]
USING
    (VALUES (?, ?, ?, ?)) AS [qb_src] ([Code], [Count], [Name], [Vendor_Code])
    ON [qb_target].[Vendor_Code] = [qb_src].[Vendor_Code]
AND [qb_target].[Code] = [qb_src].[Code]
WHEN MATCHED THEN 
    UPDATE SET [Count] = [qb_target].[Count] + 1, [Name] = ?
WHEN NOT MATCHED BY TARGET THEN 
    INSERT ([Code], [Count], [Name], [Vendor_Code])
    VALUES ([Code], [Count], [Name], [Vendor_Code])

michaelborn avatar Jun 16 '25 13:06 michaelborn