Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 如何将密钥从一个数据库插入到另一个数据库?_Sql Server_Tsql - Fatal编程技术网

Sql server 如何将密钥从一个数据库插入到另一个数据库?

Sql server 如何将密钥从一个数据库插入到另一个数据库?,sql-server,tsql,Sql Server,Tsql,下面是我将值从AdventureWorks插入自定义数据库的代码。 我也需要从AdventureWorks传输标识符,但不允许复制标识符。所以我认为使用distinct和identifier就足够了,但在插入重复项时仍然会出错 如果你能评论并告诉我我做错了什么,我将非常感激。 谢谢大家! 以下是我的全部尝试: “关于插入重复项”的错误是什么?运行Select语句并查看数据。你应该在那里看到重复。哦,我在pastebin链接中发布了错误消息,它违反了主键约束“PK_ProizvodID”。无法在对

下面是我将值从AdventureWorks插入自定义数据库的代码。 我也需要从AdventureWorks传输标识符,但不允许复制标识符。所以我认为使用distinct和identifier就足够了,但在插入重复项时仍然会出错

如果你能评论并告诉我我做错了什么,我将非常感激。 谢谢大家!

以下是我的全部尝试:


“关于插入重复项”的错误是什么?运行Select语句并查看数据。你应该在那里看到重复。哦,我在pastebin链接中发布了错误消息,它违反了主键约束“PK_ProizvodID”。无法在对象“dbo.proizvodi”中插入重复密钥。重复的键值是(747)。请查看列
pii.Quantity
。这会导致产品重复行(
product
table与
ProductInventory
table有1:m关系)。如果不需要,可以删除该列;如果需要,可以进行聚合以获得产品的总数量。
set identity_insert proizvodi ON

insert into proizvodi (ProizvodID, Sifra, Naziv, Kategorija, Podkategorija, Boja, Cijena, StanjeZaliha)
    select distinct 
        p.ProductID, p.ProductNumber,p.[Name], pc.[Name],psc.[Name], 
        p.Color, p.ListPrice, pii.Quantity
    from 
        AdventureWorks2017.Production.Product as p
    join 
        AdventureWorks2017.Production.ProductSubcategory as psc on psc.ProductSubcategoryID = p.ProductSubcategoryID
    join 
        AdventureWorks2017.Production.ProductCategory as pc on pc.ProductCategoryID = psc.ProductCategoryID
    inner join 
        AdventureWorks2017.Production.ProductInventory as pii on pii.ProductID = p.ProductID
    inner join 
        AdventureWorks2017.Sales.SalesOrderDetail as sod on sod.ProductID =  p.ProductID
    inner join 
        AdventureWorks2017.Sales.SalesOrderHeader as soh on soh.SalesOrderID = sod.SalesOrderID
    inner join 
        AdventureWorks2017.Sales.SalesTerritory as st on st.TerritoryID = soh.TerritoryID
    inner join 
        AdventureWorks2017.Production.[Location] as l on l.LocationID = pii.LocationID
    where 
        st.[Group] ='Europe'
    order by 
        p.ProductID

    set identity_insert proizvodi OFF