Suppose the records in database are as below:
Id FirstName LastName OrderPrice
1 Tom Hanks 123
2 Tom Hanks 173
3 Tom Hanks 123
4 Tom Bob 123
5 Robert Hanks 123
Given Input : Id = 1
Expected OUtput : Id = 3
I know that I can write some thing like below
Declare @Id as INT = 1
Declare @FirstName as varchar(100)
Declare @LastName as varchar(100)
Declare @OrderPrice as INT
Select @FirstName = FirstName, @LastName = LastName , @OrderPrice = OrderPrice From Customers Where Id= @ID
Select ID From Customers Where FirstName = @FirstName and LastName = @LastName and OrderPrice = @OrderPrice
But, I do not want to write some thing like where I should mention all the columns names in where condition. Because actually the record I was working on contains almost 100 columns in it. So, I was asked not to use a query some thing like this.
Can any one help me how to do this?
You can use dynamic SQL to create a query to search for duplicate records. Basically, this query builds a string like
a.col1 = b.col1 and a.col2 = b.col2 and ...and uses that as theonclause in a self-join:This searches for rows that duplicate
ID = 1, except for the ID.