Solved

Best code for creating a new column

Posted on 2016-09-06
3
42 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
3 Comments
 
LVL 24

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 48

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
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 shrink a transaction log file down to a reasonable size.

839 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