How to Execute SQL Query without Displaying results

Viewed 114948

Is it possible that Execute SQL Query without Displaying results?

like

Select * from Table_Name

after running this query result should not be displayed in sql server.

12 Answers

I needed a proc to return all records updated by a specified user after a certain point in time, only showing results where records existed. Here it is:

-- Written by David Zanke
-- Return all records modified by a specified user on or after a specified date. 


If mod date does not exist, return row anyhow

Set Nocount on 

Declare @UserName varchar(128) = 'zanked'
    , @UpdatedAfterDate Varchar( 30)  = '2016-10-08'
    , @TableName varchar( 128)
    , @ModUser varchar( 128)
    , @ModTime varchar( 128)
    , @sql varchar( 2000 )


-- In a perfect world, left join would be unecessary since every row that captures the last mod user would have last mod date.

-- Unfortunately, I do not work in a perfect world and rows w/ last mod user exist w/o last mod date

Declare UserRows Cursor for Select distinct c1.table_name, c1.column_name, c2.column_name  From INFORMATION_SCHEMA.COLUMNS c1

Left Join INFORMATION_SCHEMA.COLUMNS c2 On c1.Table_Name = c2.Table_Name And c2.Column_name like '%DTTM_RCD_LAST_UPD%'
        Where c1.column_name like '%UPDATED_BY_USER%'


Open UserRows

Fetch UserRows Into  @tablename, @ModUser, @ModTime

While ( @@FETCH_STATUS = 0 )
Begin
    -- capture output from query into a temp table

    Select @sql = 'Select ''' + @TableName + ''' TableName, * Into ##HoldResults From ' + @TableName + ' Where ' + @ModUser + ' = ''' + @userName + ''''
            + Case When @ModTime Is Null Then '' Else   ' And ' + @ModTime + ' >= ''' + @UpdatedAfterDate + '''' End

    Exec ( @sql)

    -- only output where rows exist
    If @@ROWCOUNT > 0
    Begin
        Select * from ##HoldResults
    End
    Drop Table ##HoldResults

    Fetch UserRows Into  @tablename, @ModUser, @ModTime

End
Close UserRows;
Deallocate UserRows
Related