我执行了查询,但错误如下

时间:2018-06-21 07:18:36

标签: sql-server

INSERT INTO RPT(N5,T10,TXT1,T1,T2,T3,T6,N6,T7,T9,T8,N1,N2,N3,N4,N7,N8,TXT2)
    VALUES (
        (SELECT ISNULL((UNIT_COST*EX_RATE),0)
          FROM STOCKDETAILS
          WHERE PRODUCT_NO=N'AST20471' AND CONVERT(VARCHAR(10),STOCK_DATE,120) < '2018-06-15'),
        (SELECT STOCK_JR
          FROM STOCKDETAILS
          WHERE PRODUCT_NO=N'AST20471' AND CONVERT(VARCHAR(10),STOCK_DATE,120) < '2018-06-15'),
        '15-06-2018',
        '15-06-2018',
        N'AST20471',
        N'WASHER RUBBER OUTLET VALVE N123',
        'GR9553545',
        '2.466975', 
        (SELECT CATEGORY
          FROM PRODUCT
          WHERE PRODUCT_NO=N'AST20471'),
        (SELECT CATEGORY_MIDDLE
          FROM PRODUCT
          WHERE PRODUCT_NO=N'AST20471'),
        'MAIN',
        (SELECT ISNULL(SUM(QTY_IN)-SUM(QTY_OUT),0)
          FROM STOCKDETAILS
          WHERE PRODUCT_NO=N'AST20471' AND CONVERT(varchar(10),STOCK_DATE,120) < '2018-06-15'),
        (SELECT SUM(QTY_IN)
          FROM STOCKDETAILS
          WHERE PRODUCT_NO=N'AST20471' AND CONVERT(VARCHAR(10),STOCK_DATE,120) >= '2018-06-15' AND CONVERT(VARCHAR(10),STOCK_DATE,120) <= '2018-06-15'),
        (SELECT SUM(QTY_OUT)
          FROM STOCKDETAILS
          WHERE PRODUCT_NO=N'AST20471' AND CONVERT(VARCHAR(10),STOCK_DATE,120) >= '2018-06-15' AND CONVERT(varchar(10),STOCK_DATE,120) <= '2018-06-15'),
        (SELECT ISNULL(SUM(QTY_IN)-SUM(QTY_OUT),0)
          FROM STOCKDETAILS
          WHERE PRODUCT_NO=N'AST20471' AND CONVERT(VARCHAR(10),STOCK_DATE,120) < ='2018-06-15'),
        '4.933950',
        '2',
        '15-06-2018 4:24:00 PM'
    )
  

子查询返回了多个值。当   子查询遵循=,!=,<,<=,>,> =,或当子查询用作   表达式。

出现此错误,请帮助

1 个答案:

答案 0 :(得分:0)

因此,您尝试在值内部使用子查询,并且其中一个或多个将返回多行作为结果,这就是导致错误的原因。 您可以尝试逐个运行每个子查询,以查看所有子查询都给出多行,然后相应地更改查询,或者可以使用如下所示的查询来给出所有可能的结果集。 如果每个子查询返回一行,则其行为类似于值构造函数

Insert into RPT(N5,T10,TXT1,T1,T2,T3,T6,N6,T7,T9,T8,N1,N2,N3,N4,N7,N8,TXT2)
SELECT N5,T10,TXT1,T1,T2,T3,T6,N6,T7,T9,T8,N1,N2,N3,N4,N7,N8,TXT2 from 
(
    select 
        N5=isnull((UNIT_COST*EX_RATE),0) 
    from STOCKDETAILS 
    where 
        PRODUCT_NO=N'AST20471' and
        CONVERT(varchar(10),STOCK_DATE,120) < '2018-06-15'
)t1

CROSS JOIN

(
    select 
        T10=STOCK_JR 
    from STOCKDETAILS 
    where 
        PRODUCT_NO=N'AST20471' 
    and  CONVERT(varchar(10),STOCK_DATE,120)
    < '2018-06-15'
) t2
CROSS JOIN
(
    SELECT 
        TXT1='15-06-2018',
        T1='15-06-2018',
        T2=N'AST20471',
        T3=N'WASHER RUBBER OUTLET VALVE N123',
        T6='GR9553545',
        N6='2.466975',
        T8='MAIN',
        N7='4.933950',
        N8='2',
        TXT2='15-06-2018 4:24:00 PM'
)t3
CROSS JOIN
(
    select 
        T7=CATEGORY 
    from PRODUCT 
    where 
        PRODUCT_NO=N'AST20471'
)t4
CROSS JOIN
(
    select 
        t9=CATEGORY_MIDDLE 
    from PRODUCT 
    where 
        PRODUCT_NO=N'AST20471'
)t5
 CROSS JOIN
(
    select 
        N1=isnull(SUM(QTY_IN)-SUM(QTY_OUT),0) 
    from STOCKDETAILS 
    where 
        PRODUCT_NO=N'AST20471'
        and  CONVERT(varchar(10),STOCK_DATE,120) < '2018-06-15'
)t6
CROSS JOIN
(
    select 
        N2=SUM(QTY_IN) 
    from STOCKDETAILS
    where 
        PRODUCT_NO=N'AST20471' 
        and CONVERT(varchar(10),STOCK_DATE,120) >= '2018-06-15' 
        AND CONVERT(varchar(10),STOCK_DATE,120) <= '2018-06-15'
)t7

CROSS JOIN
(
    select 
        N3=SUM(QTY_OUT) 
    from STOCKDETAILS
    where 
        PRODUCT_NO=N'AST20471' 
        and CONVERT(varchar(10),STOCK_DATE,120) >= '2018-06-15' 
        AND CONVERT(varchar(10),STOCK_DATE,120) <= '2018-06-15'
)t8

CROSS JOIN
(
    select 
        n4=isnull(SUM(QTY_IN)-SUM(QTY_OUT),0)
    from STOCKDETAILS 
    where 
        PRODUCT_NO=N'AST20471' 
        and CONVERT(varchar(10),STOCK_DATE,120)< ='2018-06-15'
)t9