Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 30th, 2004, 09:06 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting data from two tables

Hi everybody,

 I have a problem in SQL server 2000 I have a stored procedure that returns loads of data for my CrystalReport in VB.NET

however I cant get over this problem

I have a table - tblConditions that looks lik this

DescId Description Condition
------------------------------------
   1 Raw State
   2 Cooked State
   3 Partly Cooked State
   4 Ambient Temp
   5 Frozen Temp
   6 Refrigerated Temp

Then I have another table - tblSamples looks like this

SampleId Name StateOfSample TempR TempS
------------------------------------------------------
   1 Chicken 1 4 5
   2 Beef 2 6 4
   3 Prawn 3 5 5


I need a SQL query that will return data from both of the tables so the row returned for SampleId = 2 will look like this:

SampleId Name StateOfSample TempR TempS
------------------------------------------------------
   2 Beef Raw Ambient Frozen


Does anybody have an idea how to do this in SQL statement?


 
Old September 30th, 2004, 09:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You can JOIN tblConditions to tblSamples 3 times, you just need to use a different ON clause for each of the three JOINs, plus a table alias for at least 2 of them.

For example, the first JOIN will be ON StateOfSample=DescId, but the 2nd will be ON TempR=DescId.

Rather than me type it all out for you, just do it in Enterprise Manager (Open tblSamples using the Query option, then Add tblCondition three times and change the JOINS). EM will write the SQL for you, which you can save as a View or Stored Proc or whatever you want.

hth
Phil
 
Old September 30th, 2004, 09:58 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks I have tried that but got into this problem

The StateOfSample in tblSamples is type smallInt
and Decscription in tblConditions is type VarChar

and when I try to do what you said I am getting this error:

Syntax Error converting varChar Value 'Raw ' to a column of data
type smallInt

I have even tried to change the StateOfSample in tblSamples to VarChar but then I have no records returned at all...

Do you think you can write a bit of the query for mee so I can pick it up and wirte the rest of it myself?

:(
 
Old September 30th, 2004, 10:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

pajer,

I think it should be DESCRIPTION instead of DESCID there.
Code:
ON StateOfSample=Description
Should have been a typo in Phil's case.;)

Cheers!

_________________________
- Vijay G
Strive for Perfection
 
Old September 30th, 2004, 10:15 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah thats what i have

SELECT *, tblConditions.Description AS Expr1
FROM tblSamples INNER JOIN
                      tblConditions ON tblSamples.StateOfSample = tblConditions.Description

returns an Empty resultset

 
Old September 30th, 2004, 10:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

No, not a typo. I said join it to DescID, not to Description.
 
Old September 30th, 2004, 10:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sorry phil, I just over looked that. That was my bad. you are right.

Pajer,
Phil's suggestion should have worked well. Please check your query.

_________________________
- Vijay G
Strive for Perfection
 
Old September 30th, 2004, 10:33 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Try
Code:
SELECT     
FROM         tblSamples INNER JOIN
                      tblConditions ON CAST(tblSamples.StateOfSample AS VarChar) = tblConditions.Description
Not a great design to have tables joined on mismatched columns.

--

Joe
 
Old September 30th, 2004, 10:45 AM
Authorized User
 
Join Date: Sep 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes guys I gor It working it creates a new Field Expr1 Expr2 and so on It took me while to get my head around it but I got it working finally Thanks...






Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting data from link tables psnow1985 SQL Server ASP 0 March 27th, 2008 03:19 PM
how to compare tables data ramanadyv SQL Language 0 April 10th, 2006 04:38 AM
data from multiple tables keyvanjan Classic ASP Basics 1 January 24th, 2006 06:32 PM
Getting data from linked tables SoC Classic ASP Databases 2 October 13th, 2005 08:18 PM
Getting data from tables MattLeek Access VBA 2 October 8th, 2003 11:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.