473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

to check from table1 column if the row has value it will update to table2 else it wi

Expand|Select|Wrap|Line Numbers
  1. table1---- Id name adviceno chequeno
  2.             11 abc 1         null
  3.             22 bcx 2         null
  4.             31 tex null      A2093
  5.             54 yrt 3         null
  6.             52 ths null      A3744
  7.  
  8. table2--- Id name  alldetails_cheque
  9.           11 abc     null  
  10.           22 bcx     null  
  11.           31 tex     null   
  12.           54 yrt     null   
  13.           52 ths     null   
i need to update from table1 to table2
table1.Id=table2.Id
Table1.name=table2.name
if advice is not null then
update advice number
else addice number will be null and chequeno will update

output:

Expand|Select|Wrap|Line Numbers
  1. table2---- Id name  alldetails_cheque (after update)
  2.             11 abc    1
  3.             22 bcx    2
  4.             31 tex   A2093
  5.             54 yrt    3
  6.             52 ths   A3744
SQL:
1.
Expand|Select|Wrap|Line Numbers
  1. update table2 
  2. set t2.alldetails_cheque=t1.adviceno 
  3. from table1 as t1 and table2 as t2
  4. on t1.id=t2.id and t1.name=t2.name 
  5. where chequeno is null
2.
Expand|Select|Wrap|Line Numbers
  1. update table2 
  2. set t2.alldetails_cheque=t1.chequeno 
  3. from table1 as t1 and table2 as t2
  4. on t1.id=t2.id and t1.name=t2.name 
  5. where adviceno is null

when this query is used then both is updating..


can you help me with this.

I need to do this in Asp.net(CSharp). If adviceno is has value it will update in table2 or chequeno will update in table2 alldetails_cheque
using asp.net




Expand|Select|Wrap|Line Numbers
  1. protected void Button7_Click1(object sender, EventArgs e)
  2. {
  3.  
  4. DataTable dt = new DataTable();
  5. string query = "select * from table1";
  6. using (SqlConnection con = new SqlConnection(con_str))
  7. {
  8. using (SqlCommand cmd = new SqlCommand(query))
  9. {
  10. using (SqlDataAdapter da = new SqlDataAdapter())
  11. {
  12. cmd.CommandType = CommandType.Text;
  13. cmd.Connection = con;
  14. da.SelectCommand = cmd;
  15. da.Fill(dt);
  16. }
  17. }
  18. }
  19.  
  20. try
  21. {
  22.  
  23. string query2;
  24. // string query3;
  25. using (SqlConnection con = new SqlConnection(con_str))
  26. {
  27. con.Open();
  28.  
  29. foreach (DataRow row in dt.Rows)
  30. {
  31. string str = row["AdviceNo"].ToString();
  32.  
  33.  
  34. //The conditions 
  35.  
  36. if (str.Equals(null))
  37. {
  38.  
  39.  
  40.  
  41. query2 = "update table2 set t2.alldetails_cheque=t1.adviceno from table1 as t1 and table2 as t2
  42. on t1.id=t2.id and t1.name=t2.name where chequeno is null";
  43. SqlCommand cmd2 = new SqlCommand(query2, con);
  44. cmd2.ExecuteNonQuery();
  45. }
  46. else 
  47. {
  48. query2 = "update table2 set t2.alldetails_cheque=t1.adviceno from table1 as t1 and table2 as t2
  49. on t1.id=t2.id and t1.name=t2.name where chequeno is null ";
  50. SqlCommand cmd2 = new SqlCommand(query2, con);
  51. cmd2.ExecuteNonQuery();
  52.  
  53.  
  54. }
  55.  
  56. }
  57.  
  58. con.Close();
  59. }
  60.  
  61. }
  62. catch (Exception ex)
  63. {
  64. Response.Write(ex.Message);
  65.  
  66. }
  67. finally
  68. {
  69.  
  70. Label2.Text = "updated";
  71. }

i have tried this code.. but it is updating only the adviceno in alldetails_cheque

i want to update alldetails_cheque from chequeno
Sep 9 '14 #1
0 1012

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mark Donners | last post by:
Table1 and table 2 are in the same access database. when i press a button(script) all records from table1 should be added to table2. How can i do this? Who can help m donners
0
by: Amber | last post by:
There are times when you will need to highlight or otherwise modify the contents of a particular DataGrid row-column value based upon the value in the column. In this example we will select the...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
3
by: shobu | last post by:
passing array checkbox value and update the database <?include 'dbconnect.php'; error_reporting(0);$update_qr="update...
3
by: Manikandan | last post by:
Hi, I have table with three columns as below table name:exp No(int) name(char) refno(int) I have data as below No name refno 1 a 2 b 3 c
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
3
by: hauschild | last post by:
Guys, I am looping thru a dataset and need to update rows' columns based on the ColumnName value. I get this far but I'm unsure of how to update that actual columns value with the new value. ...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
2
by: soniachouhan | last post by:
how to i check database null column value and wanna perorm some action in both if else part according to the database value.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.