How to Execute SQL Query without Displaying results

前端 未结 11 1242

Is it possible that Execute SQL Query without Displaying results?

like

Select * from Table_Name

after running this query result sh

相关标签:
11条回答
  • 2020-12-03 07:07

    I'm surprised nobody came up with the answer : switch on the "discard query results after execution" option; l I'm pretty sure that was what the interviewer was after. SET FMT ONLY is totally different thing IMHO.

    In SSMS

    • open a new query
    • in the menu select Query / Query options
    • select the Results pane
    • check the "discard result after execution"

    The reason you might want to do this is to avoid having to wait and waste resources for the results to be loaded into the grid but still be able to have e.g. the Actual Execution Plan.

    0 讨论(0)
  • 2020-12-03 07:08

    If you need the query to execute but don't need the actual resultset, you can wrap the query in an EXISTS (or NOT EXISTS) statement: IF EXISTS(SELECT * FROM TABLE_NAME...). Or alternately, you could select INTO #temp, then later drop the temp table.

    0 讨论(0)
  • 2020-12-03 07:13

    Executing will return a recordset. It may have no rows of course but get a result

    You can suppress rows but not the resultset with SET FMTONLY

    SET FMTONLY ON
    SELECT * FROM sys.tables
    
    SET FMTONLY OFF
    SELECT * FROM sys.tables
    

    Never had a use for it personally though...

    Edit 2018. As noted, see @deroby's answer for a better solution these days

    0 讨论(0)
  • 2020-12-03 07:17

    I am surprised the community can't easily find a use case for this. Large result sets take memory on the client, which may become a problem if many SSMS windows are active (it is not unusual for me to have 2-3 instances of SSMS opened, each with 50-70 active windows). In some cases, like in Cyril's example, SSMS can run out of memory and simply unable to handle a large result set. For instance, I had a case when I needed to debug a stored procedure returning hundreds of millions of rows. It would be impossible to run in SSMS on my development machine without discarding results. The procedure was for an SSIS package where it was used as a data source for loading a data warehouse table. Debugging in SSMS involved making non-functional changes (so the result set was of no interest to me) and inspecting execution statistics and actual query execution plans.

    0 讨论(0)
  • 2020-12-03 07:18

    Yet another use case is when you just want to read all the rows of the table, for example testing against corruptions. In this case you don't need the data itself, only the fact that it is readable or not. However, the option name "Discard results AFTER execution" is a bit confusing - it tells me that the result is fetched and only then discarded. In contrary, it fetches the data for sure but does not store it anywhere (by default the rows are put into the grid, or whatever output you have chosen) - the received rows are discarded on the fly (and not AFTER execution).

    0 讨论(0)
提交回复
热议问题