从多个表中选择并组合成单行临时表

时间:2016-05-02 21:12:19

标签: sql sql-server stored-procedures temp-tables

我有三个使用相同结构的表格(FuelRates_PetrolFuelRates_LPGFuelRates_Diesel):

| OrganisationID | Band1 | Band2 | Band3 | ModifiedDate        |
|----------------|-------|-------|-------|---------------------|
| 1              | 10    | 12    | 19    | 01/05/2016 19:23:21 |
| 2              | 11    | 12    | 20    | 02/05/2016 19:23:21 |

我需要创建一个存储过程,从每个表中选择匹配OrganisationID的记录。每个表中都会有一个匹配的记录。

然后我需要将结果合并到具有以下结构的临时表中并返回它:

| OrganisationID | Petrol_Band1 | Petrol_Band2 | Petrol_Band3 | LPG_Band1 | LPG_Band2 | LPG_Band3 | Diesel_Band1 | Diesel_Band2 | Diesel_Band3 | ModifiedDate        |
|----------------|--------------|--------------|--------------|-----------|-----------|-----------|--------------|--------------|--------------|---------------------|
| 1              | 10           | 12           | 19           | 10        | 12        | 19        | 10           | 12           | 19           | 01/05/2016 19:23:21 |

Band1表中的FuelRates_Petrol列应插入Petrol_Band1临时表的FuelRates列。其余列应遵循此约定创建单行,如上所示。

SQL并不是我的书包,但这是我的努力:

ALTER PROCEDURE [dbo].[FuelRates_RetrieveList] 
    @OrganisationID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    CREATE TABLE #FuelRates(
    OrganisationID int,
    Petrol_Band1 int, 
    Petrol_Band2 int,
    Petrol_Band3 int,
    LPG_Band1 int,
    LPG_Band2 int,
    LPG_Band3 int,
    Diesel_Band1 int,
    Diesel_Band2 int,
    Diesel_Band3 int,
    ModifiedDate DateTime)

    SELECT Band1 As Petrol_Band1, Band2 As Petrol_Band2, Band3 As Petrol_Band3, ModifiedDate, OrganisationID FROM [FuelRates_Petrol]
    WHERE OrganisationID = @OrganisationID
    INSERT INTO #FuelRates

    SELECT Band1 As LPG_Band1, Band2 As LPG_Band2, Band3 As LPG_Band3 FROM [FuelRates_LPG]
    WHERE OrganisationID = @OrganisationID
    INSERT INTO #FuelRates

    SELECT Band1 As Diesel_Band1, Band2 As Diesel_Band2, Band3 As Diesel_Band3 FROM [FuelRates_Diesel]
    WHERE OrganisationID = @OrganisationID
    INSERT INTO #FuelRates

    SELECT * FROM #FuelRates

    DROP TABLE #FuelRates
END

当我执行此操作时,我收到以下错误:

  

Msg 213,Level 16,State 1,Procedure FuelRates_RetrieveList,Line 33   插入错误:列名或提供的值数不匹配   表定义。

第33行是临时表创建块中的Diesel_Band1 int,

我正在使用SQL Server 2005。

导致此错误的原因是什么,我甚至以正确的方式解决这个问题?

2 个答案:

答案 0 :(得分:2)

我知道它说错误在第33行,你已归因于install.packages("maps") library(maps) #plots worldmap map('world') #plots tweets points(tweets.df$longitude,tweets.df$latitude, pch=20, cex=1, col="red") ,但在此之后就是消息:

  

插入错误:列名或提供的值数与表定义不匹配。

定义表的SQL Server的Diesel_Band1方法是首先放置insert语句。

如果未按照与定义的表完全相同的顺序插入确切的列数,则需要定义列和顺序。

INSERT

将插入内容更改为以下内容:

INSERT INTO myTable (column1, column2, column3,...columnN)

答案 1 :(得分:1)

你可以试试这个......

ALTER PROCEDURE [dbo].[FuelRates_RetrieveList] 
    @OrganisationID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    CREATE TABLE #FuelRates(
    OrganisationID int,
    Petrol_Band1 int, 
    Petrol_Band2 int,
    Petrol_Band3 int,
    LPG_Band1 int,
    LPG_Band2 int,
    LPG_Band3 int,
    Diesel_Band1 int,
    Diesel_Band2 int,
    Diesel_Band3 int,
    ModifiedDate DateTime)

    INSERT INTO #FuelRates
    (OrganisationID, Petrol_Band1, Petrol_Band2, Petrol_Band3,LPG_Band1,LPG_Band2,LPG_Band3,
      Diesel_Band1, Diesel_Band2, Diesel_Band3, ModifiedDate)
    SELECT OrganisationID,
        p.Band1 As Petrol_Band1, p.Band2 As Petrol_Band2, p,Band3 As Petrol_Band3, 
        lp.Band1 As LPG_Band1, lp.Band2 As LPG_Band2, lp.Band3 As LPG_Band3,
        d.Band1 As Diesel_Band1, d.Band2 As Diesel_Band2, d.Band3 As Diesel_Band3,
        GetDate() as ModifiedDate
    FROM [FuelRates_Petrol] p
    LEFT JOIN FuelRates_LPG lp on lp.OrganisationID = p.OrganisationID
    LEFT JOIN FuelRates_Diesel d on d.OrganizationID = p.OrganisationID
    WHERE p.OrganisationID = @OrganisationID

    SELECT * FROM #FuelRates

    DROP TABLE #FuelRates
END