1
Answer

SQL Query to Restrict Entry of Same Data in a Table

Bineesh  VP

Bineesh VP

11y
793
1
Sir, I need your help in the following:-

I want SQL Query to restrict same data entry. Please take a look to the following Row:-


id                Name                Address                                                     Age                        Place            Department          Salary 

1                 Sam                3rd street, madgaon, Chennai                        24                      Chennai              Sales                20000

2                 Sam                3rd street, madgaon, Chennai                        24                      Chennai              Sales                20000


This type of Entry does not allowed.

I need a different query than the Query I written below:-

if not exists(select Name, Address, Age, Place, Department, Salary from tbl_Employee where Name=@Name and Address=@Address and Age=@Age and Place=@Place and Department=@Department and Salary=@Salary)

begin
insert into tbl_Employee(Name, Address, Age, Place, Department, Salary)
values(@Name, @Address, @Age, @Place, @Department, @Salary)
end

 
Answers (1)
0
Vikram N
NA 66 14.7k 11y
thank u Sanjeeb...Lemme try it out...:)
0
Sanjeeb Lenka
NA 22.1k 1.3m 11y
check this code:

public int SaveEmp(Emp objEmp)
{
using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"))
{
 string query = "Emplyentry";  //stored procedure name
using (SqlCommand cmd = new SqlCommand(query , con))
{
cmd.Parameters.AddWithValue("@EmpName", objEmp.EmpName);
cmd.Parameters.AddWithValue("@EmpSal", objEmp.EmpSal);
con.Open();
int a = cmd.ExecuteNonQuery();
con.Close();
return a;
}

}
}
0
Vikram N
NA 66 14.7k 11y
Thanx a lot for ur kind reply Sanjeeb,That linkhelped me a lot...
But in dat example, database tables are used... But i need stored procedures usage... kindly help:)