Stored procedure throws error in SQL Server 2012 but works fine in SQL Server 2000

与世无争的帅哥 提交于 2019-12-25 18:11:29

问题


I have a stored procedure that works fine in SQL Server 2000 but throws an error on SQL Server 2012 Express where we are moving the DB to.

[SQLServer JDBC Driver][SQLServer]The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.

Anybody able to help me sorting this out?

CREATE PROCEDURE spSelectTopCourses
AS   
   declare @divisor int
   declare @column int

   set @column = 6 -- number of columns in the site

   create table #tmpID (
       CityID int
   )

   insert into #tmpID (CityID)
      Select top 40 -- total number of cities to be shown
         c.RID as CityID
      From Course c
      Inner join StateRegions Sr on Sr.RID = c.RID
      Inner join Client_Round cr on cr.CourseID = c.CourseID
      Group by c.RID
      Order by Count(cr.OrderID) desc

    create table #tmp (
        CityID int,
        CityName varchar(100),
        TotalOrder int identity(1,1)
    )

insert into #tmp (CityID, CityName, TotalOrder)
   Select distinct 
           sr.RID as CityID,
           sr.Rname as CityName
       From 
           #tmpID tid 
       Inner join StateRegions Sr on Sr.RID = tid.CityID
       Group by Sr.RName, sr.RID
       Order by sr.RName

    select @divisor = ceiling(count(*) * 1.0 / @column) 
    from #tmp

    create table #tmpC (
       Position int
    )

    declare @i int
    set @i = 1

    while @i <= @divisor * @column
    begin
        insert into #tmpC(Position) 
           select @i
        set @i = @i + 1
    end

    select 
       t.CityID, t.CityName + ' Golf' CityName, cnt.CountryID, c.Position, 
       (c.Position - 1) % @divisor WebOrder1, 
       (c.Position - 1) / @divisor WebOrder2
    from 
       #tmpC c
    left join #tmp t on t.TotalOrder = c.Position
    inner join StateRegions sr on sr.RID = t.CityID
    inner join CountryStates cs on cs.StateID = sr.StateID
    inner join Country cnt on cnt.CountryID = cs.CountryID
    order by WebOrder1, WebOrder2

    drop table #tmpC 
    drop table #tmp
    drop table #tmpID

回答1:


insert into #tmp (CityID, CityName, TotalOrder)
Select distinct sr.RID as CityID,
     sr.Rname as CityName
From #tmpID tid 
Inner join StateRegions Sr on Sr.RID = tid.CityID
Group by Sr.RName,
     sr.RID
Order by sr.RName

you are trying to insert 3 columns into #tmp, but the query only returns 2 columns

I suspect, you want Count(*) or Sum(Orders) as the third column



来源:https://stackoverflow.com/questions/20531220/stored-procedure-throws-error-in-sql-server-2012-but-works-fine-in-sql-server-20

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!