[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Select from two DataTables for each record

Posted on 2011-10-20
7
Medium Priority
?
202 Views
Last Modified: 2012-05-12
Hello,

I am using Windows Forms.

I have a DataTable with the column where I have ID-s of my products. I need to create another column, where are displayed names of the products, which means: I have to traverse all ID-s and for each ID search on another table, which consists of ID-s and ID names and for each record find ID name. The only problem here is, that traversing through all records in the DataTable and write each particular record in new column is very, very slow.

How can I solve this faster?

My first tables column: ID
My seoond tables columns: ID, ID_Name

It is possible for me to query the database directly, but what is the query to find names for each particular ID in the first table?

My final result should like:

ID_name
ID_name
ID_name

(names for each particular ID) - so only one column should be in return.


Greetings, Frenky
0
Comment
Question by:AntonioRodrigo
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Accepted Solution

by:
VipulKadia earned 2000 total points
ID: 36998887
Select FirstTable.ID, SecondTable.ID_Name
FROM FirstTable LEFT JOIN SecondTable
ON FirstTable.ID = SecondTable.ID
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36998902
what VipulKadia done is right
only one change so only one column should be in return.

Select FirstTable.ID+ '_'  + SecondTable.ID_Name
FROM FirstTable LEFT JOIN SecondTable
ON FirstTable.ID = SecondTable.ID

Open in new window

0
 
LVL 10

Expert Comment

by:gavsmith
ID: 36998903
Are both of the DataTables in the same DataSet? If so you should be able to create a DataRelation between the 2 tables the use GetChildRows to retrieve the names.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:gavsmith
ID: 36998909
I saw DataTable and presumed this was a programming question not a sql question... I'm still not sure?!
0
 
LVL 8

Expert Comment

by:VipulKadia
ID: 36998910
And if you want only Names then write as follows :

Select SecondTable.ID_Name
FROM FirstTable LEFT JOIN SecondTable
ON FirstTable.ID = SecondTable.ID
0
 

Author Closing Comment

by:AntonioRodrigo
ID: 36998963
Very very good job. Thanks.
0
 

Author Comment

by:AntonioRodrigo
ID: 36998971
Yes, I want only names, so I've made this little change of the query immediately. It solves the problem, 100%.

I am using WinForms, but data is coming from SQL server. I've decided to make a query command builder in my code and send it to sql server.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

830 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