What is the correct syntax of a select in SQL SERVER (alias)?

0

What is the correct syntax in a select using SQL SERVER?

SELECT columna FROM [basedatos].dbo.tabla AS tb WHERE condiciones

O

SELECT columna FROM basedatos.dbo.tabla tb WHERE condiciones

Can you use aliases?

Are brackets [] necessary?

    
asked by Pavlo B. 23.01.2018 в 09:36
source

1 answer

1

The brackets are necessary if you use special characters (spaces, periods, ...) in the names of the elements of the database (database, tables, columns, views, ...).

For example you can create a field in a table with the name mi campo . If you make a query of the type:

SELECT mi campo FROM tabla

SQL Server will try to find the field mi in table tabla and use the alias campo for the result. If there is no field mi will return an error.

It is in these cases when brackets must be used:

SELECT [mi campo] FROM tabla

Many query generation tools are always added to avoid problems, but would only be necessary in those cases.

    
answered by 23.01.2018 / 09:49
source