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 ?

string
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) 


SELECT
'1','Test1'


Union all


SELECT
'2','Test2'

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.

https://www.google.co.in/#hl=en&sclient=psy-ab&q=transactions+in+.net&oq=transactions+in+.net&aq=f&aqi=g3g-m1&aql=&gs_l=hp.3..0l3j0i5.397480l400969l6l401171l21l13l1l7l7l1l258l2417l0j9j4l20l0.frgbld.&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=654647455d09614b&biw=1170&bih=645
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) )
AS 
Begin 

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

end

TRy this and let me know.