Solved

Best code for creating a new column

Posted on 2016-09-06
3
44 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 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 49

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql query help 15 55
SQL works but want to get the XML node data separately 11 30
Regarding Disk IO 3 49
SQL Server Express automatically execute SQL or SP 8 37
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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