SQL Server 2012 error: each GROUP BY expression must contain at least one column that is not an outer reference
<p>My code is like this</p>

<pre><code>SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

ALTER PROCEDURE [dbo].[getprocbyproc]
    @flg varchar(2),
    @qaaprvdt char(8), 
    @dbo varchar(20),
    @plantid char(1)
AS
    DECLARE @dynamicSQL varchar(8000) 
    DECLARE @datemth char(6) 
    DECLARE @dateday char(2) 
    DECLARE @FXTIME char(4) 
    DECLARE @TBL varchar(20) 
    DECLARE @def varchar(20) 
    DECLARE @OUTDAT CHAR(20) 

    SET @datemth = substring(@qaaprvdt,1,6)
    SET @dateday = substring(@qaaprvdt,7,2)

    IF @flg = 'QA' AND @dbo = 'PRCDAILYQA'
    BEGIN
        SET @TBL  = 'DAILYQA'
        SET @OUTDAT = 'CO.QAAPRVDT'
    END

    IF @plantid = '1' 
    BEGIN
        SET @def = "SQDPRCDEF1"
    END

    IF @flg = 'QA' AND @plantid = '1'
    BEGIN
        SET @dynamicSQL =  'insert into PrcdailyQA' +
                           '(process, seqno, qaamon, qaaday, orderno,  
                             ingotno, theopcs, cwacc, pwacc, pwout, 
                             procyld, prodyld, plantid, prodline) ' +
                           '(SELECT distinct(b.PROCNO),  b.proseq, "' + 
                           @datemth + '" , "' + @dateday +     
                           '", a.orderno, a.ingotno, a.theopcs, a.cwacc, a.pwacc, a.pwout, a.COprocyld, a.COprodyld, a.plantid, a.prodline ' +
                           'FROM DailyQA a , SQDPRCDEF1 b ' +
                           'WHERE a.qaamon = ' + @datemth  + '  and a.qaaday= ' + @DATEDAY  + ' and     a.plantid="1"' +  
                           'GROUP BY b.procno, b.proseq, a.orderno, a.ingotno, a.theopcs, a.cwacc, a.pwacc, a.pwout, a.COprocyld, a.COprodyld, a.plantid, a.prodline) ' 
</code></pre>

<p>Now when I to execute it returns no error. But when I try to execute it at runtime, it returns error </p>

<blockquote>
  <p>Msg 164, Level 15, State 1, Line 1<br>
  Each GROUP BY expression must contain at least one column that is not an outer reference.</p>
</blockquote>

<p>I already refer this <a href="http://www.sql-server-performance.com/2007/group-by-expression-contain-one-column-not-an-outer-reference/" rel="nofollow">http://www.sql-server-performance.com/2007/group-by-expression-contain-one-column-not-an-outer-reference/</a> and try to change the group by statement, also return error.</p>

<p>Please help!</p>