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 2000 Forums
 Transact-SQL (2000)
 Subquery

Author  Topic 

ikhuram
Starting Member

28 Posts

Posted - 2004-06-07 : 06:50:49
If I want to run a subquery containing outer query's column'value as sunquery's column, how can I do this e.g.,

select employeeid, first_name, section
'name' = (select employee.section from employee_personal_info)
from employee

where employee.section's value is the column name of employee_personal_info table.

When I run this query it doesn't execute the subquery but fills the name column with the section's value. Please solve this problem

nr
SQLTeam MVY

12543 Posts

Posted - 2004-06-07 : 07:02:53
select employeeid, first_name, [section name] = (select employee_personal_info.section from employee_personal_info where employee_personal_info.employee_id = employee.employeeid)
from employee

or better

select employeeid, first_name,
[section name] = employee_personal_info.section from employee_personal_infofrom employee
join employee_personal_info
on employee_personal_info.employee_id = employee.employeeid

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ikhuram
Starting Member

28 Posts

Posted - 2004-06-07 : 10:11:16
But in employee_personal_info, section is not the field, but employee's section field's value is the field in employee_personal_info. So I want to run the query against the value, and considering value the field of employee_personal_info. how can I do this Suppose employee.section contains A the query will be lik
(select A from employee_personal_info). Please help me in this
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-06-07 : 10:46:58
Probably a case statement.

select employeeid, first_name,
[section name] = case when employee.section = 'fld1' then employee_personal_info.fld1
when employee.section = 'fld2' then employee_personal_info.fld2
end
from employee_personal_infofrom employee
join employee_personal_info
on employee_personal_info.employee_id = employee.employeeid


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ikhuram
Starting Member

28 Posts

Posted - 2004-06-07 : 11:41:18
And if there would be more than 100,000 values then how much case will we use? Please tell me a generic way, Please
Go to Top of Page
   

- Advertisement -