C# .NET - correct qry - Asked By Jahir on 09-Apr-12 06:41 AM

how to write this 2 qries into 1 qry ? in concat ?

qry = "insert into tbl_outcome(code,name,address,date,specification,prevamount,newamount,balance)values('" + lbl.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','"+textBox7.Text+"','"+textBox8.Text+"')";

string qry1 = "update tbl_income set amount='" + textBox8.Text + "' where name='" + textBox2.Text + "'";

dipa ahuja replied to Jahir on 09-Apr-12 06:51 AM
You can't.

You cannot combine two different types of query that is insert query and update query in one.
Somesh Yadav replied to Jahir on 09-Apr-12 06:52 AM
insert into table1(field1,field2) 


Union all


In SQL Server 2008, you can insert multiple records like this:

insert into table1(field1,field2) values('1','Test1'),('2','Test2')
Venkat K replied to Jahir on 09-Apr-12 12:17 PM
Why you want to do that? What is the neccessity for you to combone a insert and Update query?
Rather i think you need a transaction here to make it as a set.
Refere .Net transactions on how to make multiple statements as a transaction.

Jitendra Faye replied to Jahir on 10-Apr-12 02:49 AM
In single query it is not possible, but you can use stored procedure for this-

like this-
CREATE PROCEDURE InsertUpdate (@id int,@name varchar(50) )

if exist(select * from tablename where id=@id)
  update query
  Insert query
end if


TRy this and let me know.