fsprojects / Rezoom.SQL

Statically typechecks a common SQL dialect and translates it to various RDBMS backends
MIT License
670 stars 26 forks source link

Error using LIMIT with a parameter #33

Closed rkosafo closed 6 years ago

rkosafo commented 6 years ago

I get the error below when I execute a query with limit specified as a variable.

System.Data.SqlClient.SqlException: 'Incorrect syntax near '@RZSQL_0'.'

To reproduce

open Rezoom.SQL
open Rezoom.SQL.Migrations
open Rezoom.SQL.Synchronous

type MyModel = SQLModel<".">

let migrate () =
  let config =
    { MigrationConfig.Default with 
        LogMigrationRan = fun m -> printfn "Run migrations: %s" m.MigrationName }
  MyModel.Migrate config

type ReadUsers = SQL<"Select name, email from Users limit @top">
let error () =
  use context = new ConnectionContext ()
  let users = ReadUsers.Command(5L).Execute context
  printfn "Users: %A" users

[<EntryPoint>]
let main argv = 
  migrate ()
  error ()
  0 // return an integer exit code

Using Rezoom.SQL.Provider.TSQL v0.7.0 Rezoom.SQL.Provider v0.7.0.36923 Rezoom v0.4.2

rspeele commented 6 years ago

Problem was the T-SQL output would never parenthesize the EXPR in:

select top EXPR col1, col2, col3 from tbl

This works if EXPR is a literal like select top 1 but not for more complex expressions, including parameter references. They need parentheses, e.g. select top (@parameter).

I've released 0.7.1 with the simple fix for this.

https://www.nuget.org/packages/Rezoom.SQL.Provider/0.7.1.10316

rkosafo commented 6 years ago

Issue resolved using the new nuget package. Thanks.