Introduction:
In this article I will explain how to write SQL Query to add new column to existing table in sql server and query to delete column from table in sql and query to modify column in existing table.
In this article I will explain how to write SQL Query to add new column to existing table in sql server and query to delete column from table in sql and query to modify column in existing table.
Description:
In
previous posts I explained many articles relating to SQL
Server. Now I will explain how to write query to add, remove or modify
column in existing table using SQL Server. During work with one application I
got requirement like add new column to existing table in SQL Server at that
time I realized that it’s better to write query to add new column instead of
open table structure and modify to add new column in SQL Server.
To add new column to existing table in SQL server the declaration of system
will be like this
Query
to add new column to table
If
we want to add new column to existing table that syntax will be like this
ALTER TABLE Table_Name
ADD COLUMN_NAME DATATYPE
|
Ex:
ALTER TABLE
emp_table ADD Manager_Name VARCHAR(50)
|
From
the above query declaration new column “Manager_Name”
will add to emp_table with data type
VARCHAR(50)
Query
to Drop or delete column from existing table
If
we want to drop column from existing table that syntax will be like this
ALTER TABLE
Table_Name DROP COLUMN
COLUMN_NAME
|
Ex:
ALTER TABLE
emp_table DROP COLUMN
Manager_Name
|
From
the above query declaration column “Manager_Name”
will drop from emp_table
Query
to modify existing column datatype in table
If
we want to modify existing column datatype from data table that syntax will be
like this
ALTER TABLE
Table_Name ALTER COLUMN
COLUMN_NAME DATATYPE
|
Ex:
ALTER TABLE
emp_table ALTER COLUMN
Manager_Name nvarchar(max)
|
From
the above query declaration I changed “Manager_Name”
column datatype from VARCHAR(50) to nvarchar(max)
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
7 comments :
hello , i have a doubt , see my requirement is to add a new column to an existing table such tat the column name should be automatically given the name of the variable . i wrote the query as
alter table test add'+@count+' int. , but i am getting error any idea how to solve this , waiting for your reply
please write a post on javascript events in my system have firefox 13.0 version it is not firing on key press event i want to validate against numarics
iam using 3rd party controls dbnetsuite please can you help as it is urgent requirement for me please suresh..................?
please write a post on javascript events in my system have firefox 13.0 version it is not firing on key press event i want to validate against numarics
hi
nice
alter table test add'+@count+' int
exec('alter table test add ['+@count+'] int')
Note: Only a member of this blog may post a comment.