Search code examples
sqlsql-serversql-server-2012ssms-2012

How to get NULL as values for invalid column


I'm using Sql Server 2012. Consider the table schema to be,

create table A (col1 int, col2 int)

I'm trying to execute this query,

select col1, col2, col3, col4 from A

I get execution error as col3 and col4 are not in table.

But is there any way, these 2 columns can be displayed in result with NULL as value for every row, even though it's not available in the table?


Solution

  • Use an alias for each one of these 2 columns:

    select col1, col2, null as col3, null as col4 from A