Solved

Best code for creating a new column

Posted on 2016-09-06
3
46 Views
Last Modified: 2016-09-08
I have two different blocks of sql code which basically perform the same functionality. One queries SYS.COLUMNS and the other queries
INFORMATION_SCHEMA.COLUMNS.
Which query is better to use, #1 or #2 ? Also ignore the fact that the second query uses parameters for the table name and column name instead of the hard coding which occurs in #1.


(1)
IF NOT EXISTS (SELECT * FROM sys.columns WHERE Name = N'ROTOCOST' AND Object_ID = Object_ID())
                  BEGIN
                        EXEC('ALTER TABLE CUSTOMERS ADD ROTOCOST CHAR(9)')
                  END




(2)
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = @NewColName AND TABLE_NAME =  @TableName)
                        BEGIN
                              SET @sql = 'ALTER TABLE ' + @TableName + ' ADD ' +  @NewColName + ' ' + ' CHAR(9) '
                              EXEC(@sql)
                        END
0
Comment
Question by:brgdotnet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 250 total points
ID: 41787215
INFORAMTION_SCHEMA is an ANSI standard. Theoretically when you use it it should work for any RDBMS types. However, also because of this it has an extra overhead. The ANSI standard may require to include some columns related to the data type, indexes and other stuff that will require the vendors to join multiple tables. Therefore the INFORMATION_SCHEMA views may perform worse comparing to the SYS views

If you are not planning to use any other database types then I suggest you stick to the SYS views.

On a side note use IF NOT EXISTS (SELECT 1 FROM...) instead of SELECT * FROM. There is no need to select all columns when you checking for the fact if records exist.
0
 
LVL 50

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 41787456
Just went around reading in MSDN to know what Microsoft says and looks like there are some warnings about INFORMATION_SCHEMA so I would go sys.columns instead.

Important
Do not use INFORMATION_SCHEMA views to determine the schema of an object. The only reliable way to find the schema of a object is to query the sys.objects catalog view.
0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 41789566
Thank you.
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question