Click here to Skip to main content
15,891,981 members
Articles / Database Development / SQL Server

SQL SERVER – Bad Practice of Using Keywords as an Object Name – Avoid Using Keywords as an Object

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
10 Dec 2011CPOL 23.6K   2   2
Avoid Using Keywords as an Object

Madhivanan is a SQL Server MVP and a very talented SQL expert. Here is one of the nuggets he shared on Just Learned.

He shared a tip where there were two interesting points to learn:

  1. Do not use keywords as an object name
  2. [Read DHall's excellent comment below]

He has given an excellent example of how GO can be executed as a stored procedure. Here is the extension of the tip. Create a small table and now just hit EXEC GO; and you will notice that there is a row in the table.

Create Stored Procedure:

SQL
CREATE PROCEDURE GO
AS
SELECT 1 AS NUMBER

Create Table:

SQL
CREATE TABLE T1 (ID INT)

Now execute the following code:

SQL
INSERT INTO T1(ID)
EXEC GO;

Now when selecting from table, it will give us the following result:

SQL
SELECT *
FROM T1

Now see the following resultset:

So without inserting any data, we inserted the data, well indeed good puzzle but very bad practice. Everybody should be aware of this gotcha and avoid it. Thanks Madhivanan for teaching this interesting lesson.

Republishing here with authorization of Just Learned.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Filed under: CodeProject, Pinal Dave, PostADay, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder http://blog.SQLAuthority.com
India India
Pinal Dave is a Microsoft Technology Evangelist (Database and BI). He has written over 2200 articles on the subject on his blog at http://blog.sqlauthority.com. Along with 8+ years of hands on experience he holds a Masters of Science degree and a number of certifications, including MCTS, MCDBA and MCAD (.NET). He is co-author of two SQL Server books - SQL Server Programming, SQL Wait Stats and SQL Server Interview Questions and Answers. Prior to joining Microsoft he was awarded Microsoft MVP award for three continuous years for his contribution in community.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Global Analyser9-Nov-12 5:06
Global Analyser9-Nov-12 5:06 
GeneralNot so black & white Pin
Wendelius10-Dec-11 8:21
mentorWendelius10-Dec-11 8:21 
SQL Server (as basically all of databases) has few mechanisms you can use in order to distinguish object names from keywords: "" and []. For example, if you want to create a table called Table you can do it either:
SQL
CREATE TABLE [Table] (
 col1 int
 );

SELECT * FROM [Table];

or
SQL
CREATE TABLE "Table" (
 col1 int
 );

SELECT * FROM "Table";

These mechanisms exists for a number of reasons. For example, it may make sense to create a table called User although it's a keyword if this table contains user information for your application. Another reason is that new keywords are added to the database with new releases. It doesn't make sense that existing object names are changed just because it conflicts with a keyword in future version.

These mechanisms provide a way to ensure that the object name is interpreted correctly even if it conflicts keywords.

I do agree that using keywords as object names without surrounding them with either "" or [] is a bad habit, but when the syntax is used properly it can definitely make sense to use even reserved words.
The need to optimize rises from a bad design.My articles[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.