Posts

Showing posts from June, 2013

Update a column with values which its parent have.

Say we have a Table (Product) ProductID, ProductName, IntParentID, IntCategoryID. With the script below the intCategoryid of Parent  will become the intCategoryID of Child. ;with cte as ( select intCategoryId, intProductID from Product where intCategoryId is not null and intProductID is not null union all select cte.intCategoryId, t.intProductID from Product t join cte on t.intParentCategory = cte.intProductID and t.intCategoryId is null ) update t set t.intCategoryId = cte.intCategoryId from Product t join cte on t.intProductID = cte.intProductID option (maxrecursion 5000)

Delete Duplicate Records from Table

This code is copied from the link mentioned below http://www.codeproject.com/Articles/25622/Sql-Queries-most-asked-in-job-interviews Let the table employee_test1 contain some duplicate data like:- CREATE TABLE Employee_Test1 ( Emp_ID INT, Emp_name Varchar( 100 ), Emp_Sal Decimal ( 10 , 2 ) ) INSERT INTO Employee_Test1 VALUES ( 1 , 'Anees' , 1000 ); INSERT INTO Employee_Test1 VALUES ( 2 , 'Rick' , 1200 ); INSERT INTO Employee_Test1 VALUES ( 3 , 'John' , 1100 ); INSERT INTO Employee_Test1 VALUES ( 4 , 'Stephen' , 1300 ); INSERT INTO Employee_Test1 VALUES ( 5 , 'Maria' , 1400 ); INSERT INTO Employee_Test1 VALUES ( 6 , 'Tim' , 1150 ); INSERT INTO Employee_Test1 VALUES ( 6 , 'Tim' , 1150 ); Step 1:  Create a temporary table from the main table as:- e select top 0* into employee_test1_temp from employee_test1 Step2 :  Insert the result of the GROUP BY query into the temporary tabl...