Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Subquery returned more than 1 value. This is not permitted when the subquery follows RE: Subquery returned more than 1 value. This is not permitted when the subquery follows

  • you have two queries that could potentially be comparing more than one row to a single value;

    one is a simple test for a value:

    if (select UNITE_COUT_MATERIEL from DIM_INTER_MATERIEL) = 'Unité'

    begin

    end

    and the other is assigning a calculation to a variable.

    set @DU_QTE = (select (SUPERFICIE_INTER_CULTURE / SUPERFICIE_INTER) * QUANTITE_MATERIEL from FACT_INTERVENTION inter,DIM_INTER_MATERIEL mat

    where inter.ID_INTER=mat.ID_INTER )

    So the issue is if multiple rows exist, what calculation do you really want? would the sum() work where there are multiple rows? don't you need to filter the calculation as well?

    I would consider changing it to something like this:

    --is there ANY data which matches this criteria?

    if EXISTS (select 1 from DIM_INTER_MATERIEL WHERE UNITE_COUT_MATERIEL = 'Unité')

    begin

    select @DU_QTE = (SUM(SUPERFICIE_INTER_CULTURE) / SUM(SUPERFICIE_INTER)) * SUM(QUANTITE_MATERIEL)

    from FACT_INTERVENTION inter

    INNER JOIN DIM_INTER_MATERIEL mat

    ON inter.ID_INTER=mat.ID_INTER

    WHERE UNITE_COUT_MATERIEL = 'Unité'

    print cast(@DU_QTE as varchar (50));

    end

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!