| Author |
Topic |
|
bunny28
Starting Member
13 Posts |
Posted - 2010-01-20 : 04:01:02
|
| hi,i got this error message when i am trying to create a sp with the following statementError Message: Msg 164, Level 15, State 1, Procedure Inventory_Report, Line 9Each GROUP BY expression must contain at least one column that is not an outer reference.create proc [dbo].[Inventory_Report]( @sw as varchar(100))as begin select count(*) nooflicences,entity,@sw from inventorylist where @sw is not null group by entity,@sw order by entityendcan any one help me how to overcome this error. i want use the result to design a crystal report.Thanks |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-20 : 04:04:20
|
| I don;t think you need @SQ in your GROUP BY clause, as it is a constant. |
 |
|
|
bunny28
Starting Member
13 Posts |
Posted - 2010-01-20 : 04:19:51
|
| my query is like thisselect count(*) nooflicences,entity,autocad from inventorylist where autocad is not nullgroup by entity,autocad order by entityi want the same result using sp.as autocad is not constant i want to pass this value to sp.if i take out that from the group by clause, result i am getting is different from the statement.Regards |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-20 : 05:31:02
|
| So you want @sw to contain a column name?If you do WHERE @sw IS NULL that checks if the value contained in the variable @sw is null - if @sw happens to contain a column name the test does NOT check the value of that column, only the contents of @sw.If you want @sw to contain a column name instead, then you will have to use dynamic SQL, you can't do it as a "generic SProc" in the way you currently have it, I'm afraid. |
 |
|
|
bunny28
Starting Member
13 Posts |
Posted - 2010-01-20 : 20:57:48
|
| thanks Kristeni agree with u.but i am finding it hard to send a column name dynamicallycan u please help me out.Regards |
 |
|
|
bunny28
Starting Member
13 Posts |
Posted - 2010-01-21 : 00:46:54
|
| hii am trying in this way still i am getting errors@sw as varchar(100)ASDeclare @SQLQry as varchar(500)SET ANSI_WARNINGS ONSET ANSI_NULLS ONSET QUOTED_IDENTIFIER OFFBeginset @SQLQry = "select count(*) cnt,entity," +@swset @SQLQry = @SQLQry + " from inventorylist where " +@sw set @SQLQry = @SQLQry + " is not null "set @SQLQry = @SQLQry + " group by entity," +@sw set @SQLQry = @SQLQry + " order by entity "print @SQLQryendplease correct me if i am wrong.regards |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-21 : 02:03:58
|
Looks alright to me, you missed off the CREATE PROCEDURE bit at the top, and there is no "AS" in the parameter definition:CREATE PROCEDURE dbo.MyTempSproc @sw varchar(100)ASDeclare @SQLQry varchar(500)SET @sw = 'autocad'SET ANSI_WARNINGS ONSET ANSI_NULLS ONSET QUOTED_IDENTIFIER OFFBeginset @SQLQry = "select count(*) cnt,entity," +@swset @SQLQry = @SQLQry + " from inventorylist where " +@swset @SQLQry = @SQLQry + " is not null "set @SQLQry = @SQLQry + " group by entity," +@swset @SQLQry = @SQLQry + " order by entity "print @SQLQryendEXEC dbo.MyTempSproc @sw='AUTOCAD' gives me:select count(*) cnt,entity,autocad from inventorylist where autocad is not null group by entity,autocad order by entity which is what I think you want?When you are happy with the SQL that you are producing you can replace the PRINT withEXEC (@SQLQry)Note that the user will HAVE to have SELECT permission on the inventorylist, rather than just EXECUTE permissions on the Procedure, which is a downside of using dynamic SQL. |
 |
|
|
bunny28
Starting Member
13 Posts |
Posted - 2010-01-21 : 02:37:56
|
| thanks for the reply sorry to bother u agianwhen i tried to create procedure it gave me errors so i changed all the "double" quotes to 'single' quote then only i could able to create.when i execute it with the parameter it was executed very well and printed statement was absolutely correct.then i changed print to exec and executed again.i am sorry this time this is the error message.Msg 2812, Level 16, State 62, Procedure Inventory_Report, Line 22Could not find stored procedure 'select count(*) cnt,entity,autocad from inventorylist where autocad is not null group by entity,autocad order by entity '. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-01-21 : 03:12:16
|
| Have you got parentehsis around the EXEC as per my example above, i.e.EXEC (@SQLQry) |
 |
|
|
bunny28
Starting Member
13 Posts |
Posted - 2010-01-21 : 03:16:38
|
| hey i am sorry now got the result.Thanks a lot. |
 |
|
|
|