Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Concatenate SQL statements

I am building a  sql statement through a for loop.  see below.  when it runs I want string selectquery = selectquery + "INTERSECT " + [the next query generated from the for loop]  how do I do this?


string selectquery = "";
for (int q=0, a=1, x=2; q < carList.Count && a < dealerList.Count && x < dealerList.Count; q++, a+=4, x+=4 )
                        {
                           carname = carList[q].ToString();
		         carprice = dealerList[a].ToString();
		         location = dealerList[x].ToString();
 
 string selectquery = query "SELECT car_name FROM names n, price p, dealer d where name =" + carname + "AND price =" + carprice + "AND dealer =" + location + ";

Open in new window

0
MichelleLacy
Asked:
MichelleLacy
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
something like this:
string selectquery = ""; 
for (int q=0, a=1, x=2; q < carList.Count && a < dealerList.Count && x < dealerList.Count; q++, a+=4, x+=4 )
{
  carname = carList[q].ToString();
  carprice = dealerList[a].ToString();
  location = dealerList[x].ToString();
 
  selectquery += ( selectquery == "" ? "" : " UNION ALL " ) + "SELECT car_name FROM names n, price p, dealer d where name = '" + carname.Replace("'", "''") + "' AND price =" + carprice + " AND dealer = '" + location.Replace("'", "''") + "' ";

Open in new window

0
 
MichelleLacyAuthor Commented:
thanks for the solution.  I used it immedieately.  I apologize for not updating expert exchange sooner.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now