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

Posted

技术标签:

【中文标题】TSQL 帮助 | INSERT 语句的选择列表包含的项目少于插入列表 [关闭]【英文标题】:TSQL Help | The select list for the INSERT statement contains fewer items than the insert list [closed] 【发布时间】:2012-10-07 09:05:01 【问题描述】:

请看下面我的查询,我已经计算了很多次列,也检查了逗号,它们似乎很好,但我不断收到错误

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】:

由于此处缺少逗号,您的 SELECT 部分和 INSERT (..column list..) 中的字段列表减少了 1:

PhysicianPhone
PhysicianZip,

在选择中。它将 Phone 别名为 Zip,其他所有内容都不同步。

【讨论】:

非常感谢,我想我已经检查了所有的逗号。 :D 太久了,还有几遍 :)

以上是关于TSQL 帮助 | INSERT 语句的选择列表包含的项目少于插入列表 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

SQL 语句插入

sql tsql__bulk_insert.sql

SSIS包定时执行

TSQL 到 LINQ 转换参考?

没有列列表的 INSERT 语句的错误

MySQL中INSERT,UPDATE和REPLACE的区别与用法