Solved

use select value as column name in sub sql

Posted on 2014-11-03
4
107 Views
Last Modified: 2014-12-06
I need to use the value of a selected column as column name in sub sql.

Like this:
Select A.id, A.name, A.header, (Select A.name from table2 B where B.id = 2) as value from table1 A

A.name is the Clue here. that contains the column name that I need in the sub sql

How to do this ??
0
Comment
Question by:thorv71
  • 2
4 Comments
 
LVL 11

Expert Comment

by:David Kroll
Comment Utility
declare @sql nvarchar(max);
declare @fieldname nvarchar(max);

select @fieldname = A.name from table1 A

set @sql = 'Select A.id, A.name, A.header, (Select :FIELDNAME from table2 B where B.id = 2) as value from table1 A'

set @sql = REPLACE(@sql, ':FIELDNAME', @fieldname)

exec (@sql)
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Not 100% sure what you need, but this is my best guess:

Select A.id, A.name, A.header,
    (Select  B.id from table2 B where B.name = A.name) as value
from table1 A
0
 

Accepted Solution

by:
thorv71 earned 0 total points
Comment Utility
I had to solve this in another way, no one of the above solutions fixed my problem.
0
 

Author Closing Comment

by:thorv71
Comment Utility
No one of the other solution fixed my problem.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now