Search code examples
sql-servert-sqlsql-server-2016

Select fictitious column headers without records


I can't believe I can't figure this out or find anything related to this. I'm trying to generate a set of column headers dynamically but with no data (as if it was an empty table).

SELECT Null AS [CODE], Null AS [DESC];

will return

CODE        DESC
----------- -----------
NULL        NULL

which is close, but I need it to have no records:

CODE        DESC
----------- -----------

As best I can replicate the exact requirement:

DECLARE @Table TABLE
(
    [CODE] bit NULL,
    [DESC] bit NULL
);

SELECT [CODE], [DESC]
FROM @Table;

Which is what I'll go with if I can't find anything similar but this just feels soooo verbose for something that feels trivial?


Solution

  • Just use a false condition in a WHERE clause:

    SELECT Null AS [CODE], Null AS [DESC]
    WHERE 1=0
    

    See the demo.
    This way you can pass any value to the 2 columns, not just null.