首页 > 解决方案 > 从另一个表更新 SQL - 大搞砸了

问题描述

我有一个 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 子句应该这样做,但我今天丢失的。

非常感谢任何和所有帮助。

标签: sqlsql-servertsqlsql-update

解决方案


你需要加入两个表:

UPDATE V
SET 
  V.EmploymentStatus = ...
...

FROM VETTING V
INNER JOIN dbo.ApplicationForm af
ON af.Company_ID =V.Company_ID 
WHERE af.Company_ID = @CompanyId;

推荐阅读