How To Insert Title At Start of SQL Results and Between Another SQL Result Sets

I have three queries in one file.

I would like to insert a title line at the beginning of the first set of results, and between the next two queries, to show the change of information.

Don't want want my users to need use three separate queries, one and done preferred.

Using MS SQL Server Management Studio 18.4

PRINT 'Title for Query One'

SELECT * FROM myTable1 -- query 1

PRINT 'Title for Query Two'

SELECT * FROM myTable2 -- query 2

PRINT 'Title for Query Three'

SELECT * FROM myTable3 -- query 3
1 Like

Hot DANG!!

Thank you.

Well, I thought that was going to work, but...that did not show in the results...

The PRINT output will show in the Messages tab, not grid results. If they were outputting to Text Results, then they would see it.

Change PRINT to SELECT in order to see the headings in Grid Results.

Maybe this is an option:

SELECT '--' AS Unit, '---------' AS System
UNION
SELECT Unit, System
FROM FirstQuery
UNION 
SELECT '--' AS Unit, '---------' AS System
UNION
SELECT Unit, System
FROM SecondQuery

If it is, make sure you underestand the difference between UNION and UNION ALL.

I know it's been awhile since I posted, but wanted to share back to you.

I receive the following message when performing:

Conversion failed when converting the varchar value '----' to data type tinyint.

That is strange, he thinks that '----' is an tiny integer, like unit. Are you inserting the results into a table first? I prefer to keep the formatting / display issues at the reporting side and not at the database.

Maybe you should convert unit to a varchar to be safe:

SELECT '--' AS Unit, '---------' AS System
UNION
SELECT CONVERT(VARCHAR(5),Unit), System
FROM FirstQuery
UNION 
SELECT '--' AS Unit, '---------' AS System
UNION
SELECT CONVERT(VARCHAR(5),Unit), System
FROM SecondQuery