EDIT: Judging from the comments, I have been unclear in what I am trying to achieve. I'll try from another angle.
I have been developing sprocs for a number of years. I increasingly feel that choosing between SQL embedded in C# code and sprocs are both bad choices. I know that many people will disagree and that's fine. I didn't elaborate on this in the question to avoid having the discussion be about sprocs or not :-) .
As an experiment, I have tried embedding .sql files in my project in Visual Studio. Literally, in my project tree I have files with the .sql extension and the Build Action set to Embedded Resource.
That way I can edit the SQL query code from within VS and even run/execute it from within VS, without running the project. I love that. The actual code that I am working on right now, is akin to a "product list" with paging and multiple ordering and filtering options. The SQL query has parameters like @skip, @search, etc. This means that if I try to run/execute it from within VS (specifically by pressing CTRL + SHIFT + e or selecting "Execute" from the "SQL" menu in VS) these parameters are missing (of course), since they are meant to be provided as SqlParameter at runtime. I understand why this happens and by no way intend to imply that either VS or SSMS is bugged. I am merely looking for a way to "tell" VS that "When I execute this query from within VS, I intend for @skip to be 10". I (perhaps mistakenly) assumed that the reason that VS had IntelliSense support and Execute support for .sql files was to support a scenario akin to what I am trying).
I was just hoping someone else was doing the same and had a clever way/addon/trick to support it.
EDIT END
In an project in Visual Studio 2017, I have a number of .sql files which are embedded into the application when built.
While editing the files in VS, I can conveniently connect to a SQL server and execute the query.
However, if the query has parameters like this:
SELECT * FROM Employee WHERE ID=@ID;
The execution, from within Visual Studio, fails with
Must declare the scalar variable "@ID".
This can be "fixed" by adding a line at the top of the script/file, so it looks like:
DECLARE @ID int = 123;
SELECT * FROM Employee WHERE ID=@ID;
However, now it doesn't work when called from within the code like this (using Dapper, it's not relevant to the question, but explains the syntax):
var emp = conn.Query<Employee>( sql, new { ID=123 } );
I was hoping that either I could specify the value of ID somewhere not in the file, or specify some part of the file which VS would execute, but would be ignored later when calling from code.
EDIT: To be clear, I can craft the SQL so that it works flawlessly from within Visual Studio or at runtime, but I cannot have both. I was hoping there was some neat hack, a VS addon or just some VS feature that I was just missing. I have considered adding a simple pre-processor so I can do something like this:
--if DEBUG
DECLARE @ID int = 123;
--endif
SELECT * FROM Employee WHERE ID=@ID;
And then I could add it to my function that loads the embedded resource.
sqlthe value that you expect it to be? what you show should work fine as long assqlis the string like the one in your question. Dapper doesn't care where the string comes from. It only cares what the string contains - so: what is the value ofsql?