I need assistance with writing SQL for a view in SQL Server 2005/2008. For this task, I have two tables “table1” and “table2.” I am trying to come up with a procedure to dynamically create a view. There are two challenges. 1. I don’t want to include the key and 2. There is a User Interface which allows the administrator to add columns to one of the tables.
Example:
CREATE TABLE [dbo].[Dim](
[DimCode] [varchar](50) Not Null,
[DimDescription] [varchar](100) NULL)
CREATE TABLE [dbo].[DimAttribute](
[DimCode] [varchar](50) Not Null,
[Att1] [varchar](100) NULL,
[Att2] [varchar](100) NULL,
[Att3] [varchar](100) NULL)
The view would be based on something like:
Select D1.DimCode, D1.DimDescription, D2.Att1, D2.Att2, D2.Att3
From Dim D1
Left Outer Join DimAttribute D2 on D1.DimCode = D2.DimCode
Note: The actual table has custom names, not Att1..Att3, etc.
I want to avoid showing the DimCode two times, so I can’t use Select D2.*. However, since new columns can be added in the DimAttribute table, I need a way to update the view to ensure all columns show when new fields are added to the dimAttribute table.
Any ideas on how this is achievable or thoughts on a better design approach are greatly appreciated.
Regards,
Mike
Example:
CREATE TABLE [dbo].[Dim](
[DimCode] [varchar](50) Not Null,
[DimDescription] [varchar](100) NULL)
CREATE TABLE [dbo].[DimAttribute](
[DimCode] [varchar](50) Not Null,
[Att1] [varchar](100) NULL,
[Att2] [varchar](100) NULL,
[Att3] [varchar](100) NULL)
The view would be based on something like:
Select D1.DimCode, D1.DimDescription, D2.Att1, D2.Att2, D2.Att3
From Dim D1
Left Outer Join DimAttribute D2 on D1.DimCode = D2.DimCode
Note: The actual table has custom names, not Att1..Att3, etc.
I want to avoid showing the DimCode two times, so I can’t use Select D2.*. However, since new columns can be added in the DimAttribute table, I need a way to update the view to ensure all columns show when new fields are added to the dimAttribute table.
Any ideas on how this is achievable or thoughts on a better design approach are greatly appreciated.
Regards,
Mike