Skip to content

[mssql] MSSQL returns incorrect result for query #819

Open
@JanBerktold

Description

@JanBerktold

Overview

The simple console app below returns a widely incorrect result for the 'id' value, presumably because of a bug in the parsing logic.

Repro

Database

In a local MSSQL docker instance running 2017-CU7, create a database called 'bug-repo' with this table:

USE [bug-repro]
GO

CREATE TABLE [dbo].[SimpleTable](
	[id] [bigint] IDENTITY(1,1) NOT NULL,
	[string_field] [nvarchar](max) NOT NULL,
 CONSTRAINT [PK_Roblox.Testing.TestRecords] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

Console App

use sqlx::Arguments;
use sqlx::{mssql::MssqlArguments, Row};

#[tokio::main]
async fn main() {
    let connection_pool =
        sqlx::MssqlPool::connect_lazy("mssql://sa:test_123@localhost/bug-repro").unwrap();

    let mut args = MssqlArguments::default();
    args.add("hi");

    let row = sqlx::query_with("INSERT INTO [SimpleTable] ([string_field]) OUTPUT INSERTED.[string_field],INSERTED.[id] VALUES (@p1);", args)
    .fetch_one(&connection_pool).await.unwrap();

    let id: i64 = row.try_get("id").unwrap();
    println!("{:#?}", id);
}

[package]
name = "sqlx-tester"
version = "0.1.0"
edition = "2018"

[dependencies]
sqlx = { version = "0.4.1", default-features = false, features = [ "runtime-tokio-native-tls", "macros", "mssql" ] }
tokio = { version = "0.2.22", features = ["macros", "rt-threaded", "signal"] }

Results

Actual

cargo run
   Compiling sqlx-tester v0.1.0 (/home/jberktold/git/sqlx-tester)
    Finished dev [unoptimized + debuginfo] target(s) in 1.37s
     Running `target/debug/sqlx-tester`
29273397578170368

Expected

I expected the console app to return the correct ID, which is 1 in my case since I've setup a fresh database. Note that the result is correct if I flip 'id' and 'string_field'.

let row = sqlx::query_with("INSERT INTO [SimpleTable] ([string_field]) OUTPUT INSERTED.[id],INSERTED.[string_field] VALUES (@p1);", args)

leads to the correct

cargo run
   Compiling sqlx-tester v0.1.0 (/home/jberktold/git/sqlx-tester)
    Finished dev [unoptimized + debuginfo] target(s) in 0.97s
     Running `target/debug/sqlx-tester`
151

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions