hopefully this chart makes sense...
the problem is that I have many columns in the many-many table, how can I get all the column values in a view, without doing ISNULL
for each row?
(sql server 10.5)
ITEM
+------+
| ID |
|------|
| 1 |
| 2 |
| 3 |
+------+
LANGUAGE
+-------+---------+
| ID | Name |
|-------+---------|
| 1 | English |
| 2 | French |
+-------+---------+
Item Names
+----------+---------+------------+------------+
| ItemID | LangId | Name | Color |
|----------+---------+------------+------------|
| 1 | 1 | apple | red |
| 1 | 2 | pomme | rouge |
| 2 | 1 | orange | orange |
| 3 | 1 | bannana | yellow |
+----------+---------+------------+------------+
desired view
+----------+---------+------------+------------+
| ItemID | LangId | Name | Color |
|----------+---------+------------+------------|
| 1 | 1 | apple | red |
| 1 | 2 | pomme | rouge |
| 2 | 1 | orange | orange |
| 2 | 2 | orange | orange | <--- added automatically
| 3 | 1 | bannana | yellow |
| 3 | 2 | bannana | yellow | <--- added automatically
+----------+---------+------------+------------+
because I'm trying to create a view there are certain limitations:
The columns being modified in the view must directly reference the underlying data in the table columns. The columns cannot be derived in any other way, such as through the following:
- An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP, VAR, and VARP.
- A computation. The column cannot be computed from an expression that uses other columns. Columns that are formed by using the set operators UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT amount to a computation and are also not updatable.
I an however create multiple views, which is how I've gotten around some of these restrictions before. we can assume that I already have this table
intermediary view:
+----------+---------+------------+------------+
| ItemID | LangId | Name | Color |
|----------+---------+------------+------------|
| 1 | 1 | apple | red |
| 1 | 2 | pomme | rouge |
| 2 | 1 | orange | orange |
| 3 | 1 | bannana | yellow |
+----------+---------+------------+------------+
as well as:
+----------+---------+------------+------------+
| ItemID | LangId | Name | Color |
|----------+---------+------------+------------|
| 1 | 1 | apple | red |
| 1 | 2 | pomme | rouge |
| 2 | 1 | orange | orange |
| 2 | 2 | - | - |
| 3 | 1 | bannana | yellow |
| 3 | 2 | - | - |
+----------+---------+------------+------------+
these are some of the views:
view1 - all combinations
view2 - all combinations with languages
the corresponding SQL:
SELECT dbo.view1.ItemID, dbo.view1.LanguageID, dbo.ItemLanguages.Name, dbo.ItemLanguages.Color
FROM dbo.ItemLanguages RIGHT OUTER JOIN
dbo.view1 ON dbo.ItemLanguages.LanguageID = dbo.view1.LanguageID AND dbo.ItemLanguages.ItemID = dbo.view1.ItemID
result of view 2
here is the test database with the views and tables: http://pastebin.com/4BpBSmHY