Frage

I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName What does this imply?

SELECT *, NULL as aColumn
War es hilfreich?

Lösung

Aliasing can be used in a number of ways, not just to shorten a long column name.

In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.

Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.

Andere Tipps

When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.

In the statement result we have a column that has all NULL values. We can refer to that column using alias.

In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.

It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.

---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.

When using SELECT you can pass a value to the column directly. So something like :

SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress

Is valid.

It can be used to format nicely a query output when using UNION/UNION ALL.

Query result can have a new column that has all NULL values. In SQL Server we can do it like this

SELECT *, CAST(NULL AS <data-type>) AS as aColumn

e.g.

SELECT *, CAST(NULL AS BIGINT) AS as aColumn

How about without using the the as SELECT ID , Name , 'None' AS Hobbies , 0 AS NumberOfPets , NULL Picture

Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.

UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...

Then exporting or saving the results to another table.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top