【问题标题】:Insert data dynamically in a temporary table from two tables将两个表中的数据动态插入到临时表中
【发布时间】:2017-05-23 20:52:40
【问题描述】:

我想从 table1 和 table2 下面的两个不同表中动态地将数据插入到临时表 @temp 中

表一:

表 2:

Declare @temp table ( Pays nvarchar (300), Factory nvarchar (300), Count bigint)

我尝试了这个命令,但出现错误消息:Subquery returned more than 1 value.

Insert into @temp select (select pays from table1),(select factory,count from table 2  where @Pays=’yes’)

结果应该如下表@temp

感谢您的帮助。

【问题讨论】:

  • 你怎么知道 Factory SUX、DRV 和 HK 在法国而不是美国?
  • 您正在寻找一个单一的选择来提供您的插入,并连接 Table1 和 Table2。顺便说一句,您还没有解释这两者之间的关系。
  • 映射表在哪里,上面写着“SUX”去“法国”
  • 这里没有任何东西可以说明这些表是如何连接在一起的。在您想要的输出中,您有示例数据中不存在的行。我会敦促您阅读您发布的内容,并问问自己您是否认为自己能够回答这个问题。然后去这里 (spaghettidba.com/2015/04/24/…) 看看如何改进你的问题。
  • 我已经编辑了我的问题,请看一下,我很难解释我实际需要什么,所以请理解

标签: sql sql-server tsql ssrs-2008 reportbuilder3.0


【解决方案1】:

您希望join (documentation here) 运行单个选择语句。

您可能还想阅读this,因为表变量 (@table) 和临时表 (#table) 之间存在重要区别

【讨论】:

    【解决方案2】:

    如果必须的话,首先不要使用像Count这样的关键字作为属性,然后像这样使用-->[Count]

    DROP TABLE IF EXISTS table1
    create table table1 (Pays nvarchar(300))
    DROP TABLE IF EXISTS table2
    create table table2 (Factory nvarchar(300), [Count] bigint)
    
    /*temp table in sql should be created like this*/
    create table #temp (Pays nvarchar(300),Factory nvarchar(300), [Count] bigint)
    /*table variable should be created like this*/
    Declare @temp table ( Pays nvarchar (300), Factory nvarchar (300), [Count] bigint)
    
    
    insert into #temp(Pays) select Pays
    from table1
    insert into #temp(Factory,[Count]) select Factory,[Count]
    from table2
    
    Insert into @temp select Pays,Factory,[Count] from #temp
    where Pays='yes'
    go

    【讨论】:

      【解决方案3】:

      如果必须的话,首先不要使用像Count这样的关键字作为属性,然后像这样使用-->[Count]

      DROP TABLE IF EXISTS table1
      create table table1 (Pays nvarchar(300))
      DROP TABLE IF EXISTS table2
      create table table2 (Factory nvarchar(300), [Count] bigint)
      
      /*temp table in sql should be created like this*/
      create table #temp (Pays nvarchar(300),Factory nvarchar(300), [Count] bigint)
      /*table variable should be created like this*/
      Declare @temp table ( Pays nvarchar (300), Factory nvarchar (300), [Count] bigint)
      
      
      insert into #temp(Pays) select Pays
      from table1
      insert into #temp(Factory,[Count]) select Factory,[Count]
      from table2
      
      Insert into @temp select Pays,Factory,[Count] from #temp
      where Pays='yes'
      go

      【讨论】:

        【解决方案4】:

        我不太确定您要实现什么目标,但似乎您的 table2 中的数据需要取消透视才能将 table1 和 table2 连接到另一个。以下正是这样做的。

        declare @t2 table (Factory nvarchar(40), [Count] int, [France] nvarchar(300), [Morocco] nvarchar(300), [USA] nvarchar(300))
        insert into @t2 values ('SUX',233,'YES','NO','NO')
        , ('DRV',12,'YES','NO','NO')
        , ('HK',144,'YES','NO','NO')
        , ('MA',2016,'NO','YES','NO')
        , ('ZER',2,'NO','YES','NO')
        , ('RST',1777,'NO','YES','NO')
        , ('ZE',24,'NO','NO','YES')
        , ('VR',566,'NO','NO','YES')
        
        select  unpvt.[Pays], unpvt.[Factory], unpvt.[Count], unpvt.[PaysInd]
        from    
            (select [Factory], [count], [France], [Morocco], [USA]
             from @t2
             ) p
        unpivot
            ([PaysInd] for [Pays] IN 
                ([France], [Morocco], [USA])
            ) AS unpvt
        join @t1 countries on countries.[Pays] = unpvt.[Pays] and [PaysInd] = 'YES'
        

        【讨论】:

          猜你喜欢
          • 2017-11-13
          • 1970-01-01
          • 2017-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-22
          • 1970-01-01
          相关资源
          最近更新 更多