Given:
- SQL Server 2008 R2
- SSRS 2008
- Humble Programmer
Consider two tables:
CREATE TABLE PERSON
(
PersonID INT,
Name VARCHAR(100)
)
CREATE TABLE PERSON_EXTENDED
(
PersonExtendedID INT,
PersonID INT,
DOB DATETIME,
FavoriteColor VARCHAR(100)
)
Further consider the following query joining the tables suggesting their relationship.
SELECT
*
FROM
PERSON AS P
JOIN PERSON_EXTENDED AS PE ON (P.PersonID = PE.PersonID)
To give you the overall concept I want to create a stored procedure that will populate the field selection parameter of an SSRS report. Then using the selected parameters pass that as as argument to another stored procedure which creates the report with the selected fields.
My thought was something like the following initially, however that yields 2 issues. First the duplicate ordinality for 1 and 2 across both the tables. Second the duplicate column name “PersonID”.
SELECT
C.COLUMN_NAME,
C.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.columns AS C
WHERE
C.TABLE_NAME = 'PERSON'
OR C.TABLE_NAME = 'PERSON_EXTENDED'
Any thoughts? Extra credit would be to do this in a scalable way and include some sort of “Display Name” field so the end user didn’t have to deal with PersonID but could deal with “Personal Identification”.
Great Article!
This article is a big step in the right direction and may actually be the complete solution. It is certainly relevant enough to be an answer for anyone else in a similar situation.
One of the comments is particularly intriguing:
I am concerned this is going to be hugely resource intensive though.