SQL Server: view the final query with filled parameters

Is there a way to see the final query that is being transferred to the SQL Server database from my C # application?

For ex, I received a request:

SELECT * FROM mytable WHERE x = @yyyy; 

This creates an SQLCommand object.

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

Plus I need to pass a parameter:

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

What I want to see (in debugging in C # or somewhere in SQL Server Management Studio) is the following:

 SELECT * FROM mytable WHERE x = MyValue 

Where can I find such a request ?!

Best wishes

+4
source share
4 answers

Where can I find such a request ?!

You can not. Such a request never exists. Values ​​are not replaced in SQL.

I think that sp_executesql is actually called, and this function takes parameters separately from SQL. You can check this with 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 not an expression - it is a reserved word. You cannot use a parameter containing the string "DESC" instead of writing the DESC keyword in the query.

In addition, you did not indicate which column to order.

+6
source

You can run Profiler SQL Server and see all the queries that will be executed, see what happens (and copy them to Sql Server Management Studio for tests, etc.).

+2
source

I expect the query to be passed to SQL Server with parameters. Under no circumstances do you need to create a full SQL query. It makes no sense to do this, because it just means more conversions for the client, server, or both. On the server side, the query processor wants to analyze the query in sentences with values ​​- if the command can pass these values ​​directly, where is the advantage when converting them to an SQL statement, only to let the server analyze them for separate values ​​again?

+1
source

1. You can use SQL Profiler. (here you can see the whole process) 2. You can write all your queries in a SQL Server table. And then you can always receive requests from this table.

0
source

Source: https://habr.com/ru/post/1344365/


All Articles