Guest User

Untitled

a guest
Dec 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. Select a.*, coalesce(x.guid,y.guid,z.guid) as guid
  2. from TableA a
  3. left join TableX x
  4. on a.ProductId = x.ProductId
  5. and a.ProductTypeId = x.ProductTypeId -- 1
  6. left join TableY y
  7. on a.ProductId = y.ProductId
  8. and a.ProductTypeId = y.ProductTypeId -- 2
  9. left join TableZ z
  10. on a.ProductId = z.ProductId
  11. and a.ProductTypeId = z.ProductTypeId -- 3
  12. left join other items of variation with where clauses at end...
  13.  
  14. Select b.*,coalesce(x.guid,y.guid,z.guid) as guid
  15. from Tableb b
  16. left join TableX x
  17. on b.ProductId = x.ProductId
  18. and b.ProductTypeId = x.ProductTypeId -- 1
  19. left join TableY y
  20. on b.ProductId = y.ProductId
  21. and b.ProductTypeId = y.ProductTypeId -- 2
  22. left join TableZ z
  23. on b.ProductId = z.ProductId
  24. and b.ProductTypeId = z.ProductTypeId -- 3
  25. left join other items of variation with where clauses at end...
  26.  
  27. left joinTableX x
  28. on a.ProductId = x.ProductId
  29. and a.ProductTypeId = x.ProductTypeId -- 1
  30. left join TableY y
  31. on a.ProductId = y.ProductId
  32. and a.ProductTypeId = y.ProductTypeId -- 2
  33. left join TableZ z
  34. on a.ProductId = z.ProductId
  35. and a.ProductTypeId = z.ProductTypeId -- 3
  36. left join other items of variation with where clauses at end...
  37.  
  38. create view view_you_need as
  39. select
  40. all relevant columns
  41. from TableX x
  42. left join TableY y
  43. on x.ProductId = y.ProductId
  44. and x.ProductTypeId = y.ProductTypeId -- 2
  45. left join TableZ z
  46. on x.ProductId = z.ProductId
  47. and x.ProductTypeId = z.ProductTypeId -- 3
  48. left join other items of variation with where clauses at end...
  49.  
  50. declare @sourcetable varchar(100) = 'TableB';
  51. declare @sql nvarchar(max);
  52.  
  53. select @sql =
  54. 'select
  55. all relevant columns
  56. from ' + @sourcetable +
  57. ' left join view_you_need
  58. on etc etc';
  59.  
  60. exec(@sql);
Add Comment
Please, Sign In to add comment