Sunday, 25 May 2014

How to handle erros in T-SQL code ?

From SQL Server 2005 introduced a TRY ... CATCH statements that you can use to provide error handling.
  • A TRAY block must be followed immediately by a single CATCH block
  • When an error occurs in a statement within a TRY block, control is passed to the CATCH block where the error can be processed. If no error occurs inside the TRY block, the CATCH  block is skipped.
  • Within a CATCH  block, you can use the functions shown in this figure to return data about the error that caused the CATCH block to be executed.
Syntax:
BEGIN TRY
{sql_statement / statement block}
END TRY
BEGIN CATCH
{sql_statement / statement block}
END CATCH

How to test the existence of a database object ?



OBJECT_ID  is used to check the existence of the table and DB_ID is used to check the existence of the Database.

Syntax of the OBJECT_ID function

<pre class="brush:sql;">

OBJECT_ID(‘object/table’)

</pre> 


Syntax of the DB_ID function

<pre class="brush:sql;">

DB_ID(‘database’)

</pre> 

EXAMPLES:

  • Code that tests whether a database exists before it deletes it
<pre class="brush:sql;">

IF DB_ID(‘Test_DB’) IS NOT NULL

DROP DATABASE Test_DB

CREATE DATABASE Test_DB

</pre>

  • Code that tests for the existence of the talbe
<pre class="brush:sql;">

IF OBJECT_ID(‘Test_Table’) IS NOT NULL

DROP TABLE Test_Table

</pre>

  • Another way to test the existence of a table
<pre class="brush:sql;">

IF EXISTS ( SELECT * FROM sys.tables where name = ‘Test_Table’)

DROP TABLE Test_Table

</pre>

  • Code that tests for the existence of the temp table
<pre class="brush:sql;">

IF OBJECT_ID(‘tempdb..#temptable_name’) IS NOT NULL

DROP TABLE #temptable_name

</pre>

 

Saturday, 24 May 2014

Types of Transact-SQL Table Objects

There are Five Types of Transact-SQL Table Objects
#
Type
Scope
1
Standard Table
Available within the system until explicitly deleted
2
Temporary Table
Available within the system while the current database session is open
3
Table Variable
Available within a script while the current batch is executing
4
Derived Table
Available within a statement while the current statement is executing
5
View
Available within the system until explicitly deleted

Note: 
  • Derived Tables are select Statements in from clause, referred by an alias name.
  • CTE (Common table expression) is a type of derived table.

Sunday, 4 May 2014

Diffence between Table Variable and Temp Table



#.
Table Variable
Temp Table
1
It stored in primary memory(ie. in RAM)
It stored in system database(ie. In msdb)
2
It has batch scope only, ie. Till the current t-sql stmt to be execute
It has the scope till the query window close, In case of local temp table and has the scope till all the SSMS query windows close in case of global temp table
3
Table variables can’t use within select into clause
Temp tables can be used within select into clause
4
Table variables gives better performance than temp table, because they stores in primary memory (ie. In RAM)
Temp tables not gives better performance than table variables, because it stores in hard disk
5
We can’t explicitly drop the table variables
We can explicitly drop the temp tables