Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table alias ignored for columns specified in INSERT statement in SQL Server

Tags:

People also ask

Can we use alias in insert statement?

As others have said, you cannot alias the name as part of the INSERT INTO statement. You would need to put it in the subquery in the WHERE statement.

Is an alias required for a column created in the SELECT clause?

The aliases are mandatory for inline queries (queries embedded within another statement) as a data source to qualify the columns in a select list.

Can we use column alias in SELECT statement?

Alias is used to give a temporary name(only for the duration of the query) to the column or table in order to make the column name or table name more readable. It does not change the name of the column permanently.

Can we add alias columns in SQL query?

SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.


I saw above question in one of the blogs. But couldn't find the answer anywhere. Maybe SQL Server experts here can help me on this.

CREATE TABLE #DEMO (VALUE1 INT, VALUE2 VARCHAR(10))

CREATE TABLE #DEMO1 (VALUE1 INT, VALUE2 VARCHAR(10))

INSERT INTO #DEMO VALUES (1, '10')

INSERT INTO #DEMO1 (a.a.a.value1,b.b.b.value2)
    SELECT
        Value1, Value2
    FROM #DEMO

If you see the insert statement, I have specified some alias name in the inserted columns. To my wonder, SQL Server didn't throw any error while executing the statement. What's the reason behind this? Could anyone help?