Sql server 如何将新列(作为主键列)添加到视图中

Sql server 如何将新列(作为主键列)添加到视图中,sql-server,sql-server-2008,view,primary-key,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.SalesHeaderI

是否可以将列(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'
创建视图[dbo]。[SalesDetailView]
作为
挑选
日期名称(yyyy,SH.CreatedDateTime)作为年份,
DATENAME(mm,SH.CreatedDateTime)表示月份,SH.CreatedDateTime表示日期,
SH.TransactionName作为类型,SH.SalesHeaderID作为编号,
Customer.CustomerName作为客户,
CustomerGroup.CustomerGroup名称为Customer\u Group,SH.参考为备注,
Item.ItemName作为项目,SD.LineDescription作为项目描述,
Item.ItemType作为Item_类型,Item.UOM,
ItemGroup.ItemGroupName作为项目组,
将(SD.Quantity AS INT)转换为数量,将(SD.Amount AS MONEY)转换为金额,
SD.价格,SD.折扣,SH.汇率作为汇率,
Currency.CurrencyDescription作为货币,SD.ClassID作为类别ID,
SD.SalesTaxID作为SalesTax\u ID,SalesTaxGroup.SalesTaxGroupName作为Tax\u组,
Employee.EmployeeName作为销售人员,
ShippingMethod.ShippingMethodName作为Shipping方法,
PaymentTerm.PaymentTerm名称作为付款期限,
PaymentMethod.PaymentMethodName作为付款方法
从…起
售货员,顾客
左外连接
SH.SalesHeaderID=SD.SalesHeaderID上的SalesDetail SD
左外连接
SD.ItemID上的项目=Item.ItemID
左外连接
Item.ItemGroupId=ItemGroup.ItemGroupId上的ItemGroup
左外连接
Customer.CustomerGroupId=CustomerGroup.CustomerGroupId上的CustomerGroup
左外连接
Customer.EmployeeID=Employee.EmployeeID上的员工
左外连接
Customer.CurrencyID=Currency.CurrencyID上的货币
左外连接
Customer.SalesTaxGroupID=SalesTaxGroup.SalesTaxGroupID上的SalesTaxGroup
左外连接
Customer.PaymentTermID=PaymentTerm.PaymentTermID上的PaymentTerm
左外连接
客户的ShippingMethod.ShippingMethodID=ShippingMethod.ShippingMethodID
左外连接
Customer.PaymentMethodID=PaymentMethod.PaymentMethodID上的PaymentMethod
哪里
SH.CustomerID=Customer.CustomerID
和SH.TransactionName“SalesOrder”
和Sh.TransactionName“报价”

您可以添加带有
行号的唯一列,如:

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

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

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

如果已经在基础表中添加了标识列,则只需更改视图并在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'
否则,您需要首先将标识添加到表中,然后编辑视图以在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'
alterview[dbo].[SalesDetailView]
作为
挑选
日期名称(yyyy,SH.CreatedDateTime)作为年份,
DATENAME(mm,SH.CreatedDateTime)表示月份,SH.CreatedDateTime表示日期,
SH.TransactionName作为类型,SH.SalesHeaderID作为编号,
Customer.CustomerName作为客户,
CustomerGroup.CustomerGroup名称为Customer\u Group,SH.参考为备注,
Item.ItemName作为项目,SD.LineDescription作为项目描述,
Item.ItemType作为Item_类型,Item.UOM,
ItemGroup.ItemGroupName作为项目组,
将(SD.Quantity AS INT)转换为数量,将(SD.Amount AS MONEY)转换为金额,
SD.价格,SD.折扣,SH.汇率作为汇率,
Currency.CurrencyDescription作为货币,SD.ClassID作为类别ID,
SD.SalesTaxID作为SalesTax\u ID,SalesTaxGroup.SalesTaxGroupName作为Tax\u组,
Employee.EmployeeName作为销售人员,
ShippingMethod.ShippingMethodName作为Shipping方法,
PaymentTerm.PaymentTerm名称作为付款期限,
PaymentMethod.PaymentMethodName作为付款方法,
[你的专栏]作为主键
从…起
售货员,顾客
左外连接
SH.SalesHeaderID=SD.SalesHeaderID上的SalesDetail SD
左外连接
SD.ItemID上的项目=Item.ItemID
左外连接
Item.ItemGroupId=ItemGroup.ItemGroupId上的ItemGroup
左外连接
Customer.CustomerGroupId=CustomerGroup.CustomerGroupId上的CustomerGroup
左外连接
Customer.EmployeeID=Employee.EmployeeID上的员工
左外连接
Customer.CurrencyID=Currency.CurrencyID上的货币
左外连接
Customer.SalesTaxGroupID=SalesTaxGroup.SalesTaxGroupID上的SalesTaxGroup
左外连接
Customer.PaymentTermID=PaymentTerm.PaymentTermID上的PaymentTerm
左外连接
客户的ShippingMethod.ShippingMethodID=ShippingMethod.ShippingMethodID
左外连接
Customer.PaymentMethodID=PaymentMethod.PaymentMethodID上的PaymentMethod
哪里
SH.CustomerID=Customer.CustomerID
和SH.TransactionName“SalesOrder”
和Sh.TransactionName“报价”

请将代码添加到您的帖子中,而不是链接到其他地方。rry view包含40行代码。.我试图添加代码。但我无法。.在此处发布查看代码。一个注释是:
Customer
为什么不像所有其他表那样正确地连接?在SH.CustomerID=Customer.CustomerID上使用
internal JOIN Customer(并从那里删除该条件
WHERE
子句),使其与所有其他表相同!我在sql中遇到了这个错误:FROM子句中的对象“Customer”和“Customer”具有相同的公开名称。使用相关名称来区分它们。如果您将my line与
内部联接客户一起使用
-当然,您必须删除
,客户
来自您的
来自
声明!感谢您的回复。在Employee表中,employeeID是identity列。因此,我刚刚在我的视图中添加了这一行(order by Employee.employeeID)作为PK,现在实体模型显示了正确的数据。但是当我尝试查看SalesDetailView的设计视图时。它给出了错误消息:SQL文本无法在网格窗格和图表窗格中表示。我添加了此行:row_number()(order by Employee.EmployeeID)作为PK,在Employee表中,EmployeeID列是identity列。