Unix Technical Forum

Each GROUP BY expression must contain at least one column that is notan outer reference

This is a discussion on Each GROUP BY expression must contain at least one column that is notan outer reference within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a stored procedure as follows ALTER PROCEDURE [dbo]. [spLoaderCoverageObjectTypeActionTypeWithOptional] -- Add the parameters for the stored procedure ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-06-2008, 03:04 PM
Vic
 
Posts: n/a
Default Each GROUP BY expression must contain at least one column that is notan outer reference

I have a stored procedure as follows
ALTER PROCEDURE [dbo].
[spLoaderCoverageObjectTypeActionTypeWithOptional]
-- Add the parameters for the stored procedure here
@ObjectClass sysname,
@ObjectType sysname,
@RecordAction sysname,
@FieldName sysname
AS

BEGIN
SELECT DISTINCT @ObjectClass as ObjectClass,@ObjectType as
ObjectType,@RecordAction as RecordAction,@FieldName as FieldName,
COUNT(*) AS VALIDCount--,
/*(select Count(*) from TestData6061.dbo.accounting WHERE
(AutomationType LIKE 'loader') AND ([Negative Testcase] = 0) AND
(@ObjectType = @ObjectType) AND (@RecordAction = 'New') AND
('['+@FieldName+'] ' IS NULL)
GROUP BY [01-RecordClass],[02-RecordAction]) as NULLCount*/

FROM TestData6061.dbo.accounting
WHERE (AutomationType LIKE 'loader') AND ([Negative Testcase] =
0) AND (@ObjectType = @ObjectType) AND (@RecordAction = 'New') AND
('['+@FieldName+'] ' IS NOT NULL)
GROUP BY [01-RecordClass],[02-RecordAction],@FieldName
END

I call the stored procedure as
exec spLoaderCoverageObjectTypeActionTypeWithOptional
'accounting','AccountingParameters','New','13-
OverdueOpeningTargetAccount'

and I am seeing the above mentioned error. If I remove the
'@fieldname' in the GROUPBY clause the query works but the result is
not really what I want.
Are there any solutions or workaround for this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-06-2008, 03:04 PM
czytacz
 
Posts: n/a
Default Re: Each GROUP BY expression must contain at least one column thatis not an outer reference

@fieldname is treated like a constant value,
so no another group is returned
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-06-2008, 03:04 PM
Vic
 
Posts: n/a
Default Re: Each GROUP BY expression must contain at least one column that isnot an outer reference

On Mar 4, 11:32 am, czytacz <redak...@dupa.gazeta.pl> wrote:
> @fieldname is treated like a constant value,
> so no another group is returned


So I tired to modify the stored procedure a little bit as below
Declare @TableNullCount int
Declare @TableValidCount int

select @TableNullCount = (SELECT Count(*)
from TestData6061.dbo.accounting
WHERE (AutomationType = 'loader') AND ([Negative Testcase] = 0)
AND ([01-RecordClass] = @ObjectType) AND
([02-RecordAction]= 'New') AND ('['+@FieldName+']+ IS NULL') )
print @ObjectType
print '['+@FieldName+'] IS NULL'
print @TableNullCount

/*select @TableValidCount = (SELECT Count(*)
from TestData6061.dbo.accounting
WHERE (AutomationType = 'loader') AND ([Negative Testcase] = 0)
AND ([01-RecordClass] = @ObjectType) AND
([02-RecordAction]= 'New') AND ('['+@FieldName+'] IS NOT NULL') )

print @TableValidCount

SELECT DISTINCT @ObjectClass as ObjectClass,@ObjectType as
ObjectType,@RecordAction as RecordAction,@FieldName as FieldName,
@TableValidCount as VALIDCount, @TableNullCount as NULLCount

FROM TestData6061.dbo.accounting
WHERE (AutomationType LIKE 'loader') AND ([Negative Testcase] =
0) AND (@ObjectType = @ObjectType) AND (@RecordAction = 'New')

GROUP BY [01-RecordClass],[02-RecordAction] */

some commented code but basically I am trying to get the total number
of rows (ValidCount) and (NullCount) seperately and use that in the
3rd select statement but it gives me an error saying 'An expression of
non-boolean type specified in a context where a condition is expected,
near ')'. Which is @FieldName in the select statement. Am I doing
anything wrong here?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-06-2008, 03:04 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Each GROUP BY expression must contain at least one column that is not an outer reference

Vic (vikrantp@gmail.com) writes:
> ([02-RecordAction]= 'New') AND ('['+@FieldName+'] IS NOT NULL') )
>...
> some commented code but basically I am trying to get the total number
> of rows (ValidCount) and (NullCount) seperately and use that in the
> 3rd select statement but it gives me an error saying 'An expression of
> non-boolean type specified in a context where a condition is expected,
> near ')'. Which is @FieldName in the select statement. Am I doing
> anything wrong here?


You have

AND (string-expression)

where the string expression is

'[' + @FieldName + '] IS NOT NULL'

and that does not make much sense at all.

Maybe you like to believe that SQL Server will guess that it should
substitute the value in @FieldName with a column name, but that did
not happen last week, and it is not going to happen this week either.

Generally: if you want to use dynaimc table and column names, you will
need to use dynamic SQL, and it is going to be very painful.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-06-2008, 03:04 PM
Vic
 
Posts: n/a
Default Re: Each GROUP BY expression must contain at least one column that isnot an outer reference

On Mar 4, 2:47 pm, Erland Sommarskog <esq...@sommarskog.se> wrote:
> Vic (vikra...@gmail.com) writes:
> > ([02-RecordAction]= 'New') AND ('['+@FieldName+'] IS NOT NULL') )
> >...
> > some commented code but basically I am trying to get the total number
> > of rows (ValidCount) and (NullCount) seperately and use that in the
> > 3rd select statement but it gives me an error saying 'An expression of
> > non-boolean type specified in a context where a condition is expected,
> > near ')'. Which is @FieldName in the select statement. Am I doing
> > anything wrong here?

>
> You have
>
> AND (string-expression)
>
> where the string expression is
>
> '[' + @FieldName + '] IS NOT NULL'
>
> and that does not make much sense at all.
>
> Maybe you like to believe that SQL Server will guess that it should
> substitute the value in @FieldName with a column name, but that did
> not happen last week, and it is not going to happen this week either.
>
> Generally: if you want to use dynaimc table and column names, you will
> need to use dynamic SQL, and it is going to be very painful.
>
> --
> Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
>
> Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
> Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx


Hi Erland,
I actually posted and it and right after that I realized that. I got
it working actually with dynamic sql using sp_executesql. Thanks
anyways and sorry for the spam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:11 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com