2

Has anyone tried to dynamically turn a standard sql string into a parameterized query? In other words, I need to change this:

SELECT * FROM customers WHERE name = 'Adams'

to this:

SELECT * FROM customers WHERE name = @name

I've started to work with the Microsoft.Data.Schema.ScriptDom.Sql and Microsoft.Data.Schema.ScriptDom assemblies, but before I bear down on it, I was wondering if there is something already out in the wild that would do this.

1
  • 1
    I've never seen it before. What exactly are you trying to do that would require this? Commented Feb 11, 2011 at 23:59

2 Answers 2

5

SQL Server does this under the covers (and tools like ClearTrace).

One way is to use a regular expression to normalise (not exactly what you are looking for) such as this SQL CLR method based on work done by Itzik Ben-Gan and modified by Adam Machanic:

[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true)]
public static SqlString sqlsig(SqlString querystring)
{
    return (SqlString)Regex.Replace(
       querystring.Value,
       @"([\s,(=<>!](?![^\]]+[\]]))(?:(?:(?:(?:(?# expression coming
       )(?:([N])?(')(?:[^']'')*('))(?# character
       )(?:0x[\da-fA-F]*)(?# binary
       )(?:[-+]?(?:(?:[\d]*\.[\d]*[\d]+)(?# precise number
       )(?:[eE]?[\d]*)))(?# imprecise number
       )(?:[~]?[-+]?(?:[\d]+))(?# integer
       )(?:[nN][uU][lL][lL])(?# null
       ))(?:[\s]?[\+\-\*\/\%\&\\^][\s]?)?)+(?# operators
       )))",
       @"$1$2$3#$4");
}

but accuracy of output may not catch every possible parameterisable phrase.

I'm curious as to why you need this?

Update: As Martin mentioned, there is also the RML Utilities for SQL Server

Description of the Replay Markup Language (RML) Utilities for SQL Server

Sign up to request clarification or add additional context in comments.

4 Comments

+1 I think RML Utilities does this as well but I'm not sure whether it requires a trace file to work off.
@Martin: Yup. Mentioned them here: mitch-wheat.blogspot.com/2008/08/…
This is a backend for a site that is completely data driven instead of code driven - ie: there are no objects per se in our code, the objects are built up into JSON via a metadata layer that retrieves the schema from database/caching layer. Interesting about RML - never heard of that. Its not what I need for this case, but its nice to know its out there.
I was ultimately able to do this via Microsoft.Data.Schema.ScriptDom rather easily but you had an interesting answer - and the regex is scary looking ;) so I am marking this as the answer.
2

Well my knee jerk reaction for someone doing string searches/manipulation would be to use a regex to search/replace.

The hard part becomes determining what you replace with? a parameter called @Adam seems pretty weird and i'm not sure how you would know you want to replace it with @Name.

Can you describe what your end goals is? There may be a better way to do this...

4 Comments

I actually need to parse the string and parameterize it but I also need to map back the columns to a metadata layer that applies the proper validation and column names, etc.
I was able to get Microsoft.Data.Schema.ScriptDom to work. Once I've cleaned it up, I'll post the code back for future ref.
@HostDude - please do. Sounds interesting.
I decided to bag it using the scriptdom. Not sure if it was something I was doing, but it was very very slow (200+ ms) to evaluate simple strings. I wound up using the Gold Parser which already has a SQL syntax - devincook.com/goldparser. Overkill for simple strings, but we needed it in other parts of our app anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.