Sunday, November 20, 2016

Is it possible to compare NULL value to NULL?

Quation:

SELECT  Case When NULL=NULL Then 1
                 ELSE 0 END as result

ANS: 0

my observation:

NULL means just nothing and NULL is not equal to zero or some other value or empty string. If you need to attach a specific meaning to NULL then missing or absent data. Nothing cannot be the same as nothing. Nothing cannot be compared to nothing. Because case condition fails then it will return 0.

Why it will be 0 ,if you have any ideas let me know 

Thursday, November 17, 2016

EOMONTH() New Date Function In 2012

it is the new date time function in sql server 2012 and and higher versions.It cannot be remoted to servers with a version lower than SQL Server 2012.
Definition:
    Returns the last day of the month that contains the specified date, with an optional offset.
syntax :
  EOMONTH ( start_date [, month_to_add ] )  
Examples:
DECLARE @date DATETIME = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO
    Result :   2011-12-31  
DECLARE @date VARCHAR(255) = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO
    Result : 2011-12-31 
DECLARE @date DATETIME ='2011-12-01';
SELECT EOMONTH ( @date ) AS 'This Month';
SELECT EOMONTH ( @date, 1 ) AS 'Next Month';
SELECT EOMONTH ( @date, -1 ) AS 'Last Month';
GO
result
-----------------------
2011-12-31
(1 row(s) affected)
Next Month
-----------------------
2012-01-31
(1 row(s) affected)
2011-11-30
Last Month
-----------------------
(1 row(s) affected)
--> By using EOMONTH function to easyly get perticuar month last date.
                                                                          థాంక్స్ ,
                                                                          బాబ్జి రెడ్డి జి వి

Wednesday, October 5, 2016

Avoiding Deadlock Transaction Errors by Using ROWLOCK Hint in T-SQL



  • When updating a single row data in a table, it may take a nasty while if the table is big and you have a condition in a WHERE clause on a column that is not indexed. In this case SQL Server will have to scan all rows in order to figure out which records to update. The update statement will lock the table and if other statements are executed at that time, you very well are likely to start getting deadlock errors.
  • To avoid this unfortunate scenario, you can use the ROWLOCK hint on your UPDATE queries. The hint will override locking the whole table and instead will only lock the row that is being updated. 
for example:

create table hints
    ( id int identity(1,1) primary key,
      name varchar(30),
      age int,
      valid_bit bit
    )


create proc get_hint_details
  (@age int)
As 
BEGIN 

update a
  valid_bit=0
  hints a where a.age=@age

END 
  • Adding an index on Age will certainly speed up things, but the whole table will still be locked while SQL Server looks for matching records, and if the table is big enough you are likely to still get deadlock errors. Instead of locking the whole table, you can hint the server to lock only those rows that match the criteria with ROWLOCK hint. This will allow your code to execute statements on rows that are not affected by the UPDATE query. However, if you are updating a large number of rows as in the example above assuming a billion records, you will probably still get deadlocks and may consider different approaches. To lock only the rows the query is affecting, the stored procedure above can be modified as follows:
create proc get_hint_details
  (@age int)
As 
BEGIN 

update a WITH(rowlock)
  valid_bit=0
  hints a where a.age=@age

END 

NOTE:
---> finally we  use this rowlock hint in both delete and update statements.for example if you use so many joins while delete and update table that time we should use rowlock hint to what table will be delete or update to the query and remaining tables use NOLOCK hint. 


Tuesday, September 20, 2016

New T-SQL features in SQL Server 2012 – OFFSET and FETCH clause

The OFFSET-FETCH clause provides you with an option to fetch only a window or page of results from the result set.

Note:OFFSET-FETCH can be used only with the ORDER BY clause.

Syntax:  
    
syntax for exclude n number of row from the result set:

                SELECT column_names 
                FROM table_name
               ORDER BY column_names
               OFFSET n ROWS

Syntax for exclude first n records and return only the next m records:

                 SELECT column_names 
                FROM table_name
               ORDER BY column_names
               OFFSET n ROWS
               FETCH NEXT m ROWS ONLY

For examples: use emp table to excluded first 2 sorted employee number getting remaining row 

             SELECT * FROM EMP
             ORDER BY empno
             OFFSET 2 ROWS 

example 2: exclude first 2 records and return only the next 3 records

             SELECT * FROM EMP
             ORDER BY empno
             OFFSET 2 ROWS 
             FETCH NEXT 3 ROWS ONLY


Thursday, September 8, 2016

Check and Create a Folder Using T-SQL Code

> Check if Folder Exist or Not in our Desired Location Using System Stored Procedure
"master.dbo.xp_fileexist" And if not exists create Directoty Using Below T-SQL Code:

declare @chkdirectory as nvarchar(4000)
    declare @folder_exists as int     
    set @chkdirectory = 'C:\SQLDBPool\SQL\Articles' 
    declare @file_results table
    (file_exists int,
    file_is_a_directory int,
    parent_directory_exists int
    ) 
    insert into @file_results
    (file_exists, file_is_a_directory, parent_directory_exists)
    exec master.dbo.xp_fileexist @chkdirectory
     
    select @folder_exists = file_is_a_directory
    from @file_results     
    --script to create directory       
    if @folder_exists = 0
     begin
        print 'Directory is not exists, creating new one'
        EXECUTE master.dbo.xp_create_subdir @chkdirectory
        print @chkdirectory +  'created on' + @@servername
     end       
    else
    print 'Directory already exists'
GO