Violation of PRIMARY KEY constraint 'PK__FacVenta'. Cannot insert duplicate key in object
Publicado por ernesto (5 intervenciones) el 25/01/2020 23:29:48
Me trabe con este error al empezar a crear mi cubo dimensional a la hora de insertar datos de una bd a otra bd
todos se insertan normal el error es cuando intento insertar a la tabla FacVenta
Msg 2627, Level 14, State 1, Line 39
Violation of PRIMARY KEY constraint 'PK__FacVenta__C6F4FC51C4DAB3BD'. Cannot insert duplicate key in object 'dbo.FacVenta2'. The duplicate key value is (10248).
The statement has been terminated.
todos se insertan normal el error es cuando intento insertar a la tabla FacVenta
Msg 2627, Level 14, State 1, Line 39
Violation of PRIMARY KEY constraint 'PK__FacVenta__C6F4FC51C4DAB3BD'. Cannot insert duplicate key in object 'dbo.FacVenta2'. The duplicate key value is (10248).
The statement has been terminated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
create database DWFINAL
USE DWFINAL
CREATE TABLE DimEmpleado
(
CodEmp int primary key,
NomEmp varchar(26)
)
CREATE TABLE DimProducto
(
CodPro int primary key,
CodCat int references DimCategoria
NomPro varchar (130),
PrePro money
)
CREATE TABLE DimCategoria
(
CodCat int primary key,
NomCat varchar(40)
)
CREATE TABLE DimClientes
(
CodCli varchar(7) primary key,
Empr varchar(60),
CiuCli varchar(75),
RegCli varchar(23),
)
CREATE TABLE DimTiempo
(
CodDimTie int primary key,
FecTie date, --fecha
AnnTie int, --año
MesTie int, --mes
NomMes varchar(10), -- nombre del mes
DiaTie int, --dia
NmDiaTie varchar(9) --nombre del dia
)
CREATE TABLE FacVenta2
(
CodVen int primary key ,
CodPro int references DimProducto,
CodEmp int references DimEmpleado ,
CodCli varchar(7) references DimClientes,
CodDimTie int references DimTiempo ,
ImpVen money
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
--Origen
insert DWFINAL.dbo.DimEmpleado
select CodEmp,Nombre+''+Apellido
from Empleados
insert DWFINAL.dbo.DimCategoria
select CodCat,NombreCategoria
from Categorias
insert DWFINAL.dbo.DimProducto
select CodPro,CodCat,ProductName,PrecioUnitario
from Productos
insert DWFINAL.dbo.DimClientes
select CodCli,Empresa,Ciudad,Region
from Clientes
insert DWFINAL.dbo.DimTiempo
select distinct
year(FechaVenta)*10000+month(FechaVenta)*100+Day(FechaVenta),
FechaVenta,
year(FechaVenta),
month(FechaVenta),
datename(month,FechaVenta),
day(FechaVenta),
datename(weekday,FechaVenta)
from Venta
insert DWFINAL.dbo.FacVenta2
select VentaDetalle.CodVen,CodPro
CodEmp,
CodCli,
year(FechaVenta)*10000+month(FechaVenta)*100+Day(FechaVenta),
sum(PrecioUnitario*Cantidad)--calculo
from Venta
inner join VentaDetalle
on Venta.CodVen=VentaDetalle.CodVen
group by VentaDetalle.CodVen,CodPro,CodEmp,CodCli,
year(FechaVenta)*10000+month(FechaVenta)*100+Day(FechaVenta)
Valora esta pregunta


0