Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: way to see final query with filled parameters

Is there a way to see final query which is passed to SQL Server database from my C# app ?

For ex I got query:

SELECT * FROM mytable WHERE x = @yyyy;

This creates and SQLCommand object

SqlCommand cmd = new SqlCommand("SELECT * FROM mytable WHERE x = @yyyy");

Plus I need to pass parameter:

cmd.Parameters.Add("@yyyy","MyValue");

What I want to see (in debug in C# or somewhere in SQL Server Management Studio) is this:

SELECT * FROM mytable WHERE x = MyValue

Where can I find such query ?!

Best regards

like image 441
jackal Avatar asked Jan 21 '23 05:01

jackal


2 Answers

Where can I find such query ?!

You can't. Such a query never exists. The values are not substituted into the SQL.

I think actually sp_executesql is called, and this function accepts the parameters separately from the SQL. You can check this using SQL Profiler to see the actual SQL.


Update:

ORDER BY @descOrAsc

Your problem is that parameters can only be used in certain places where expressions are allowed. DESC is not an expression - it is a reserved word. You cannot use a parameter containing the string "DESC" instead of writing the keyword DESC in the query.

Also, you haven't specified which column to order by.

like image 68
Mark Byers Avatar answered Jan 28 '23 14:01

Mark Byers


You can run the SQL Server Profiler and see all the queries that get executed, to see whats happening (and copy paste these into the Sql Server Management Studio to do tests etc)

like image 31
Mark Redman Avatar answered Jan 28 '23 16:01

Mark Redman