【发布时间】:2019-12-12 14:18:06
【问题描述】:
我有一个需要根据申请表结果更新的 SQL 表。
但是,我错过了 where 子句中最简单的内容,并设法覆盖了所有表数据。对我来说幸运的是,我们能够恢复这些信息,但是,我做了一个嘘声,现在需要修复它并使其更强大,这就是我需要帮助的地方。
UPDATE VETTING
SET EmploymentStatus = CASE
WHEN af.Company_Status = 2 THEN
0
ELSE
CASE
WHEN af.Company_Status = 1
OR af.Company_Status = 4 THEN
1
ELSE
NULL
END
END,
CompanyLtdName = @CompanyId,
CompanyRegistrationNo = af.Company_Number,
VATRegistered_Ind = CASE
WHEN af.boolVATRegistered = 0
AND
(
af.VATNumber IS NOT NULL
AND af.VATNumber <> ''
) THEN
1
ELSE
0
END,
VATRegistration = CASE
WHEN af.VATNumber IS NULL
OR af.VATNumber = '' THEN
NULL
ELSE
SUBSTRING(
REPLACE(af.VATNumber, ' ', ''),
PATINDEX('%[0-9]%', REPLACE(af.VATNumber, ' ', '')),
LEN(REPLACE(af.VATNumber, ' ', ''))
- PATINDEX('%[0-9]%', REPLACE(af.VATNumber, ' ', ''))
)
END,
NoCCJs = af.App_HasCCJ,
NoCCJsDetails = af.App_HasCCJ,
NoDrugs = af.App_HasPastConviction,
NoDrugsDetails = af.App_PastConvictions,
Work_24Hour_Ind = af.Service_24hr,
Work_Commercial_Ind = af.Service_Commercial,
Work_Domestic_Ind = af.Service_Domestic,
Work_FreeEstimates_Ind = af.Service_Estimates,
Work_FreeEstimatesExceptIns_Ind = af.Service_EstimatesExclude,
Cards_Accepted_Ind = af.Service_Credit,
Insurance_Work_Undertaken_Ind = af.Service_Insurance,
PLInsurance_Ind = af.Vet_HasPLI,
PLAmount = af.Vet_PLIAmount,
PLPolicyNo = af.Vet_PLINumber,
PLExpiry_Dt = af.Vet_PLIExpDt,
PLIsCombined = af.Vet_PLI_Employee,
PLCost = af.Vet_PLICost,
PLInsuranceCompanyName = af.Vet_PLIProvider,
ELAmount = af.Vet_ELIAmount,
ELPolicyNo = af.Vet_ELINumber,
ELExpiry_Dt = af.Vet_ELIExpDt,
ELCost = af.Vet_ELICost,
ELInsuranceCompanyName = af.Vet_ELIProvider,
ExperienceStartDate = af.Accred_ExperienceStartDt
FROM dbo.ApplicationForm af
WHERE af.Company_ID = @CompanyId;
我还需要使用 where 子句,其中审核表公司 ID 也与 @CompanyId 匹配,并且我有一个代码盲时刻,我认为只有一个 and Vetting.Company_Id = @companyId 子句应该这样做但是我今天迷路了。
非常感谢任何和所有帮助。
【问题讨论】:
-
您没有尝试将查询简化为能说明问题的最小示例。请阅读MCVE 并相应地编辑您的问题。将大量代码复制粘贴到问题中并要求我们对其进行调试对站点没有用处,实际上是题外话。另请阅读How to ask。
标签: sql sql-server tsql sql-update