Solved

Best code for creating a new column

Posted on 2016-09-06
3
41 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 47

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 66
kill process lock Sql server 9 46
sql 2014,  lock limit 5 29
ms sql + get number in list out of total 7 26
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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

785 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