-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-product-unit-null
65 lines (55 loc) · 1.5 KB
/
fix-product-unit-null
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
54
55
56
57
58
59
60
61
62
63
64
65
--Find Data
select ProductId, STRING_AGG(Convertion, '_') as Convertions
from ProductUnits
group by ProductId
having STRING_AGG(Convertion, '_') not like '%1.00%'
--==================================================
--Fix data
IF OBJECT_ID('tempdb..#Temp_Product') IS NOT NULL DROP TABLE #Temp_Product
select ProductId, STRING_AGG(Convertion, '_') as Convertions
INTO #Temp_Product
from ProductUnits
group by ProductId
having STRING_AGG(Convertion, '_') not like '%1.00%'
DECLARE @ProductId varchar(128)
DECLARE @UnitId int
DECLARE @UnitName varchar(128)
WHILE(SELECT count(*) FROM #Temp_Product) > 0
BEGIN
SELECT TOP 1 @ProductId = ProductId from #Temp_Product
SELECT @UnitId = UnitId from Products where Id = @ProductId
SELECT @UnitName = Name from Units where UnitId = @UnitId
INSERT INTO [dbo].[ProductUnits]
([Id]
,[ProductId]
,[UnitId]
,[UnitName]
,[Convertion]
,[CreatedTime]
,[ModifiedTime]
,[ModifiedBy]
,[CreatedAt]
,[UpdatedAt]
,[Deleted])
VALUES
(NEWID()
,@ProductId
,@UnitId
,@UnitName
,1
,GETDATE()
,GETDATE()
,'System'
,GETDATE()
,GETDATE()
,0)
DELETE #Temp_Product
WHERE ProductId = @ProductId
END
IF OBJECT_ID('tempdb..#Temp_Product') IS NOT NULL DROP TABLE #Temp_Product
--select * from ProductUnits
--where ModifiedBy = 'System'
--order by UpdatedAt desc
--select * from ProductUnits
--where ProductId = '5703421b-3e8a-4e73-b4e7-c50d56525191'
--order by Convertion