How to use nested querieshttp://forums.asp.net/t/1759758.aspx/1?How+to+use+nested+queriesThu, 19 Jan 2012 01:31:42 -050017597584786770http://forums.asp.net/p/1759758/4786770.aspx/1?How+to+use+nested+queriesHow to use nested queries <p>I want to update [msg] and [friendid] field in friends table where friendid is in members table. How can I achieve this? I am trying to use nested query as:</p> <pre class="prettyprint">string sql = &quot;update friends set msg=@msg where uid=@uid and friendid IN (select id from members where email=@email)&quot;; using (OleDbConnection con = ConnectionManager.getConnection()) { OleDbCommand cmd = new OleDbCommand(sql, con); cmd.CommandType = System.Data.CommandType.Text; cmd.Parameters.AddWithValue(&quot;@msg&quot;, &quot;You have received a friend request from &quot; &#43; returnName(uid)); cmd.Parameters.AddWithValue(&quot;@uid&quot;, uid); cmd.Parameters.AddWithValue(&quot;@email&quot;, email); cmd.ExecuteNonQuery(); }</pre> <p>Even I don't know wheather I should use NonQuery for update or Scalar for select. Plz help me.</p> 2012-01-17T05:33:40-05:004786955http://forums.asp.net/p/1759758/4786955.aspx/1?Re+How+to+use+nested+queriesRe: How to use nested queries <p>Use ExecuteNonQuery()</p> 2012-01-17T07:05:09-05:004787144http://forums.asp.net/p/1759758/4787144.aspx/1?Re+How+to+use+nested+queriesRe: How to use nested queries <p>OleDb parameters are recognized by position, not by their name. So at first sight, you seem to add them in the right order. But the problem with subquerys is, that they&nbsp;will be processed first. So the correct order of the parameters is: email, msg, uid</p> <p>cmd.Parameters.AddWithValue(&quot;@email&quot;, email);<br> cmd.Parameters.AddWithValue(&quot;@msg&quot;, &quot;You have received a friend request from &quot; &#43; returnName(uid));<br> cmd.Parameters.AddWithValue(&quot;@uid&quot;, uid);</p> <p>&nbsp;</p> <p>And yes, ExecuteNonQuery is the right method</p> <p>By the way, is <span class="typ">ConnectionManager</span><span class="pun">.</span><span class="pln">getConnection</span><span class="pun">() returning an open connection? If not, you need to open the connection before calling ExecuteNonQuery</span><span class="pln">&nbsp;</span></p> 2012-01-17T08:36:55-05:004787614http://forums.asp.net/p/1759758/4787614.aspx/1?Re+How+to+use+nested+queriesRe: How to use nested queries <pre><blockquote><span class="icon-blockquote"></span><h4>siddharth_kcr</h4>want to update [msg] and [friendid] field in friends table where friendid is in members table</blockquote><br><br><br>Try the following query,<br><br><strong>update friends set msg = @msg ,friendID =members.id from (select @email as tmpEmail) as temptable</strong><br><br><strong>join members on temptable.tmpEmail = members.email where friends.uid = @uid</strong><br><br><br>I dont't find any meanig from the above operation becuase you mentioed,<br><br><br><blockquote><span class="icon-blockquote"></span><h4>siddharth_kcr</h4>&quot;@msg&quot;, &quot;You have received a friend request from &quot; &#43; returnName(uid)</blockquote><br><br>You have used &quot;uid&quot; as request sender but again updating the same user.. please check that too..<br><br>Regards,<br><strong>Nivash Ramachandran.</strong><br><br><br></pre> 2012-01-17T11:47:40-05:004787655http://forums.asp.net/p/1759758/4787655.aspx/1?Re+How+to+use+nested+queriesRe: How to use nested queries <p></p> <blockquote><span class="icon-blockquote"></span> <h4>rnivash</h4> Try the following query,<br> <br> <strong>update friends set msg = @msg ,friendID =members.id from (select @email as tmpEmail) as temptable</strong><br> <br> <strong>join members on temptable.tmpEmail = members.email where friends.uid = @uid</strong><br> </blockquote> <p></p> <p>Do you really think this syntax is correct?</p> 2012-01-17T12:01:43-05:004787689http://forums.asp.net/p/1759758/4787689.aspx/1?Re+How+to+use+nested+queriesRe: How to use nested queries <p>&nbsp; &nbsp;&nbsp;</p> 2012-01-17T12:20:23-05:004790761http://forums.asp.net/p/1759758/4790761.aspx/1?Re+How+to+use+nested+queriesRe: How to use nested queries <p>Hello siddharth_kcr</p> <p>Your codes seem right</p> <p>1 Do any modifies data contents to the db's tablessuch as insertingdeleting or updatingyou should use ExecuteNonQuery()</p> <p>2Do to fetch single record value from db's tableyou should use ExecuteScalar()something like &quot;select top 1 XXXField from XXX&quot;</p> <p>Reguards</p> 2012-01-19T01:31:42-05:00