插入到select语句中返回错误:“SQL Anywhere错误-185:SELECT返回多行”

时间:2017-05-19 08:03:54

标签: sql sybase sqlanywhere sybase-iq

我正在使用insert into ... select ...语句 在表中插入多行但是,我收到了 执行语句时出现以下错误:

  

SQL Anywhere错误-185:SELECT返回多行。

这是我的代码:

begin
declare   @pyear integer= 0
  print 'Step 1 '
  print 'Step 2 '
  declare @startDateYear integer,@llystartDt integer,
  @tradeYear integer,
  @evRefreshDate integer,@err integer,@l_cnt integer
  declare @mtdmin integer
  declare @mtdmax integer
  declare @cyear integer
  declare @cmonth integer
  select @cyear = year,@cmonth = month from table1 where last_trade_date = 'Y'
  select @mtdmin = min(intDate) from table1 where year = @cyear and month = @cmonth
  select @mtdmax = intDate from table1 where last_trade_date = 'Y'
  if @pyear <> 0
    begin
      select @tradeYear = @pYear, @startDateYear = convert(integer,convert(char(4),@pyear)+'0101')
      select @evRefreshDate = @startDateYear
      select @llystartDt = convert(integer,convert(char(4),@pyear-2)+'0101')
    end
  else begin
      select @startDateYear = MIN(dd.intDate)
        from table1 as dd
          join table1 as dd1
          on dd1.year = dd.year
          and dd1.LAST_TRADE_DATE = 'Y'
      select @evRefreshDate = intDate
        from table1 as dd
        where dd.LAST_TRADE_DATE = 'Y'
      select @tradeYear = dd.year
        from table1 as dd
          join table1 as dd1
          on dd1.year = dd.year
          and dd1.LAST_TRADE_DATE = 'Y'
      select @llystartDt = convert(integer,convert(char(4),(@tradeYear-2))+'0101')
    end

  print 'EV Refresh Date =%1!',  @evRefreshDate

  print 'MktType','Dimension','Dimension_Value','cnsEv','YearlyRollup','MonthlyRollUp','DailyRollup','YearlyDiff','MonthlyDiff','DailyDiff','stat'

  print 'Step 3 '

  print 'Step 4 '

   --truncate table table2
  insert into table2
    ( MktType,
    Dimension,
    Dimension_Value,
    cnsEv,
    YearlyRollup,
    MonthlyRollUp,
    DailyRollup,
    YearlyDiff,
    MonthlyDiff,
    DailyDiff,
    stat ) 
    select 'MktType'='FI',
      'Dimension'='' || 'TradeDate',
      'Dimension_Value'='' || @tradeYear,
      'cnsEv'=cns.ev,
      'YearlyRollup'=yrly.ev,
      'MonthlyRollUp'=mth.ev,
      'DailyRollup'=dly.ev,
      'YearlyDiff'=(cns.ev-yrly.ev),
      'MonthlyDiff'=(cns.ev-mth.ev),
      'DailyDiff'=(cns.ev-dly.ev),
      'stat'=convert(char(20),null)
      from(select 'ev'=SUM(net)
          from table3 as a
            join table4 as pd on pd.STRG = a.productMappingKey
          where TrdDt >= @startDateYear
          and SalesGrpLvl1 = 'GIS'
          and reportDt >= @llystartDt
          and not(trddt >= @mtdmin and trddt <= @mtdmax and GPNum = '49135' and(trim(prod3) = 'Equities - Cash (EE)' or prod5 like '% DMA %' or prod5 like '% DSA %'))
          and not(gpnum = '63376' and trim(prod2) = 'Equity Cash' and trddt >= @mtdmin and trddt <= @mtdmax)) as cns
        ,(select 'ev'=SUM(ev)
          from table5 as r
            ,table6 as d
          where r.tradeDateMonthKey = d.month_key
          and d.year = @tradeYear
          and salesForce = 'GIS') as mth
        ,(select 'ev'=SUM(ev)
          from table7
          where tradeDate >= @startDateYear
          and salesForce = 'GIS') as dly
        ,(select 'ev'=SUM(ev)
          from table8
          where YEAR >= @tradeYear
          and salesForce = 'GIS'
          and dateType = 'tradeDate') as yrly 
          end

我注意到的一件事是,当我注释掉Insert语句时,其余的查询工作正常但是当我取消注释它时,我得到了上面提到的错误。

1 个答案:

答案 0 :(得分:0)

在将值赋给@variable时,请检查table1中的每个查询。

例如@cyear期待一个标量/单值但是#34; select * from table1,其中last_trade_date =&#39; Y&#39;&#34;本可以返回多个值。

您只能将单个值分配给单个@variable。