Error in Sql store procedure

ⅰ亾dé卋堺 提交于 2020-01-17 06:09:46

问题


INSERT INTO Tbl_Basicinfo(Employer_Id,Lname,Fname,UserName,Userpass,Location_Id,cc2,CC3,cc4,cc5,Dohire,Job_Status,ssn,Import_Emp_No,[Benefit_Terminate], [Show_Age], [Change_IP], [IsNewHired])
            SELECT M.Employer_Id,M.LASTNAME,M.FIRSTNAME,UPPER(LEFT(ISNULL(M.FIRSTNAME,'A'),1)+''+LEFT(ISNULL(M.LASTNAME,'C'),1))+CONVERT(VARCHAR,MaxImportEENumber+ROWNUM) [UserName],RIGHT(NEWID(),10) [UserPass],ISNULL(M.Location_Id,0)[Location_Id],M.Level2code,M.Level3code,M.LEVEL4Code,M.LEVEL5Code,M.DATEOFHIRE,
            1 [JobStatus],SSN,M.MaxImportEENumber+ROWNUM [ImportEmpNo],0 [Benefit_Terminate], 0[Show_Age],M.ChangeIP,1 [IsNewHired] from(
            SELECT ROW_NUMBER() OVER(ORDER BY [FIRSTNAME]) AS ROWNUM,* FROM
            (
                    SELECT DISTINCT @Employer_Id [Employer_Id],PR.LASTNAME,PR.FIRSTNAME,(SELECT  MAX(CAST(ISNULL(Employee_Id,0) as BIGINT)) from Tbl_Basicinfo) [MaxImportEENumber],
                    (SELECT TOP 1 Location_Id from Tbl_Location where Location_Code=PR.LEVEL1CODE and Employer_Id=@Employer_Id and Location like '%'+PR.LEVEL1DESCRIPTION+'%')  [Location_Id],
                    (SELECT TOp 1 LEVEL2Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL2Code,
                    (SELECT TOp 1 LEVEL3Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL3Code,
                    (SELECT TOp 1 LEVEL4Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL4Code,
                    (SELECT TOp 1 LEVEL5Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL5Code,
                    (SELECT MIN(DATEOFHIRE) from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) DATEOFHIRE,1 Job_Status,PR.SSN,PR.ChangeIP

                        from Tbl_PPACA_Import PR
                        LEFT OUTER JOIN Tbl_Basicinfo L ON L.ssn=PR.SSN and L.Employer_Id=PR.EmployerId
                        WHERE PR.EmployerId=@Employer_Id and L.SSN is null and ISNULL(PR.SSN,'') not like '' AND ISNUMERIC(PR.SSN)=1
                        and ISNULL(PR.PPE,DATEOFHIRE) =(SELECT MAX(ISNULL(PPE,DATEOFHIRE)) FROM Tbl_PPACA_Import I WHERE I.SSN=PR.SSN AND I.EmployerId=PR.EmployerId ) 

                    ) V
            ) M;

Note: Can some one help me I am receiving this error from above query:

Error 515: Cannot insert the value NULL into column 'UserName', table 'ppacahelper.dbo.Tbl_Basicinfo'; column does not allow nulls. INSERT fails. Line: 131 spImportPPACAREPORT

Any help will highly be apprecieted


回答1:


I think you need to add ISNULL to all the columns you are concatenating for username value:

INSERT INTO Tbl_Basicinfo(Employer_Id,Lname,Fname,UserName,Userpass,Location_Id,cc2,CC3,cc4,cc5,Dohire,Job_Status,ssn,Import_Emp_No,[Benefit_Terminate], [Show_Age], [Change_IP], [IsNewHired])
        SELECT M.Employer_Id,M.LASTNAME,M.FIRSTNAME,UPPER(LEFT(ISNULL(M.FIRSTNAME,'A'),1)+''+LEFT(ISNULL(M.LASTNAME,'C'),1))+CONVERT(VARCHAR,ISNULL(MaxImportEENumber,0)+ISNULL(ROWNUM,0)) [UserName],RIGHT(NEWID(),10) [UserPass],ISNULL(M.Location_Id,0)[Location_Id],M.Level2code,M.Level3code,M.LEVEL4Code,M.LEVEL5Code,M.DATEOFHIRE,
        1 [JobStatus],SSN,M.MaxImportEENumber+ROWNUM [ImportEmpNo],0 [Benefit_Terminate], 0[Show_Age],M.ChangeIP,1 [IsNewHired] from(
        SELECT ROW_NUMBER() OVER(ORDER BY [FIRSTNAME]) AS ROWNUM,* FROM
        (
                SELECT DISTINCT @Employer_Id [Employer_Id],PR.LASTNAME,PR.FIRSTNAME,(SELECT  MAX(CAST(ISNULL(Employee_Id,0) as BIGINT)) from Tbl_Basicinfo) [MaxImportEENumber],
                (SELECT TOP 1 Location_Id from Tbl_Location where Location_Code=PR.LEVEL1CODE and Employer_Id=@Employer_Id and Location like '%'+PR.LEVEL1DESCRIPTION+'%')  [Location_Id],
                (SELECT TOp 1 LEVEL2Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL2Code,
                (SELECT TOp 1 LEVEL3Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL3Code,
                (SELECT TOp 1 LEVEL4Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL4Code,
                (SELECT TOp 1 LEVEL5Code from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) LEVEL5Code,
                (SELECT MIN(DATEOFHIRE) from Tbl_PPACA_Import where ssn=PR.SSN and EmployerId=PR.EmployerId) DATEOFHIRE,1 Job_Status,PR.SSN,PR.ChangeIP

                    from Tbl_PPACA_Import PR
                    LEFT OUTER JOIN Tbl_Basicinfo L ON L.ssn=PR.SSN and L.Employer_Id=PR.EmployerId
                    WHERE PR.EmployerId=@Employer_Id and L.SSN is null and ISNULL(PR.SSN,'') not like '' AND ISNUMERIC(PR.SSN)=1
                    and ISNULL(PR.PPE,DATEOFHIRE) =(SELECT MAX(ISNULL(PPE,DATEOFHIRE)) FROM Tbl_PPACA_Import I WHERE I.SSN=PR.SSN AND I.EmployerId=PR.EmployerId ) 

                ) V
        ) M;



回答2:


It seems that this expression returns NULL:

UPPER(LEFT(ISNULL(M.FIRSTNAME,'A'),1)+''
+LEFT(ISNULL(M.LASTNAME,'C'),1))
+CONVERT(VARCHAR,MaxImportEENumber+ROWNUM)

The first part will not be NULL due to the ISNULL on M.FIRSTNAME The second part will not be NULL due to the ISNULL on M.LASTNAME So it must be the third part...

Either MaxImportEENumber or ROWNUM must be NULL...

Please check this...

You can easily define a default by writing

Attention: you probably want to change the CONVERT(VARCHAR to CONVERT(VARCHAR(XY). Without specifying a length you won't get what you expect...

+ISNULL(CONVERT(VARCHAR(100),MaxImportEENumber+ROWNUM),'YourDefaultValue')


来源:https://stackoverflow.com/questions/33746127/error-in-sql-store-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!