You are here:   Forums
Register   |  Login

Forum

 
ForumForumSQL Server 2008...SQL Server 2008...General Develop...General Develop...TSQL---- Sub query --- need helpTSQL---- Sub query --- need help
Previous Previous
 
Next Next
New Post
 11/14/2009 3:51 PM
 

Hello everyone...

Can subquery return more than one columns....if yes then how...or any other solution....u have please send me......

New Post
 11/18/2009 3:46 PM
 

Welcome Omi,

Here is excerpts from BOL: In some Transact-SQL statements, the subquery can be evaluated as if it were an independent query. Conceptually, the subquery results are substituted into the outer query (although this is not necessarily how Microsoft SQL Server actually processes Transact-SQL statements with subqueries). There are three basic types of subqueries. Those that:

  1. Operate on lists introduced with IN, or those that a comparison operator modified by ANY or ALL.
  2. Are introduced with an unmodified comparison operator and must return a single value.
  3. Are existence tests introduced with EXISTS.

Here are the code sample from BOL(please not that the subquery in 1st query will return more then one record):

USE

AdventureWorks;

GO

SELECT

DISTINCT c.LastName, c.FirstName, e.EmployeeID

FROM

Person.Contact AS c JOIN HumanResources.Employee AS e

ON

e.ContactID = c.ContactID

WHERE

5000.00 IN

(SELECT BonusFROM Sales.SalesPerson spWHERE e.EmployeeID = sp.SalesPersonID) ;

GO

/* SELECT statement built using a subquery. */

SELECT

Name

FROM

AdventureWorks.Production.Product

WHERE

ListPrice =

(SELECT ListPriceFROM AdventureWorks.Production.ProductWHERE Name = 'Chainring Bolts' )

/* SELECT statement built using a join that returns

the same result set. */

SELECT

Prd1. Name

FROM

AdventureWorks.Production.Product AS Prd1JOIN AdventureWorks.Production.Product AS Prd2ON (Prd1.ListPrice = Prd2.ListPrice)

WHERE

Prd2. Name = 'Chainring Bolts'

hth


Who'sOnline

Membership Membership:
Latest New User Latest: Matang
Past 24 Hours Past 24 Hours: 0
Prev. 24 Hours Prev. 24 Hours: 0
User Count Overall: 110

People Online People Online:
Visitors Visitors: 4
Members Members: 0
Total Total: 4

Online Now Online Now:

Spread the Word

Bookmark and Share

);