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

Posted

技术标签:

【中文标题】如何添加新列(作为主键列)查看【英文标题】:How to add a new column (as a primary key column ) to view 【发布时间】:2012-10-27 12:15:42 【问题描述】:

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

【问题讨论】:

请将代码添加到您的帖子中,而不是链接到其他地方 抱歉视图包含 40 行代码..我试图添加代码。但我不能.. 在此处发布查看代码。一条评论:为什么Customer 没有像所有其他表一样正确连接?使用INNER JOIN Customer ON SH.CustomerID = Customer.CustomerID(并从WHERE 子句中删除该条件)使其与所有其他表相同! 我在 sql 中收到此错误:FROM 子句中的对象“客户”和“客户”具有相同的公开名称。使用相关名称来区分它们。 如果您将我的行与 INNER JOIN Customer 一起使用 - 您当然必须从您的 FROM 语句中删除 ..... , Customer 【参考方案1】:

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

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

如果这不是您的意思,请澄清您的问题。一些示例结果总是很有帮助的。

【讨论】:

我添加了这一行:row_number() over (order by Employee.EmployeeID) 作为PK,在Employee 表中EmployeeId 列是标识列。我运行实体模型。现在,视图显示正确的数据。问题是当我在 SQL 中看到视图设计时,它没有显示。当我从视图中删除 row_number() 时,它会显示在视图的设计中。【参考方案2】:

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

如果你已经在基础表中添加了标识列,你只需要改变你的视图并在select stmt中添加标识列。

否则,您需要先将标识添加到表中,然后编辑视图以在您的选择 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'

【讨论】:

感谢您的回复。在 Employee 表中,employeeID 是标识列。所以我只是在视图 row_number() 中添加了这一行(按 Employee.EmployeeID 排序)作为 PK, 现在实体模型显示正确的数据。但是当我尝试查看 SalesDetailView 的设计视图时。它给出错误消息:SQL 文本无法在网格窗格和图表窗格中表示。

以上是关于如何添加新列(作为主键列)查看的主要内容,如果未能解决你的问题,请参考以下文章

如何通过phoenix中查看表的主键信息

如何在Django中加入非主键和外键列的查询

jqgrid 主键列的设定

SQL:向数据库表添加新的主键列

当主键列是mysql中不同表的外键时,如何将主键列更改为自动递增

实体框架 4.1 代码优先映射到将主键作为外键列的表