如何添加新列(作为主键列)进行查看

时间:2012-10-27 12:15:43

标签: sql-server sql-server-2008 view primary-key

是否可以添加列(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'

2 个答案:

答案 0 :(得分:1)

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

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

如果那不是您的意思,请澄清您的问题。一些示例结果总是有用的。

答案 1 :(得分:0)

当您看到视图的sp_help时,提到的标识列只是基础表的标识。

如果您已在基础表中添加了标识列,则只需更改视图并在select stmt中添加标识列。

否则,您需要先在表格中添加标识,然后编辑视图以在选择标准中添加该列。

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'