Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Select a.*, coalesce(x.guid,y.guid,z.guid) as guid
- from TableA a
- left join TableX x
- on a.ProductId = x.ProductId
- and a.ProductTypeId = x.ProductTypeId -- 1
- left join TableY y
- on a.ProductId = y.ProductId
- and a.ProductTypeId = y.ProductTypeId -- 2
- left join TableZ z
- on a.ProductId = z.ProductId
- and a.ProductTypeId = z.ProductTypeId -- 3
- left join other items of variation with where clauses at end...
- Select b.*,coalesce(x.guid,y.guid,z.guid) as guid
- from Tableb b
- left join TableX x
- on b.ProductId = x.ProductId
- and b.ProductTypeId = x.ProductTypeId -- 1
- left join TableY y
- on b.ProductId = y.ProductId
- and b.ProductTypeId = y.ProductTypeId -- 2
- left join TableZ z
- on b.ProductId = z.ProductId
- and b.ProductTypeId = z.ProductTypeId -- 3
- left join other items of variation with where clauses at end...
- left joinTableX x
- on a.ProductId = x.ProductId
- and a.ProductTypeId = x.ProductTypeId -- 1
- left join TableY y
- on a.ProductId = y.ProductId
- and a.ProductTypeId = y.ProductTypeId -- 2
- left join TableZ z
- on a.ProductId = z.ProductId
- and a.ProductTypeId = z.ProductTypeId -- 3
- left join other items of variation with where clauses at end...
- create view view_you_need as
- select
- all relevant columns
- from TableX x
- left join TableY y
- on x.ProductId = y.ProductId
- and x.ProductTypeId = y.ProductTypeId -- 2
- left join TableZ z
- on x.ProductId = z.ProductId
- and x.ProductTypeId = z.ProductTypeId -- 3
- left join other items of variation with where clauses at end...
- declare @sourcetable varchar(100) = 'TableB';
- declare @sql nvarchar(max);
- select @sql =
- 'select
- all relevant columns
- from ' + @sourcetable +
- ' left join view_you_need
- on etc etc';
- exec(@sql);
Add Comment
Please, Sign In to add comment