SSMS – Having “Select and Print” Share Same Output Space with sqlcmd mode

Background

Ever since I can remember, transact SQL select and print statements send do not share same output area; especially when the the “Results To” is Grid.

SQL

For instance this SQL Statements produces both a Grid and a Text Output.

Code


set nocount on
go

select
        [database] = left(tblSD.[name], 30)

from   sys.databases tblSD

where  tblSD.[database_id] <= 4

order by
        1

print 'Database Current :- '
        + db_name()

Output

Grid

Text

 

Another Way

Like TC, Tevin Campbell, another way, is to have both on same output space.

 

Revised SQL

Use sqlcmd

Outline

  1. Enable SQLCMD Mode by accessing the menu option “Query” \ “SQLCMD mode”.
  2. Add the SQL Statements documented below
    • :OUT STDOUT
      • Enable Text Output for this session
    • GO
      • Execute previous batch

 

Code


set nocount on
go

:OUT STDOUT
GO

select
        [database] = left(tblSD.[name], 30)

from   sys.databases tblSD

where  tblSD.[database_id] <= 4

order by
        1

print 'Database Current :- '
        + db_name()

Output

Text

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s