ALTER PROCEDURE [dbo].[UpdateValidicBiometricsData_Test]
AS
BEGIN
DECLARE @intErrorCode INT
BEGIN TRAN
SELECT * INTO #ValidicBiometricData_Stg FROM ValidicBiometricData_Stg
DELETE FROM #ValidicBiometricData_Stg WHERE (diastolic > 400 and systolic > 400 and resting_heartrate > 400) OR ActivityDate > GETDATE()
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.IsDuplicate = 1
FROM #ValidicBiometricData_Stg STG
INNER JOIN ValidicBiometricData T ON
STG.user_id = T.user_id AND STG.systolic=T.systolic AND STG.diastolic= T.diastolic AND
STG.resting_heartrate=T.resting_heartrate AND STG.ActivityDate=T.ActivityDate
--AND DATEDIFF(d,T.ActivityDate,GETDATE()) <= 5
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
MERGE ValidicBiometricData AS T
USING #ValidicBiometricData_Stg AS S
ON T.ID = S.ID AND T.USER_ID = S.USER_ID
WHEN MATCHED THEN
UPDATE SET
T.ActivityDateUTC = S.ActivityDateUTC,
T.ActivityDate = S.ActivityDate,
T.BLOOD_CALCIUM = S.BLOOD_CALCIUM,
T.blood_chromium = S.blood_chromium,
T.blood_folic_acid = S.blood_folic_acid,
T.blood_magnesium = S.blood_magnesium,
T.blood_potassium = S.blood_potassium,
T.blood_sodium = S.blood_sodium,
T.blood_vitamin_b12 = S.blood_vitamin_b12,
T.blood_zinc = S.blood_zinc,
T.creatine_kinase = S.creatine_kinase,
T.crp = S.crp,
T.diastolic = S.diastolic,
T.ferritin = S.ferritin,
T.hdl = S.hdl,
T.hscrp = S.hscrp,
T.iL6 = S.iL6,
T.ldl = S.ldl,
T.resting_heartrate = S.resting_heartrate,
T.systolic = S.systolic,
T.testosterone = S.testosterone,
T.total_cholesterol = S.total_cholesterol,
T.tsh = S.tsh,
T.uric_acid = S.uric_acid,
T.vitamin_d = S.vitamin_d,
T.white_cell_count = S.white_cell_count,
T.source = S.source,
T.source_name = S.source_name,
T.last_updated_UTC = S.last_updated_UTC,
T.last_updated = S.last_updated,
T.UTC_OFFSET = S.UTC_OFFSET
WHEN NOT MATCHED BY TARGET THEN
INSERT (
ID,
ActivityDateUTC,
ActivityDate,
BLOOD_CALCIUM,
blood_chromium,
blood_folic_acid,
blood_magnesium,
blood_potassium,
blood_sodium,
blood_vitamin_b12,
blood_zinc,
creatine_kinase,
crp, diastolic,
ferritin,
hdl,
hscrp,
iL6,
ldl,
resting_heartrate,
systolic,
testosterone,
total_cholesterol,
tsh,
uric_acid,
vitamin_d,
white_cell_count,
source,
source_name,
last_updated_UTC,
last_updated,
user_id,
UTC_OFFSET,
IsDuplicate
)
VALUES (
S.ID,
S.ActivityDateUTC,
S.ActivityDate,
S.BLOOD_CALCIUM,
S.blood_chromium,
S.blood_folic_acid,
S.blood_magnesium,
S.blood_potassium,
S.blood_sodium,
S.blood_vitamin_b12,
S.blood_zinc,
S.creatine_kinase,
S.crp,
S.diastolic,
S.ferritin,
S.hdl,
S.hscrp,
S.iL6,
S.ldl,
S.resting_heartrate,
S.systolic,
S.testosterone,
S.total_cholesterol,
S.tsh,
S.uric_acid,
S.vitamin_d,
S.white_cell_count,
S.source,
S.source_name,
S.last_updated_UTC,
S.last_updated,
S.user_id,
S.UTC_OFFSET,
S.IsDuplicate
);
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
DELETE FROM #ValidicBiometricData_Stg WHERE IsDuplicate = 1
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE #ValidicBiometricData_Stg SET ActivityDate = DATEADD(HOUR, -7 , ActivityDate )
WHERE source = 'withings'
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
ALTER TABLE #ValidicBiometricData_Stg
ADD SBPGoalID INT,
SBPGoalDiaryID INT,
DBPGoalID INT,
DBPGoalDiaryID INT ,
Token VARCHAR(30),
ActivityDate1 DATETIME,
Ntoken VARCHAR(30),
Nactivitydate DATETIME
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.SBPGoalID = G.ID
FROM #ValidicBiometricData_Stg STG
INNER JOIN HRA_EMP_ADMIN..User_Identities UI ON UI.UUID = 'VALIDIC_ID' AND STG.[User_ID] = UI.[User_ID]
INNER JOIN ActionPlanWSdb..UserProgramInfo UPI ON UI.[User_ID] = UPI.token
INNER JOIN ActionPlanWSdb..Goals G ON UPI.ActionItemID = G.ActionItemID
WHERE G.GoalTemplateID = 25
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.SBPGoalDiaryID = G.ID
FROM #ValidicBiometricData_Stg STG
INNER JOIN HRA_EMP_ADMIN..User_Identities UI ON UI.UUID = 'VALIDIC_ID' AND STG.[User_ID] = UI.[User_ID]
INNER JOIN ActionPlanWSdb..UserProgramInfo UPI ON UI.[User_ID] = UPI.token
INNER JOIN ActionPlanWSdb..Goals G ON UPI.ActionItemID = G.ActionItemID
INNER JOIN CoachingPortal_PROD..ValidicBiometricData VB on UI.uid = VB.[User_ID]
INNER JOIN ActionPlanWSdb..goalDiary GD ON GD.goalID = G.id AND GD.vid = VB.ID
WHERE G.GoalTemplateID = 25
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.DBPGoalID = G.ID
FROM #ValidicBiometricData_Stg STG
INNER JOIN HRA_EMP_ADMIN..User_Identities UI ON UI.UUID = 'VALIDIC_ID' AND STG.[User_ID] = UI.[User_ID]
INNER JOIN ActionPlanWSdb..UserProgramInfo UPI ON UI.[User_ID] = UPI.token
INNER JOIN ActionPlanWSdb..Goals G ON UPI.ActionItemID = G.ActionItemID
WHERE G.GoalTemplateID = 26
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.DBPGoalDiaryID = G.ID
FROM #ValidicBiometricData_Stg STG
INNER JOIN HRA_EMP_ADMIN..User_Identities UI ON UI.UUID = 'VALIDIC_ID' AND STG.[User_ID] = UI.[User_ID]
INNER JOIN ActionPlanWSdb..UserProgramInfo UPI ON UI.[User_ID] = UPI.token
INNER JOIN ActionPlanWSdb..Goals G ON UPI.ActionItemID = G.ActionItemID
INNER JOIN CoachingPortal_PROD..ValidicBiometricData VB on UI.uid = VB.[User_ID]
INNER JOIN ActionPlanWSdb..goalDiary GD ON GD.goalID = G.id AND GD.vid = VB.ID
WHERE G.GoalTemplateID = 26
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
MERGE ActionPlanWSdb..goalDiary AS T
USING #ValidicBiometricData_Stg AS S
ON T.ID = S.SBPGoalDiaryID
WHEN MATCHED THEN
UPDATE SET
T.goalid = S.SBPGoalID,
T.ActivityDate = S.ActivityDate,
T.ProgressGoal = S.systolic,
T.LastUpdated = GETDATE(),
T.Note = '',
T.VID= S.ID
WHEN NOT MATCHED BY TARGET AND S.SBPGoalID IS NOT NULL THEN
INSERT (goalid, activitydate, progressGoal, note, vid, lastUpdated)
VALUES (S.SBPGoalID, S.ActivityDate , S.systolic, '', S.ID, GETDATE() );
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
MERGE ActionPlanWSdb..goalDiary AS T
USING #ValidicBiometricData_Stg AS S
ON T.ID = S.DBPGoalDiaryID
WHEN MATCHED THEN
UPDATE SET
T.goalid = S.DBPGoalID,
T.ActivityDate = S.ActivityDate,
T.ProgressGoal = S.diastolic,
T.LastUpdated = GETDATE(),
T.Note = '',
T.VID= S.ID
WHEN NOT MATCHED BY TARGET AND S.DBPGoalID IS NOT NULL THEN
INSERT (goalid, activitydate, progressGoal, note, vid , lastUpdated )
VALUES (S.DBPGoalID, S.ActivityDate , S.diastolic, '', S.ID , GETDATE() );
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.Token = UI.User_ID
FROM #ValidicBiometricData_Stg STG
INNER JOIN ActionPlanWSdb..goalDiary GD ON STG.ID = GD.id
INNER JOIN HRA_EMP_ADMIN..User_Identities UI ON STG.USER_ID = UI.UID
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET STG.ActivityDate1= T.ActivityDate
FROM #ValidicBiometricData_Stg STG
INNER JOIN
(SELECT u.token, max(gd.activityDate) AS ActivityDate
FROM ActionPlanWSdb..UserProgramInfo U
INNER JOIN ActionPlanWSdb..Goals G ON U.actionitemID = G.ActionItemID
INNER JOIN ActionPlanWSdb..goalDiary GD ON G.ID = GD.goalID AND GD.progressGoal IS NOT NULL AND GD.progressGoal <> 0
WHERE (G.GoalTemplateID = 25 AND (GD.progressGoal >= 160 OR GD.progressGoal <= 90)) OR
(G.GoalTemplateID = 26 AND (GD.progressGoal >= 100 OR GD.progressGoal <= 55))
GROUP BY U.token) AS T
ON STG.TOKEN = T.TOKEN
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
UPDATE STG SET
STG.Ntoken = T.Token,
STG.NActivitydate= T.ActivityDate
FROM #ValidicBiometricData_Stg STG
INNER JOIN
(
SELECT token,isnull(max(date), max(CreatedDate)) AS activitydate from coachingportal_prod..Notifications
WHERE NotificationTypeID=7 and isread<>0
GROUP BY token
) AS T
ON STG.Token = T.Token
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
MERGE coachingportal_prod..Notifications AS T
USING #ValidicBiometricData_Stg AS S
ON S.TOKEN = T.TOKEN AND T.NotificationTypeID=7 AND T.isread=0
AND (S.Ntoken IS NULL OR (S.Nactivitydate<S.Activitydate1 and S.Nactivitydate IS NOT NULL AND S.Ntoken IS NOT NULL))
AND S.user_id IS NOT NULL
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
Token,
NotificationTypeID,
[Date],
CreatedDate,
LastUpdatedDate,
isRead,
UserID,
ReadDate
)
VALUES
(
S.Token,
7,
S.ActivityDate1,
GETDATE(),
GETDATE(),
0,
0,
NULL
);
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
ALTER TABLE #ValidicBiometricData_Stg
DROP COLUMN SBPGoalID ,
SBPGoalDiaryID ,
DBPGoalID ,
DBPGoalDiaryID ,
Token,
ActivityDate1 ,
Ntoken ,
Nactivitydate
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
--TRUNCATE TABLE ValidicBiometricData_Stg
--SELECT @intErrorCode = @@ERROR
-- IF (@intErrorCode <> 0) GOTO PROBLEM
DROP TABLE #ValidicBiometricData_Stg
SELECT @intErrorCode = @@ERROR
IF (@intErrorCode <> 0) GOTO PROBLEM
COMMIT TRAN
PROBLEM:
IF (@intErrorCode <> 0) BEGIN
PRINT 'Unexpected error occurred!'
ROLLBACK TRAN
END
END
毕竟当我尝试执行它时,它会发出以下错误
Msg 8114,Level 16,State 5,Procedure UpdateValidicBiometricsData_Test,Line 269
将数据类型varchar转换为数字时出错。
答案 0 :(得分:1)
我想这里是错误
您正在将
''
值插入vid(我猜是integer
数据类型)@line number 268
WHEN NOT MATCHED BY TARGET AND S.DBPGoalID IS NOT NULL THEN
INSERT (goalid, activitydate, progressGoal, note, vid , lastUpdated )
VALUES (S.DBPGoalID, S.ActivityDate , S.diastolic,, S.ID , GETDATE() );
尝试在没有单引号'
的情况下给出null或空值或者您可能是数据类型为integer
的不同列名,并且您在其中插入了不同的值