Solved

Concatenate SQL statements

Posted on 2008-10-02
2
321 Views
Last Modified: 2013-12-17
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
Comment
Question by:MichelleLacy
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22622844
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
 

Author Closing Comment

by:MichelleLacy
ID: 31502324
thanks for the solution.  I used it immedieately.  I apologize for not updating expert exchange sooner.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now