Click here to Skip to main content
15,882,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
cmd.CommandText = "insert into " + table + " values ('" + textBox1.Text + "', '" + textBox2.Text + "') where not exists(select * from " + table + " where character='" + textBox1.Text + "' and entity='" + textBox2.Text + "');";


i want to insert unique data in my table and i'm getting error in this query (Incorrect syntax near where)...
Posted

T-SQL does not allow for a where clause in that place. See here: http://msdn.microsoft.com/de-de/library/ms174335.aspx[^]

Cheers!
 
Share this answer
 
Comments
ShobuGpta 3-Jun-14 4:17am    
I dint get it
ShobuGpta 3-Jun-14 4:27am    
i checked the NOT EXISTS condition first and it's working
Hi,

Try following query.

C#
cmd.CommandText = "if not exists (select * from " + table + " where character='" + textBox1.Text + "' and entity='" + textBox2.Text + "') insert into " + table + " values ('" + textBox1.Text + "', '" + textBox2.Text + "') where not exists";



Thanks,
Bhagyesh
 
Share this answer
 
Comments
ShobuGpta 3-Jun-14 7:30am    
yeah thnx...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900