2012-10-27 50 views
0

是否可以添加列(PK)以查看。如果是的話。我們可以嗎?如何添加新列(作爲主鍵列)以查看

這是我的看法:

CREATE VIEW [dbo].[SalesDetailView] 
AS 
SELECT 
    DATENAME(yyyy, SH.CreatedDateTime) AS Year, 
    DATENAME(mm, SH.CreatedDateTime) AS Month, SH.CreatedDateTime AS Date, 
    SH.TransactionName AS Type, SH.SalesHeaderID AS No, 
    Customer.CustomerName AS Customer, 
    CustomerGroup.CustomerGroupName AS Customer_Group, SH.Reference AS Memo, 
    Item.ItemName AS Item, SD.LineDescription AS Item_Description, 
    Item.ItemType AS Item_Type, Item.UOM, 
    ItemGroup.ItemGroupName AS Item_Group, 
    CAST (SD.Quantity AS INT) AS Quantity, CAST(SD.Amount AS MONEY) AS Amount, 
    SD.Price, SD.Discount, SH.ExchangeRate AS Exchange_Rate, 
    Currency.CurrencyDescription AS Currency, SD.ClassID AS Class_ID, 
    SD.SalesTaxID AS SalesTax_ID, SalesTaxGroup.SalesTaxGroupName AS Tax_Group, 
    Employee.EmployeeName AS Salesperson, 
    ShippingMethod.ShippingMethodName AS Shipping_Method, 
    PaymentTerm.PaymentTermName AS Payment_Term, 
    PaymentMethod.PaymentMethodName AS Payment_Method 
FROM 
    SalesHeader SH, Customer 
LEFT OUTER JOIN 
    SalesDetail SD ON SH.SalesHeaderID = SD.SalesHeaderID 
LEFT OUTER JOIN 
    Item ON SD.ItemID = Item.ItemID 
LEFT OUTER JOIN 
    ItemGroup ON Item.ItemGroupId = ItemGroup.ItemGroupID 
LEFT OUTER JOIN 
    CustomerGroup ON Customer.CustomerGroupId = CustomerGroup.CustomerGroupID 
LEFT OUTER JOIN 
    Employee ON Customer.EmployeeID = Employee.EmployeeID 
LEFT OUTER JOIN 
    Currency ON Customer.CurrencyID = Currency.CurrencyID 
LEFT OUTER JOIN 
    SalesTaxGroup ON Customer.SalesTaxGroupID = SalesTaxGroup.SalesTaxGroupID 
LEFT OUTER JOIN 
    PaymentTerm ON Customer.PaymentTermID = PaymentTerm.PaymentTermID 
LEFT OUTER JOIN 
    ShippingMethod ON Customer.ShippingMethodID = ShippingMethod.ShippingMethodID 
LEFT OUTER JOIN 
    PaymentMethod ON Customer.PaymentMethodID = PaymentMethod.PaymentMethodID 
WHERE 
    SH.CustomerID = Customer.CustomerID 
    AND SH.TransactionName <> 'SalesOrder' 
    AND Sh.TransactionName <> 'Quote' 
+0

請把代碼添加到您的文章,而不是鏈接到別的地方 –

+0

對不起視圖包含code..i 40行試圖添加code.But我不能... – Kavitha

+0

發佈視圖代碼在這裏。一個評論:**爲什麼**是客戶沒有像所有其他表一樣正確加入?使用'INNER JOIN Customer ON SH.CustomerID = Customer.CustomerID'(並從那裏'WHERE'子句中刪除該條件)使其與所有其他表相同! –

回答

1

您可以用row_number添加唯一一列,例如:

CREATE VIEW [dbo].[SalesDetailView] 
AS 
SELECT 
    row_number() over (order by SH.CreatedDateTime) as PK, 
    DATENAME(yyyy, SH.CreatedDateTime) AS Year, 

如果這不是你的意思,請澄清你的問題。一些示例結果總是有幫助的。

+0

我添加了這行:row_number()over(order by Employee.EmployeeID)作爲PK, 在Employee表中,EmployeeId列是標識列。 我運行實體模型。現在,視圖顯示正確的數據。問題是當我在SQL中看到視圖設計時,它沒有顯示。當我從視圖中刪除row_number()時,它將顯示在視圖的設計中。 – Kavitha

0

當您看到視圖的sp_help時,所提及的標識列只是基礎表的標識。

如果您已經在基礎表中添加了標識列,您只需要更改視圖並在選擇標記中添加標識列。

否則,您需要先將標識添加到您的表格中,然後編輯視圖以在選擇標記中添加列。

ALTER VIEW [dbo].[SalesDetailView] 
AS 
SELECT 
    DATENAME(yyyy, SH.CreatedDateTime) AS Year, 
    DATENAME(mm, SH.CreatedDateTime) AS Month, SH.CreatedDateTime AS Date, 
    SH.TransactionName AS Type, SH.SalesHeaderID AS No, 
    Customer.CustomerName AS Customer, 
    CustomerGroup.CustomerGroupName AS Customer_Group, SH.Reference AS Memo, 
    Item.ItemName AS Item, SD.LineDescription AS Item_Description, 
    Item.ItemType AS Item_Type, Item.UOM, 
    ItemGroup.ItemGroupName AS Item_Group, 
    CAST (SD.Quantity AS INT) AS Quantity, CAST(SD.Amount AS MONEY) AS Amount, 
    SD.Price, SD.Discount, SH.ExchangeRate AS Exchange_Rate, 
    Currency.CurrencyDescription AS Currency, SD.ClassID AS Class_ID, 
    SD.SalesTaxID AS SalesTax_ID, SalesTaxGroup.SalesTaxGroupName AS Tax_Group, 
    Employee.EmployeeName AS Salesperson, 
    ShippingMethod.ShippingMethodName AS Shipping_Method, 
    PaymentTerm.PaymentTermName AS Payment_Term, 
    PaymentMethod.PaymentMethodName AS Payment_Method, 
    [your column] as PK 
FROM 
    SalesHeader SH, Customer 
LEFT OUTER JOIN 
    SalesDetail SD ON SH.SalesHeaderID = SD.SalesHeaderID 
LEFT OUTER JOIN 
    Item ON SD.ItemID = Item.ItemID 
LEFT OUTER JOIN 
    ItemGroup ON Item.ItemGroupId = ItemGroup.ItemGroupID 
LEFT OUTER JOIN 
    CustomerGroup ON Customer.CustomerGroupId = CustomerGroup.CustomerGroupID 
LEFT OUTER JOIN 
    Employee ON Customer.EmployeeID = Employee.EmployeeID 
LEFT OUTER JOIN 
    Currency ON Customer.CurrencyID = Currency.CurrencyID 
LEFT OUTER JOIN 
    SalesTaxGroup ON Customer.SalesTaxGroupID = SalesTaxGroup.SalesTaxGroupID 
LEFT OUTER JOIN 
    PaymentTerm ON Customer.PaymentTermID = PaymentTerm.PaymentTermID 
LEFT OUTER JOIN 
    ShippingMethod ON Customer.ShippingMethodID = ShippingMethod.ShippingMethodID 
LEFT OUTER JOIN 
    PaymentMethod ON Customer.PaymentMethodID = PaymentMethod.PaymentMethodID 
WHERE 
    SH.CustomerID = Customer.CustomerID 
    AND SH.TransactionName <> 'SalesOrder' 
    AND Sh.TransactionName <> 'Quote' 
+0

感謝您的答覆。在員工表employeeID是標識column.so我剛剛添加此行在我的看法row_number()over(order by Employee.EmployeeID)作爲PK, – Kavitha

+0

現在實體模型顯示正確的數據。但是,當我嘗試看到SalesDetailView的設計視圖。它給出錯誤消息:SQL文本不能在網格窗格和圖表窗格中表示。 – Kavitha