Michael Cole
2012-05-01 06:36:31 UTC
This is a bit of a weird one.
We have tables along the lines of the following design (note - not our
design, please don't tell me to change it): -
CREATE TABLE [dbo].[ZColumns](
[ID] [int] NOT NULL,
[ColumnID] [int] NOT NULL,
[Table] [varchar](50) NULL,
[Column] [nvarchar](50) NULL,
CONSTRAINT [PK_ZColumns] PRIMARY KEY CLUSTERED
(
[ID] ASC,
[ColumnID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[ZTab00001](
[ID] [int] NULL,
[001] [varchar](50) NULL,
[002] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[ZTab00002](
[ID] [int] NULL,
[001] [varchar](50) NULL,
[002] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[ZData](
[ID] [int] NULL,
[Data1] [nvarchar](50) NULL,
[Data2] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO [ZColumns] VALUES (1, 1, '00001', '001');
INSERT INTO [ZColumns] VALUES (1, 2, '00001', '002');
INSERT INTO [ZColumns] VALUES (2, 1, '00001', '001');
INSERT INTO [ZColumns] VALUES (2, 2, '00002', '002');
INSERT INTO [ZColumns] VALUES (3, 1, '00002', '001');
INSERT INTO [ZTab00001] VALUES (1, 'G', 'H');
INSERT INTO [ZTab00001] VALUES (2, 'I', 'J');
INSERT INTO [ZTab00001] VALUES (3, 'K', 'L');
INSERT INTO [ZTab00002] VALUES (1, 'M', 'N');
INSERT INTO [ZTab00002] VALUES (2, 'O', 'P');
INSERT INTO [ZTab00002] VALUES (3, 'Q', 'R');
INSERT INTO [ZData] VALUES (1, 'A', 'B');
INSERT INTO [ZData] VALUES (2, 'C', 'D');
INSERT INTO [ZData] VALUES (3, 'E', 'F');
I would like to create a view, that would return the following: -
ID Data1 Data2 Col1 Col2
1 A B G H
2 C D I P
3 E F Q Null
In other words, link to the data for Col1 and Col2 based off a
determination of the table and field from the table ZColumns.
Are there any thoughts as to how this could be done?
We have tables along the lines of the following design (note - not our
design, please don't tell me to change it): -
CREATE TABLE [dbo].[ZColumns](
[ID] [int] NOT NULL,
[ColumnID] [int] NOT NULL,
[Table] [varchar](50) NULL,
[Column] [nvarchar](50) NULL,
CONSTRAINT [PK_ZColumns] PRIMARY KEY CLUSTERED
(
[ID] ASC,
[ColumnID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[ZTab00001](
[ID] [int] NULL,
[001] [varchar](50) NULL,
[002] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[ZTab00002](
[ID] [int] NULL,
[001] [varchar](50) NULL,
[002] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[ZData](
[ID] [int] NULL,
[Data1] [nvarchar](50) NULL,
[Data2] [nvarchar](50) NULL
) ON [PRIMARY]
INSERT INTO [ZColumns] VALUES (1, 1, '00001', '001');
INSERT INTO [ZColumns] VALUES (1, 2, '00001', '002');
INSERT INTO [ZColumns] VALUES (2, 1, '00001', '001');
INSERT INTO [ZColumns] VALUES (2, 2, '00002', '002');
INSERT INTO [ZColumns] VALUES (3, 1, '00002', '001');
INSERT INTO [ZTab00001] VALUES (1, 'G', 'H');
INSERT INTO [ZTab00001] VALUES (2, 'I', 'J');
INSERT INTO [ZTab00001] VALUES (3, 'K', 'L');
INSERT INTO [ZTab00002] VALUES (1, 'M', 'N');
INSERT INTO [ZTab00002] VALUES (2, 'O', 'P');
INSERT INTO [ZTab00002] VALUES (3, 'Q', 'R');
INSERT INTO [ZData] VALUES (1, 'A', 'B');
INSERT INTO [ZData] VALUES (2, 'C', 'D');
INSERT INTO [ZData] VALUES (3, 'E', 'F');
I would like to create a view, that would return the following: -
ID Data1 Data2 Col1 Col2
1 A B G H
2 C D I P
3 E F Q Null
In other words, link to the data for Col1 and Col2 based off a
determination of the table and field from the table ZColumns.
Are there any thoughts as to how this could be done?
--
Michael Cole
Michael Cole