TSQL帮助| INSERT语句的选择列表包含的项目少于插入列表

时间:2012-10-07 09:05:01

标签: sql sql-server tsql

请参阅下面的查询,我已经多次计算了这些列,并且我已经检查了逗号,它们似乎没问题,但我继续收到错误

INSERT语句的选择列表包含的项目少于插入列表。 SELECT值的数量必须与INSERT列的数量匹配。

DECLARE @StatusValues TABLE (StatusId INT,StatusText VARCHAR(50))

INSERT INTO @StatusValues VALUES(1,'Code 1 - Entered E&D Waiver');
INSERT INTO @StatusValues VALUES(2,'Code 2 - Nursing Facility');
INSERT INTO @StatusValues VALUES(3,'Code 3 - Entered Assisted Living Waiver');
INSERT INTO @StatusValues VALUES(4,'Code 4 - Entered TBI/SCI Waiver');
INSERT INTO @StatusValues VALUES(5,'Code 5 - Entered IL Waiver');
INSERT INTO @StatusValues VALUES(6,'Code 6 - DECEASED');
INSERT INTO @StatusValues VALUES(7,'Code 7 - Refused Waiver Services');
INSERT INTO @StatusValues VALUES(8,'Code 8 - Other (Explain)');
INSERT INTO @StatusValues VALUES(NULL,'Please select');


INSERT INTO SWMPDD.dbo.Clients(
    AdditionalPertinent,
    Address1,
    Address2,
    ApplicationApprovedBy,
    ByWhom,
    City,
    ClientAt,
    ContactPerson,
    ContactPhone,
    County,
    DateClientContacted,
    Diagnostic,
    Diet,
    Direction,
    DateOfBirth,
    Email,
    FirstName,
    Gender,
    InOtherCase,
    InTakeDate,
    IPAddress,
    LastName,
    LastUpdateUser,
    LastUpdateTime,
    LockinStatus,
    Medicaid,
    Medicare,
    MethodofContact,
    MiddleInitial,
    OfficalComments,
    OtherComments,
    ParticipantId,
    ParticipantSignature,
    PersonResidenceCode,
    Phone,
    Physician,
    PhysicianAddress,
    PhysicianCity,
    PhysicianPhone,
    PhysicianZip,
    CreationDate,
    ReferralPhone,
    ReferralSoruce,
    RelationshipToClient,
    SignatureDate,
    SSN,
    [State],
    CreationUser,
    VerificationDate,
    VerificationOfMedicaidStatus,
    Zip,
    ClientId,
    StatusId,
    StatusText
)
SELECT 
    AdditionalPertinent,
    Address1,
    Address2,
    ApplicationApprovedBy,
    ByWhom,
    City,
    ClientAt,
    ContactPerson,
    ContactPhone,
    county,
    DateClientContacted,
    Diagnostic,
    Diet,
    Direction,
    DOB,
    EmailAddress,
    FirstName,
    Gender,
    InOtherCase,
    InTakeDate,
    ipaddress,
    LastName,
    lastUpdatedBy,
    lastupdatedDate,
    LockinStatus,
    Medicaid,
    Medicare,
    MethodofContact,
    MiddleInit,
    OfficalComments,
    OtherComments,
    ParticipantId,
    ParticipantSignature,
    PersonResidenceCode,
    phone,
    Physician,
    PhysicianAddress,
    PhysicianCity,
    PhysicianPhone
    PhysicianZip,
    recDate,
    ReferralPhone,
    ReferralSoruce,
    RelationshiptoClient,
    SignatureDate,
    SSN,
    [state],
    userid,
    VerificationDate,
    VerificationofMedicaidStatus,
    zip,
    NEWID(),
    (SELECT StatusId FROM @StatusValues WHERE StatusText = ReasonforRemovalCode) abc,
    Code8Other
FROM msdepart.dbo.tblParticipant;

1 个答案:

答案 0 :(得分:23)

由于此处缺少逗号,SELECT部分​​和INSERT(..列列表..)中的字段列表为1:

PhysicianPhone
PhysicianZip,

在SELECT中。它是别名Phone作为Zip,其他一切都不合时宜。