Skip to main content


Showing posts from August, 2013

Add minute, hour, day, month in date in sql server

By using DATEADD(<interval>,<increment int>,<expression smalldatetime>) function we can add or reduce hour, day etc.. For increment or decrement from the datetime the query are given below. For add minute DATEADD(MINUTE,20,GETDATE()) For reduce minute DATEADD(MINUTE,-20,GETDATE()) For add hour DATEADD(HOUR,20,GETDATE()) For reduce hour DATEADD(HOUR,-20,GETDATE()) For add day DATEADD(DAY,20,GETDATE()) For reduce day DATEADD(DAY,-20,GETDATE()) For add month   DATEADD(MONTH,20,GETDATE()) For reduce month DATEADD(MONTH,-20,GETDATE()) For add year   DATEADD(YEAR,20,GETDATE()) For reduce year  DATEADD(YEAR,-20,GETDATE())

TRY/CATCH and error handling in SQL server 2005/2008

SQL Server 2005 provide a strong set of tool to handle error or exception, in privious version it was not easy to handle exceptions. We use TRY/CATCH in SQL Server same as populer languages, We write saperate logic and exception handling code. Syntax: BEGIN TRY -- Your Code or query END TRY BEGIN CATCH -- Handling code END CATCH We write our logic code within TRy bolck and exception code in CATCh block, if nay error occur in logic code it jumps to CATCH  block, resume the execution and transaction is rollback. Functions which is use in CATCH 1- ERROR_NUMBER() Returns error no othe error message. 2- ERROR_SEVERITY() returns the error severity regardless of how many times it is run. 3- ERROR_STATE() Returns the state number of error messages that cause the catch block. 4- ERROR_PROCEDURE() Return the Procedure Name. 5- ERROR_LINE() Return error line number. 6- ERROR_MESSAGE() return error description. Note : If we are calling all above method out side from C

MERGE in SQL SERVER -INSERT, UPDATE, DELETE in single execution.

To do multiple DML operations in SQL SERVER we use MERGE, that a strong feature of SQL SERVER, In earlier versions of SQL SERVER we had to write separate statement for INSERT, UPDATE, DELETE, now by using MERGE we can perform INSERT, UPDATE, DELETE in one statement by checking data, if data matched then update otherwise insert or in particular condition we can also delete. Note-Merge statement read entire data and processed only one. The Merge query is given below: Suppose there are two tables, Table1 and table2, table1 contains data about books like ISBN, Title, quantity, price, etc.. and table2 contains updated data of book so here we need to update Table1 from Table2, Table may have new records or updated records(like quantity, price etc ) or it may be some records should be delete from Table1. CREATE   TABLE TABLE1 ( biBookId BIGINT IDENTITY ( 1 , 1 ), vIsbn VARCHAR ( 20 ), iQty INT , dcPrice DECIMAL ( 18 , 2 ) ) CREATE   TABLE TABLE2 ( biBookId BIGI

Bulk Update in sql server 2008..

/*Bulk update query*/ create table Table1 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) create table Table2 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) /*Here create two tables, we update Table1's dcPrice column from Table 2 Using Bulk update*/     insert into Table1 ( vIsbn , dcPrice ) values ( '9087657654312' , 0.00 ) insert into Table1 ( vIsbn , dcPrice ) values ( '9989876765454' , 0.00 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9087657654312' , 10.20 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9989876765454' , 20.10 ) select * from Table1 /*here bulk update Query*/ UPDATE Tbl SET dcPrice = Tbl2 . dcPrice FROM Table1 Tbl JOIN Table2 AS Tbl2 ON Tbl . vIsbn = Tbl2 . vIsbn select * from Table1 drop table Table1 dr