【问题标题】:Insert into multiple selects插入多个选择
【发布时间】:2016-09-14 23:21:35
【问题描述】:

我需要将这些选择的值插入到表中。 它们返回许多行,对于每一行,我需要在新表中插入一列。

例如:

select (select id from X where name=Contacts.Company) as IDClient, 
FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName, 
(select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole, 
Initials
from Contacts

我需要为这个选择的每一行做这样的事情:

insert into TableX (IDClient,NewName,IDRole,'0','0','0',Initials,'XXX',GETDATE(),NULL,NULL)

已编辑

insert into TableX (IDClient,NewName,IDRole,'0','0','0',Initials,'XXX',GETDATE(),NULL,NULL)
select 
(select id from X where nAme=Contacts.Company) as IDClient, 
FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName, 
(select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole,
Initials
from Contacts

当我执行这个我得到这个错误:

“'0' 附近的语法不正确。”

【问题讨论】:

    标签: sql sql-server insert


    【解决方案1】:

    除非我遗漏了什么,否则你应该可以这样做:

    insert into Table (IDclient,NewName,IDRole,Initials)
    select (select id from X where name=Contacts.Company) as IDClient, 
      FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName, 
      (select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole, 
      Initials
    from Contacts
    

    您将只使用INSERT INTO...SELECT...FROM.. 语法。

    现在如果你没有表,那么你可以SELECT..INTO一个新的临时表:

    select (select id from X where name=Contacts.Company) as IDClient, 
      FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName, 
      (select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole, 
      Initials
    into #table
    from Contacts
    

    或者如果你想为此使用连接:

    insert into Table (IDclient,NewName,IDRole,Initials)
    select x.id as IDClient, 
      c.FirstName + right(' '+ cast(c.LastName as varchar(20)), 20) as NewName, 
      y.id as IDRole, 
      c.Initials
    from Contacts c
    inner join x
      on x.name=c.Company
    inner join y
      on y.description=c.JobTitle
      and y.newid=8
    

    现在您的原始帖子显示了以下内容的插入:

     insert into Table (IDclient,NewName,'',IDRole,Initials,NULL)
    

    您有两个没有名称的字段,这是不正确的语法。在您的插入语句中,要么必须命名任何列并插入所有列,要么命名要插入值的列。您不能使用空字符串 ''null 作为列名。如果要插入这些值,则必须提供它们的名称:

    insert into Table (IDclient,NewName,col3,IDRole,Initials,col5)
    select x.id as IDClient, 
      c.FirstName + right(' '+ cast(c.LastName as varchar(20)), 20) as NewName, 
      '' as col3
      y.id as IDRole, 
      c.Initials,
      null as col5
    from Contacts c
    inner join x
      on x.name=c.Company
    inner join y
      on y.description=c.JobTitle
      and y.newid=8
    

    根据您的编辑,您需要使用以下内容:

    -- this insert line should state column names not '0', '0', etc
    -- replace these with the names of your columns you are inserting into like the others,
    -- then place these values that you want to insert in the select list
    insert into TableX (IDClient,NewName,IDRole,'0','0','0',Initials,'XXX',GETDATE(),NULL,NULL)
    select 
      (select id from X where nAme=Contacts.Company) as IDClient, 
      FirstName + right(' '+ cast(LastName as varchar(20)), 20) as NewName, 
      (select id from Y where newid=8 and description=Contacts.JobTitle) as IDRole,
      '0',
      '0',
      '0',
      Initials,
      'XXX',
      getdate(),
      null,
      null
    from Contacts
    

    【讨论】:

    • 当我使用上面的代码时,我得到一个“'0'附近的语法不正确”
    • 在您的INSERT 中,您必须使用列名而不是getdate(),等等这些值进入语句的选择部分,请参阅我使用的最后一个查询。
    猜你喜欢
    • 2014-09-04
    • 2016-10-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多