Nifty SQL Server SQL Statements

Most of the time I find myself forgetting some simple syntax for SQL statement and I spend some time googling the correct syntax. So I thought why go google when I can just put a post in the site and keep the link handy. Here goes the first syntax

Change a column size

ALTER TABLE ALTER COLUMN

If there is a table called student and it has a column called firstname with data type as varchar(100). If we we want to change the size of the specific column then the above syntax will translate into code as

ALTER TABLE Student ALTER COLUMN FirstName VARCHAR(300) NOT NULL

Change a column name


Use
GO

sp_RENAME 'TableName.CurrentColumnName' , 'NewColumnName','COLUMN'
GO

The system stored procedure sp_rename will get the job of renaming a column done for you. The syntax will translate into code as

sp_RENAME 'Student.FirstName' , 'GivenName','COLUMN'

I will keep updating this post with more SQL statements as I keep digging more.

Leave a Reply

Your email address will not be published. Required fields are marked *