存储过程在SQL Server 2012中引发错误,但在SQL Server 2000中正常工作

时间:2013-12-11 22:25:55

标签: sql-server sql-server-2012 sql-server-2000

我有一个可以在SQL Server 2000中正常运行的存储过程,但是在我们将数据库移动到的SQL Server 2012 Express上会抛出错误。

  

[SQLServer JDBC驱动程序] [SQLServer] INSERT语句的选择列表包含的项目少于插入列表。 SELECT值的数量必须与INSERT列的数量匹配。

有人能帮我解决这个问题吗?

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 个答案:

答案 0 :(得分: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

您正尝试在#tmp中插入3列,但查询只返回2列

我怀疑,你想要 Count(*) Sum(Orders)作为第三列