Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query tables with relationships established 3

Status
Not open for further replies.

Michael57

Technical User
Nov 15, 2005
131
CA
I have two tables that have relationships established but when I query the data it is as if the relationship does not exist. Do I have to restate the relationship inside the query?
 
Help me I'm new and a little lost. This is my current code:
Select JobAddChgHeader.[Index],[Work Rel#],PO#,Ref,Die,Description,JobAddChgDetail.Amount
From WorkProcess.dbo.JobAddChgHeader,WorkProcess.dbo.JobAddChgDetail
Where [Work Rel#]>0

I'm trying to relate JobAddChgHeader.[Index] to Ref
 
Code:
Select jh.[Index],[Work Rel#],PO#,Ref,Die,Description,jd.Amount
From WorkProcess.dbo.JobAddChgHeader jh
join WorkProcess.dbo.JobAddChgDetail jd on jh.[Index] =jd.[Index]
Where [Work Rel#]>0

change [Index] to the column that both tables have
maybe you need a left join since I don't know your tables it is difficult to say

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
SQLBlog.com, Google Interview Questions
 
also try out the query window in enterprise manager.

if you have defined the relationships correctly it will write out the joins for you...

Known is handfull, Unknown is worldfull
 
I'd have to disagree with vbkris. The query window in Enterprise Mangler has some limitations that can be very frustrating, and it doesn't always come up with the best code. Now is the perfect time to get started using query analyzer to write your queries so that you can REALLY understand SQL. Stop the bad habits before they start :)

Alex

Ignorance of certain subjects is a great part of wisdom
 
I've seen this query window but how do you add tables from different databases to it. It only seems to see the current database your in and you can only add tables from that database
 
If you are already in EM, you just click Tools --> SQL Query Analyzer

(it should also be available in your start menu).

To reference a table in another database (same server) use its' full name:

[Database].[owner].


ie

AlexDB.dbo.MyTable

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
I think you should read this FAQ on joins. It might help you for the future.

Questions about posting. See faq183-874
 
Oops forgeot the link
JOIN Fundamentals faq183-4785

Questions about posting. See faq183-874
 
>>The query window in Enterprise Mangler has some limitations that can be very frustrating, and it doesn't always come up with the best code

correct. but for writing out PURE join statements only its the best (assuming you have the relationships setup)! infact i use it a lot when i have to join more than 6-7 tables (no extra where clauses).

from there i copy the written SQL and process it further using the analyser...

Known is handfull, Unknown is worldfull
 
Fair enough :)

Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top