How do i get the contents/Id of a of a field for a nested query if its contained in a parent query

Posted on 2009-12-30
Medium Priority
Last Modified: 2012-05-08
I've got the following query i.e.

INSERT INTO Table1(value1,value2,value3)
      SELECT ID, * FROM Table2
      WHERE Column2 NOT IN
            SELECT DISTINCT ID FROM [Table3]
            WHERE [column2]=Table2.ID AND userId=@userId

The question is based around the nested contents i.e.

WHERE [column2]=Table2.ID AND userId=@userId

Is there anyway for Table2.id to come from the "SELECT * FROM Table2" query listed above it?

I hope this makes sense?


Question by:Webbo_1980
LVL 10

Accepted Solution

lof earned 1000 total points
ID: 26148040
that is exactly what you have written

in the sub-query Table3.Column2 will be compared with Table2.Column2 for a current row
LVL 26

Assisted Solution

tigin44 earned 1000 total points
ID: 26148482
your query does what you have described. For performance reason writing it in this way may be preferred.
INSERT INTO Table1(value1,value2,value3)
      SELECT ID, * 
      FROM Table2
            SELECT NULL 
            FROM [Table3]
            WHERE [column2]=Table2.ID AND userId=@userId

Open in new window


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
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…

862 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