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 2005 Forums
 Transact-SQL (2005)
 Select - partial automation of Select statement

Author  Topic 

Buzzard724
Yak Posting Veteran

66 Posts

Posted - 2010-02-12 : 14:21:13
I find this really difficult to explain - which is perhaps why I cannot find the solution! I want to retrieve data from fields in a table where the fieldnames for the select statement are specified in a subquery.

I want to automatically retrieve fields of data from a table (People). The field names I want to retrieve will be specified in a second table (FieldNames) - see query below.

Please can you tell me how to retrieve the data against each field - I can then restrict the query to just one person (WHERE PeopleID = their GUID)

SELECT c.name AS ColumnName
FROM sys.tables AS t
INNER JOIN sys.columns AS c ON t.object_id = c.object_id
INNER JOIN FieldNames f on f.FieldName = c.name
WHERE (t.name = 'people'))

Result Set for the above
LastName
FirstName
DateOfBirth
Company

What I want to see is:

LastName James
FirstName John
DateOfBirth 03/02/1987
Company CompanyA

This will enable me to vary the data retrieved by adding or deleting fieldnames from the fieldnames table.

thanks for your help

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-02-12 : 16:52:26
search for dynamic sql - without it isn't possible.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Buzzard724
Yak Posting Veteran

66 Posts

Posted - 2010-02-13 : 07:47:19
thank you - I am not too sure how severe the security issues are with Dynamic SQL - will research further - thank you for the pointer
Go to Top of Page
   

- Advertisement -