Thursday, March 8, 2012

Creating Queries

Is is possible to create a query to do something like
this:
For each "Record" from {SELECT field1 FROM table1}
SELECT * FROM table2
WHERE field2='Record'
next "Record"You can use a cursor to do so, but a regular query joining table1 to
table2 using field1 = field2 is the same thing.
What are you trying to accomplish?
Simon Worth
JLong wrote:
> Is is possible to create a query to do something like
> this:
> For each "Record" from {SELECT field1 FROM table1}
> SELECT * FROM table2
> WHERE field2='Record'
> next "Record"|||I think you mean something like this:
SELECT *
FROM Table2
WHERE EXISTS
(SELECT *
FROM Table1
WHERE Table1.field1 = Table2.field2)
Note that when discussing RDBMSs it's often considered more correct to
use the terms "Row" and "Column" in preference to "Record" and "Field".
Some people quite comfortably use these words interchangeably, while
others attach important conceptual differences to them and insist that
"records" and "fields" have no place in a relational database. If you
don't know or care about the difference then I suggest you make the
effort to use the least controversial terms (Rows and Columns) when
posting, if only in the interests of a quiet life ;-)
David Portas
SQL Server MVP
--|||Thank you David for your comments, I really do care about
RDBMS's terminology. Thank you for your suggestion I
will try it tomorrow morning at work and will let you
know how it turned out. As for Simon's question, I have a
query that return a calculations based on a variable
(row/record), I am running the query on the SQL query
analyzer. I was trying to see if I could pass the record
as a query, so I didn't have to change the value by hand.

>--Original Message--
>I think you mean something like this:
>SELECT *
> FROM Table2
> WHERE EXISTS
> (SELECT *
> FROM Table1
> WHERE Table1.field1 = Table2.field2)
>Note that when discussing RDBMSs it's often considered
more correct to
>use the terms "Row" and "Column" in preference
to "Record" and "Field".
>Some people quite comfortably use these words
interchangeably, while
>others attach important conceptual differences to them
and insist that
>"records" and "fields" have no place in a relational
database. If you
>don't know or care about the difference then I suggest
you make the
>effort to use the least controversial terms (Rows and
Columns) when
>posting, if only in the interests of a quiet life ;-)
>--
>David Portas
>SQL Server MVP
>--
>.
>|||Did my suggestion work? If not and you need more help, please read the
following article which explains the best way to desribe your problem
here.
http://www.aspfaq.com/etiquette.asp?id=5006
David Portas
SQL Server MVP
--|||I'm still having problems. I guess that I need to declare
a variable to hold the rows from the first select query
and then pass it to the second select query to return what
I want. In visual basic, it would be a loop like this
For each row in table1
select * from (select column1 from table2 where
coloum2='row') as table
next row
I don't know how to do it or if it could be done.
Basically you return a column from a table and you pass
every row to another query to perform calculations for
each row.
Any idias?

>--Original Message--
>Did my suggestion work? If not and you need more help,
please read the
>following article which explains the best way to desribe
your problem
>here.
>http://www.aspfaq.com/etiquette.asp?id=5006
>--
>David Portas
>SQL Server MVP
>--
>.
>|||"I'm still having problems" doesn't tell me a thing! Did you try out
the query I posted? Did you get an error message? If the result wasn't
what you expected then what was wrong? The query I posted should do
exactly what you want as I understand it: For each row in Table1 return
the matching rows from Table2 where field2 = field1.
I previously posted an excellent article that explains how to describe
your problem for the group, so please try to follow that advice and
post DDL, sample data and show your required end result. You'll find
you get a helpful answer much quicker that way.
David Portas
SQL Server MVP
--

No comments:

Post a Comment