Skip to main content

Posts

Showing posts with the label Delete duplicate rows in SQL Server

What is @@ERROR in SQL? When we should use @@ERROR?

What is @@ERROR in SQL? @@ERROR returns only current error information (error number and error) after T-SQL statements executed. @@ERROR returns 0, if the previous SQL statement has no errors otherwise return 1. @@ERROR is used in basic error handling in SQL Server and @@ERROR is a global variable of SQL and this @@ERROR variable automatically handle by SQL. If error is occurred set error number otherwise reset 0. It is work only within the current scope and also contains the result for the last operation only. Syntax : - @@ERROR   Return Type : - INT When we should use @@ERROR? 1.       While executing any stored procedures 2.       In the SQL statements like Select, Insert, Delete and Update etc. 3.       In the Open, Fetch Cursor. When we should use Try Catch Block? The Try Catch Block is generally used where want to catch errors for multiple SQL statements. ...

What is Nonclustered Index? How to create Nonclustered Index?

What is Nonclustered Index? Non-Clustered Index, 1.       Non-clustered indexes  can be used more than one time per table. 2.       Non-clustered indexes store logical structure but clustered indexes store in physical order. 3.       Faster for insert and update operations than a clustered index. 4.       Improve the performance when select data with index fields. 5.       Non-clustered indexes are stored separately. 6.       We can add only 249 non-clustered indexes for a table. 7.       Non-clustered indexes made on the any key but clustered indexes only on primary keys. How to create Nonclustered Index? -- CREATE NONCLUSTERED INDEX CREATE NONCLUSTERED INDEX Indexname_Employee ON Employee (     [EmpName] ASC --OR DESC,     [EmpDepartment] ...

What is Index? How do database indexes work? How do indexes help, Types?

What is an index in SQL? An index is created in a table to increase the performance of queries and the data pages are stored contiguously when the index is created and when the index is new-built. Index allows us to retrieve very fast data from the database and allow us to searching millions of records quickly. How do database indexes work? There are some strategies that make indexes work, 1.       Optimize your code. 2.       Restructure your data. 3.       Compress your data. 4.       Materialize them. 5.       Redundancy How do indexes help? For Example, suppose is a student and studying a book and this book contains 10,000 pages. In the first day I read some topic “abc” and next day I want to read some another topic “pqr”. I will never manually go through page by page.  It is very difficult to go there. In this situation, I a...

What is SQL view? Why use View instead of a Table? Advantages and Drawbacks

What is SQL view? The View is a virtual table, which not create physically, it create logically only. We insert, update & delete the records from a view. In view there are no any physical relations in the database and the view is a virtual table based on the result set of an SQL statement. Why use View instead of a Table? A table contains data but the View dose not stored set of data values in a database and the view is only a SELECT statement. A view can combine columns/rows from multiple tables. Views are work like a security layers and we can handle security issues. Views can be used as security mechanisms and its display only those data that you granting the permissions in this view. Advantages of View:- We can hide so of table columns. Views can model complex joins easily. Views are work like a security layers and we can handle security issues. Drawbacks of View: - When table is dropped or modified, view becomes inactive; i...

Delegates in C# Example

The basic examples of Delegates using in C# .Net as given below, using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace delegateExample { public delegate int AddByDelegate ( int var1, int var2); public class DelegateClass { public static int Add ( int a, int b) { return a + b; } static void Main ( string [] args) { //Creating the Delegate Instance AddByDelegate delObject = new AddByDelegate ( Add ); Console. Write ( "Please enter value" ); int num1 = Int32 . Parse (Console. ReadLine ()); int num2 = Int32 . Parse (Console. ReadLine ()); // Here method Add is call . int Result = delObject (num1, num2); Console. WriteLine ( "Result :" + Result); Console. ReadLine (); } } } //The Delegate - OutPut Result : 30

SQL Server AND keyword

The AND keyword is logical operator. It is used to filter table records with AND conditions. Syntax:  SELECT col1, col2 FROM table_name WHERE Condition_1 AND Condition_2 -- CREATE TABLE AND INSERT ROWS CREATE TABLE [dbo].[Tbl_Demo]( [ID] [ int ] IDENTITY( 1 , 1 ) NOT NULL , [Name] [ varchar ]( 500 ) NULL , [Age] [ int ] NULL , [IsActive] [ bit ] NULL , [IsDeleted] [ bit ] NULL , CONSTRAINT [PK_Tbl_Demo] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON , ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO SET IDENTITY_INSERT [dbo].[Tbl_Demo] ON GO INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES ( 1 , N 'Anil Singh' , 30 , 1 , 0 ) GO INSERT [dbo].[Tbl_Demo] ([ID], [Name], [Age], [IsActive], [IsDeleted]) VALUES ( 2 , N 'Aradhya' , 3 , 1 , 0 ) GO INSERT [dbo].[Tbl_Demo] ([ID...

Delete duplicate rows in SQL Server

Hello everyone, I am going to share the T-SQL query for delete duplicate rows using SQL Server 2012 and you can see the query detail as below. T-SQL Query for delete duplicate rows WITH mobile AS (     SELECT [Account] , [AlertToMobile] ,      ROW_NUMBER () OVER ( PARTITION BY [Account] , [AlertToMobile]                  ORDER BY [AlertToMobile] ) AS [count]     FROM [dbo] . [Mobile] ) DELETE mobile WHERE [count] > 1 The query result as given below                                                             OR We have another ways to delete duplicate rows using SQL Server as given below.                    ...