T-SQL

Column Aliases are Fully Ignored in INSERT Columns List

Time to time crazy things are happening in SQL Server. T-SQL looks like to be easy and rigid language without any options for playing with it like other languages. I will show you that there is still some space where you can do magic and have some fun. Check out this sample query a test on our own. Yes, it will really execute successfully:).

CREATE TABLE [dbo].[SampleTable]
	([ID] INT, 
	 [Value] NVARCHAR(100))

INSERT INTO [dbo].[Sampletable]
	( ..[Happy]..[New]...[ID], 
	  Another.[Ignored]..Alias.Value )
	SELECT  1, 'Value1'
GO

How that works? Column aliases are ignored in INSERT INTO command so that we can place any funny string there. I think that an error should be raised rather because this lazy behavior can lead to unwanted confusion.

Leave a Reply

Your email address will not be published. Required fields are marked *