Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 select command for multiple tables

Author  Topic 

sureshprabhu
Starting Member

32 Posts

Posted - 2007-09-27 : 15:38:14
Hi!
I want to get some fields from more than one table. How can I use select command to do this? Please help me! The results should be in one table only!
Thanks in advance!

Kristen
Test

22859 Posts

Posted - 2007-09-27 : 15:43:13
[code]
SELECT A.Col1, A.COl2, B.ColX, B.ColY
FROM TableA AS A
JOIN TableB AS B
ON B.SomeColumn = A.SomeOtherColumn
[/code]
Kristen
Go to Top of Page

sureshprabhu
Starting Member

32 Posts

Posted - 2007-09-27 : 16:03:08
Thanks Kristen for your response, but it is giving one error as "syntax error near '.'"
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-27 : 16:06:11
Suresh, post your query please.



Future guru in the making.
Go to Top of Page

sureshprabhu
Starting Member

32 Posts

Posted - 2007-09-27 : 16:14:06
select
A.id,
A.FileData,
A.OriginalName,
A.ContentType,
A.DateCreated,
A.Client,
A.username
B.id,
B.FileData,
B.OriginalName,
B.ContentType,
B.DateCreated,
B.Client,
B.username
from Files_Admin as A join Files_Designer AS B on B.id=A.id
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-27 : 16:26:43
You left the comma out:

quote:
Originally posted by sureshprabhu

select
A.id,
A.FileData,
A.OriginalName,
A.ContentType,
A.DateCreated,
A.Client,
A.username, -- This comma was missing!
B.id,
B.FileData,
B.OriginalName,
B.ContentType,
B.DateCreated,
B.Client,
B.username
from Files_Admin as A join Files_Designer AS B on B.id=A.id





Future guru in the making.
Go to Top of Page

sureshprabhu
Starting Member

32 Posts

Posted - 2007-09-27 : 16:36:31
Actually i am passing one parameter as OriginalName, it should check in both tables whether it is existing, if existing it should return that particular record.
Go to Top of Page

Zoroaster
Aged Yak Warrior

702 Posts

Posted - 2007-09-27 : 16:38:19
quote:
Originally posted by sureshprabhu

Actually i am passing one parameter as OriginalName, it should check in both tables whether it is existing, if existing it should return that particular record.



I don't follow your point, the reason you had a syntax error is because the comma was missing. Is the query doing what you want now that you add the comma?



Future guru in the making.
Go to Top of Page

sureshprabhu
Starting Member

32 Posts

Posted - 2007-09-27 : 16:54:49
I added comma its working, But now the problem is, I want to retrieve the data from two tables, after matching with my parameter. I am passing this parameter at the time of execution. the parameter is for OriginalName.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-27 : 19:55:59
Not really clear to me what you mean, but perhaps something like:

select
A.id,
A.FileData,
A.OriginalName,
A.ContentType,
A.DateCreated,
A.Client,
A.username,
B.id,
B.FileData,
B.OriginalName,
B.ContentType,
B.DateCreated,
B.Client,
B.username
from Files_Admin as A
join Files_Designer AS B
on B.id=A.id
WHERE A.OriginalName = @OriginalName
OR B.OriginalName = @OriginalName


Kristen
Go to Top of Page
   

- Advertisement -