In this post, we’re going to talk about using reserved keywords as column names. This is very similar to the previous “IMPALA – Reserved Keywords As Columns” article, but we will see how it can be implemented in SQL Server this time. It is not best practice to use the reserved keywords for tables and columns, as mentioned in the previous article. Yet one must know how to execute it in the case of condition demands.
In Impala, an error is returned when choosing the columns that are currently reserved keywords in Impala. This is because the tables were built in Hive with the keywords reserved in Impala. We have to use the “`” (escape) symbol for these columns. Similarly, in SQL Server, we would use the name of the column in the “[” and “]” (square brackets a.k.a. box brackets).
Let’s check it out.
USE dbTest
GO
CREATE TABLE ReservedWords([Select] INT, [Update] INT,[Delete] INT, [Insert] INT);
INSERT INTO ReservedWords VALUES (170,20,3,60)
INSERT INTO ReservedWords VALUES (120,15,5,70)
INSERT INTO ReservedWords VALUES (110,17,2,80)
INSERT INTO ReservedWords VALUES (140,12,1,90)
SELECT * FROM ReservedWords
OR you can use-
SELECT [select], [insert], [update], [delete] FROM ReservedWords
The same is applicable to the table names. Look at the below example.
CREATE TABLE [select] ([int] INT);
INSERT INTO [select] VALUES (10),(20),(30)
SELECT [int] FROM [select]
Hope you find this article helpful.
Really it is very good technique, while creating table no need to give more lines to insert values.
In one line itself we can insert the values, if we use box brackets at column names.
Thanks to SHAFI, it is very use full blog to learners as well as seniors who are working on SQL
LikeLike
I spend a lot of time writing blog posts and frequently forget to express gratitude to my readers and followers. Your feedback is really valuable to me. Thanks a lot.
LikeLike