Rename Table In SQL Server

How to Rename table In SQL Server 

 

       In SQL Server there no direct query to rename table. It can be possible using built in procedure in SQL server.

The following syntax help you to raname a table using procedure.

      EXEC sp_rename 'old_table_name', 'new_table_name' 


Rename table Using SSMS

     Another way to rename a table is Rght Click the table from  SQL Server Management Studio.

 

 

 

Using Calculated Column In SQL Server


 
     Calculated columns can add great flexibility in database design .We can apply formula while creating tables to get computed values.below example help you to create computed column in sqlsever.

1. Create a temp table with calculated columns. Here column Total is the calculated one. It is populated sum of Mark1 and Mark2

CREATE TABLE #TempCalculatedTable(
[ID] [int] NULL,
[Name] [varchar](50) NULL,
[Mark1] [int] NULL,
[Mark2] [int] NULL,
[Total] AS (ISNULL([Mark1],0)+ISNULL([Mark2],0)) /*Computed Column*/
)

2. Inserting data to temp table. Here we are inserting ID, Name, Mark1,Mark2 to temp table.

INSERT INTO #TempCalculatedTable
SELECT 1,'Jack',1000,2000
UNION ALL
SELECT 2,'Martin',2000,3000
UNION ALL
SELECT 3,'Stella',2000,3000
UNION ALL
SELECT 3,'Stella',NULL,3000
UNION ALL
SELECT 3,'Stella',NULL,NULL

3. Selecting data from temp Table. Here we can see calculated column Total populated automatically against each row.

SELECT *
FROM #TempCalculatedTable






Using iif in SQL server

IIF() is the new built-in logical function introduced as a Part of Sql Server 2012 .we can replace CASE and IF...ELSE statement using IIF().
Syntax: IIF ( boolean_expression, true, false )
If Boolean expression evaluates to TRUE then Second argument will be the result otherwise Third argument will be the result
/*Checking MALE or Female Using Case */

DECLARE @Gender INT = 1

SELECT CASE WHEN @Gender = 1 THEN 'MALE' ELSE 'FEMALE' END AS Gender

Result : MALE


/*Checking MALE or Female Using IF ELSE */

DECLARE @Gender INT = 1

IF(@Gender = 1)
   BEGIN
    SELECT 'Male' Gender
   END
ELSE
   BEGIN
    SELECT 'Female' Gender
   END

Result : MALE

/*Checking MALE or Female Using IIF */

DECLARE @Gender INT = 1

SELECT iif(@Gender = 1, 'MALE','FEMALE') AS Gender

Result : MALE

/*Checking Multivalue using IIF */

DECLARE @NAME Varchar(50) = 'SQL1'

SELECT IIF(@NAME IN ('SQL','ORACLE'),
'DATABASE', 'NOTE IN LIST' )

Result : NOTE IN LIST