FSharp.Data.SqlClient icon indicating copy to clipboard operation
FSharp.Data.SqlClient copied to clipboard

SQL LIKE operation doesn't recognize variable

Open sachinshahredmane opened this issue 1 year ago • 2 comments

Issue Summary

The following SQL in SqlCommandProvider doesn't recognize @logTblName as a substitution variable.

First, here is the raw SQL that works in SSMS:

SELECT *
FROM MyDataTable dl
WHERE dl.Options like '%"STATE\%';

The values that we are looking at look like serialized JSON.

To Reproduce

Create a simple table with at least one column Options set to nvarchar(max). Set values exactly like follows in this field (including the backslashes, quotes, etc.):

[\"COUNTY\"],[\"STATE\"],[\"COUNTRY\"]

Code:

let cmd = 
   new SqlCommandProvider<"
      SELECT *
      FROM MyDataTable dl
      WHERE dl.Options like '%\"@logTblName\\%';
   ",connectionString>(connectionString)

cmd.Execute(logTblName="STATE")

Error

Error message:

error FS0495: The member or object constructor 'Execute' has no argument or settable return property 'logTblName'. The required signature is SqlCommandProvider<...>.Execute() : Collections.Generic.IEnumerable<SqlCommandProvider<...>.Record>.

Expected behavior

It should recognize @logTblName as a parameter.

What you can do

  • [x] I am willing to contribute a PR with a unit test showcasing the issue***
  • [x] I am willing to test the bug fix before next release

The *** is because I don't know enough to fix this, but may be able to do so with (a lot of) guidance.

EDIT: Fixed F#.

sachinshahredmane avatar Jan 25 '24 14:01 sachinshahredmane

@sachinshahredmane, this isn't achievable, the SQL parser itself wouldn't accept the statement.

What you can do, is have like @logTblName in your statement, and put the patterns (and escape %) in the parameter itself; I think this would fit your need, but please let us know.

smoothdeveloper avatar Jan 25 '24 14:01 smoothdeveloper

Ah I see. Will try and get back to you.

sachinshahredmane avatar Jan 25 '24 18:01 sachinshahredmane