Tuesday, December 31, 2013

Rename SQL server DB - tables

Sol1 : just click on the table in Object explorer, hit F2 and rename it

Sol 2 :
EXECUTE sp_rename 'oldTableName' , 'newTableName'
EXECUTE sp_rename 'tb1' , 'tb2'
It really work try it.

Wednesday, November 13, 2013

Modifying SQL Server Tables

 Adding Columns to a Table

Syntax : ALTER TABLE table-name ADD column-specification
Ex : ALTER TABLE empolyee ADD JobDescription VARCHAR(500)

 Modifying Existing Columns


ALTER TABLE employee ALTER COLUMN EmpName VARCHAR(30) NOT NULL



ALTER TABLE employee ALTER COLUMN EmpName VARCHAR(30) NOT NULL



ALTER TABLE employee ALTER COLUMN address2 VARCHAR(200) NULL

 Deleting Columns


ALTER TABLE empolyee DROP COLUMN address3






   

Sunday, August 25, 2013

Rename a SQL Server database





 Note: If not done. Change user type muti-user to single user


This command works for SQL Server 2005, 2008, 2008R2 and 2012:
ALTER DATABASE oldName MODIFY NAME = newName
If you are using SQL Server 2000 you can use this T-SQL command to make the database name change.  This still works for SQL 2005, 2008, 2008R2 and 2012, but Microsoft says it will be phased out at some time.
EXEC sp_renamedb 'oldName', 'newName'
 

Saturday, August 24, 2013

tsql : Split row based on row number , ROW_NUMBER(),RANK(),



select * from  (select
 F=cast((RowNo/100) as int)+1,F1,F2 from (
               SELECT ROW_NUMBER() OVER(ORDER BY f2) RowNo,F1,F2 FROM TempTable where F2 IS NOT NULL
               ) sub1) AS T WHERE T.F=1

Monday, August 19, 2013

Drop All Stored procedures,views and functions - GET Objects from sys.objects


 GET Objects from sys.objects
--select *from sys.objects where type in('P','V','FN') order by type


--Query for stored procedure
select ('DROP PROCEDURE ' + NAME),*from sys.objects where type in('P') order by type

--Query for view
select ('DROP VIEW' + NAME),*from sys.objects where type in('V') order by type

--Query for FUNCTIONS
select ('DROP FUNCTION' + NAME),*from sys.objects where type in('FN') order by type