Tuesday, 4 October 2011

What is the difference between select count(*) and select count(any_column)?

  • COUNT(*) will include NULLS
  • COUNT(column_name) will not include NULLS.
Example :
create table myTable (id int,name nvarchar(100))
insert into myTable values(1,null)
insert into myTable values(null,'Hello')
insert into myTable values(null,null)

select COUNT(*) from myTable
Result : 3
select COUNT(name) from myTable
Result : 1

Sunday, 2 October 2011

Rename the Table/View/SP in SQL Server

sp_rename is used to rename the sql objects.
Syntax :
sp_rename 'Old Object Name','New Object Name'

sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the primary key is also automatically renamed by sp_rename.

Renaming a stored procedure, view or trigger will not change the name of the corresponding object name in the syscomments table. This may result in problems generating a script for the object as the old name will be inserted from the syscomments table into the CREATE statement. For best results, do not rename these object types. Instead, drop and re-create the object by its new name.
Note : sp_rename is not check  the object dependency.