我几乎是新的SQL服务器,我试图这样做...不知道我做错了什么

时间:2013-11-04 19:10:45

标签: sql-server

我是SQL Server的新手,我正在努力做到这一点......不确定我做错了什么

这是我的架构:

CREATE TABLE [Product_AGGREGATION](
[Product_ID] [int] NULL,
    [ITERATION] [int] NULL,
    [EQUIPMENT_USED] [varchar](8) NULL,
    [MODEL_USED] [varchar](25) NULL,
    [SERIAL_NUMBER] [varchar](14) NULL
) 

CREATE TABLE [Product_MERGE](
    [Product_ID] [int] NULL,    
    [EQUIPMENT_USED] [varchar](8) NULL,
    [MODEL_USED] [varchar](25) NULL,
    [SERIAL_NUMBER] [varchar](14) NULL
) 

INSERT INTO [dbo].[PRODUCT_AGGREGATION]                         
     VALUES
           (6,1,'X10', '302000','005'),
           (6,7,'X10' ,'310355','007'),
           (3,1,'A12' ,'NULL','NULL'),
           (3,4,'A10' ,'35205','002'),
         (7,2,'A10' ,'37545','002'),
         (7,4,'A10' ,'NULL','005')                      

这是我正在使用的存储过程

ALTER PROCEDURE [dbo].[SP_ProductMerge]
BEGIN
    DECLARE @v_product_id          INT
    DECLARE @v_iteration           INT
    DECLARE @v_equip_used          VARCHAR(8)
    DECLARE @v_model_used          VARCHAR(25)
    DECLARE @v_serial_num          VARCHAR(14)
    DECLARE @c_Pid             INT
    DECLARE @c_iteration           INT
    DECLARE @c_equip_used          VARCHAR(8)
    DECLARE @c_model_used          VARCHAR(25)
    DECLARE @c_serial_num          VARCHAR(14)

    DECLARE  c_PID CURSOR FOR
    SELECT DISTINCT Product_id
        FROM   [Product_AGGREGATION]
        ORDER BY Product_id
    OPEN c_PID

    FETCH NEXT FROM c_PID INTO @c_Pid
    WHILE(@@FETCH_STATUS = 0)
    BEGIN
        SET @v_product_id         = @c_Pid
        SET @v_equip_used           = NULL
        SET @v_model_used           = NULL
        SET @v_serial_num          = NULL


        DECLARE c_ProductMerge CURSOR FOR
        SELECT iteration, EQUIPMENT_USED, MODEL_USED, SERIAL_NUMBER
            FROM   [Product_AGGREGATION]
            WHERE  Product_id = @v_product_id
            ORDER BY iteration
        OPEN c_ProductMerge
        FETCH NEXT FROM c_ProductMerge INTO @c_iteration, @c_equip_used, @c_model_used, @c_serial_num
        WHILE(@@FETCH_STATUS = 0)
        BEGIN
            IF RTRIM(LTRIM(@c_equip_used)) IS NOT NULL AND @v_equip_used IS NULL
            BEGIN
                SET @v_equip_used = @c_equip_used
            END
            IF RTRIM(LTRIM(@c_model_used)) IS NOT NULL AND @v_model_used IS NULL
            BEGIN


                 SET @v_model_used = @c_model_used
            END
            IF RTRIM(LTRIM(@c_serial_num)) IS NOT NULL AND @v_serial_num IS NULL
            BEGIN
                SET @v_serial_num = @c_serial_num
            END


             FETCH NEXT FROM c_ProductMerge INTO @c_iteration, @c_equip_used, @c_model_used, @c_serial_num
        END
        CLOSE c_ProductMerge
        DEALLOCATE c_ProductMerge
        INSERT INTO [dbo].Product_merge
            VALUES (
                @v_product_id, @v_equip_used, @v_model_used, @v_serial_num
               )
     FETCH NEXT FROM c_PID INTO @c_Pid
    END
    CLOSE c_PID
    DEALLOCATE c_PID

 END

GO

产品合并中的预期结果

 6 , 'X10', '302000' , '005'
 3, 'A12', '35205', '002'
 7,'A10'.'37545' , '002'

我没有得到产品3的预期结果。另外,我需要帮助写这个没有光标或至少用一个光标而不是嵌套光标。

SQLFiddle of the above

1 个答案:

答案 0 :(得分:0)

首先,'NULL'不等于NULL。根据您的数据,您需要将代码IS NOT NULL替换为<> 'NULL'

我认为您需要的内容可以替换为此查询。

SELECT
  a.Product_ID
  ,EQUIPMENT_USE =(SElECT TOP 1 p.EQUIPMENT_USED FROM Product_AGGREGATION p WHERE p.Product_ID = a.Product_ID AND p.EQUIPMENT_USED <> 'NULL' ORDER BY ITERATION)   
  ,MODEL_USED =(SElECT TOP 1 p.MODEL_USED FROM Product_AGGREGATION p WHERE p.Product_ID = a.Product_ID AND p.MODEL_USED <> 'NULL' ORDER BY ITERATION)   
  ,SERIAL_NUMBER =(SElECT TOP 1 p.SERIAL_NUMBER FROM Product_AGGREGATION p WHERE p.Product_ID = a.Product_ID AND p.SERIAL_NUMBER <> 'NULL' ORDER BY ITERATION)   
FROM Product_AGGREGATION a
GROUP BY a.Product_ID

sql fiddle demo