Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

I need answer for this can anyone help me with this Expand / Collapse
Author
Message
Posted Thursday, April 04, 2013 6:38 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, May 05, 2013 11:56 PM
Points: 12, Visits: 29
Declare @CurStudent cursor
Declare @Name VARCHAR(100)
Declare @Dept_ID INT
Declare @Department VARCHAR(100)
Declare @Fees NUMERIC(10,4)
Declare @StudentStatus varchar(100)
Declare @DOJ DATETIME

CREATE table #tempTable(TempName VARCHAR(100), TempDept_ID INT,TempDepartment VARCHAR(100),
TempFees NUMERIC(10,4),TempStudentStatus varchar(100),TempDOJ DATETIME )
SET @CurStudent = CURSOR FOR
SELECT Name,Dept_ID,Department,Fees,StudentStatus,DOJ FROM StudentDatamy

OPEN @CurStudent
FETCH NEXT FROM @CurStudent INTO @Name,@Dept_ID,@Department,@Fees,@StudentStatus,@DOJ
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT #tempTable values(@Name,@Dept_ID,@Department,@Fees,@StudentStatus,@DOJ)
FETCH NEXT FROM @CurStudent INTO @Name,@Dept_ID,@Department,@Fees,@StudentStatus,@DOJ
SELECT @Department,SUM(@Fees)AS TotalFee FROM #tempTable
GROUP BY @Department
End
select * from #tempTable
drop table #tempTable
close @CurStudent
Deallocate @CurStudent





I am getting error as[b] Each GROUP BY expression must contain at least one column that is not an outer reference.
Post #1438752
Posted Thursday, April 04, 2013 6:48 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:29 PM
Points: 12,741, Visits: 31,053
you are grouping by a variable instead of the column from the temp table.
this is not allowed:
GROUP BY @Department

there is no need for a cursor in the operation you are doing...as a matter of fact, there's no need for a temp table either.

this returns the results you would get directly from tthe base table...no cursor, no variables, no temp table.
just a single set based operation.

SELECT Department,SUM(Fees) As TotalFee
FROM StudentDatamy
GROUP BY Department



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1438758
Posted Thursday, April 04, 2013 6:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, May 05, 2013 11:56 PM
Points: 12, Visits: 29
Since i am starter in sql,they ask me to create a table with student data that include (Dept_ID,Name,Department,Fees,StudentStatus,DOJ)
and they ask to retrive the data into another table which contains sum(fees) for each department and distinct name only i need in that table
Post #1438766
Posted Thursday, April 04, 2013 7:01 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:29 PM
Points: 12,741, Visits: 31,053
ok then, here's two hints:

1. your final SELECt belongs outside of the loop, not inside.
2. your final SELECT command with the Department and SUM() should not reference any @variables whatsoever.

if you fix that, your existing code will work.


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1438771
Posted Thursday, April 04, 2013 7:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, May 05, 2013 11:56 PM
Points: 12, Visits: 29
Sorry to disturb you again,Pls check and correct the code


Declare @CurStudent cursor
Declare @Name VARCHAR(100)
Declare @Dept_ID INT
Declare @Department VARCHAR(100)
Declare @Fees NUMERIC(10,4)
Declare @StudentStatus varchar(100)
Declare @DOJ DATETIME

CREATE table #tempTable(TempName VARCHAR(100), TempDept_ID INT,TempDepartment VARCHAR(100),
TempFees NUMERIC(10,4),TempStudentStatus varchar(100),TempDOJ DATETIME )

SET @CurStudent = CURSOR FOR
SELECT Name,Dept_ID,Department,Fees,StudentStatus,DOJ FROM StudentDatamy
OPEN @CurStudent
FETCH NEXT FROM @CurStudent INTO @Name,@Dept_ID,@Department,@Fees,@StudentStatus,@DOJ

WHILE @@FETCH_STATUS = 0
BEGIN
INSERT #tempTable values(@Name,@Dept_ID,@Department,@Fees,@StudentStatus,@DOJ)
FETCH NEXT FROM @CurStudent INTO @Name,@Dept_ID,@Department,@Fees,@StudentStatus,@DOJ
SELECT TempDepartment,SUM(@Fees)AS TotalFee FROM #tempTable
GROUP BY TempDepartment
End
Post #1438780
Posted Thursday, April 04, 2013 7:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 4:29 PM
Points: 12,741, Visits: 31,053
same issues remain...run and test your code, don't just change one thing and re-post it..
test test test!

Lowell (4/4/2013)
ok then, here's two hints:
1. your final SELECT belongs outside of the loop, not inside.

still an issue. find your END, move your last SELECT after it.

2. your final SELECT command with the Department and SUM() should not reference any @variables whatsoever.
if you fix that, your existing code will work.

still an issue. SUM(@Fees)


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1438789
Posted Thursday, April 04, 2013 7:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:31 PM
Points: 11,949, Visits: 10,982
Since you say are new to sql. Looping and cursors are notoriously slow. When possible a set based approach will perform a lot better than cursor in most situations. There are certainly times where a cursor is appropriate but your example does not appear to be one of those times. If you would like some help removing the cursor from this let me know. I will be happy to help.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1438807
Posted Thursday, April 04, 2013 7:46 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 6:33 PM
Points: 22,475, Visits: 30,157
Really? If all you need from the StudentData in a separate table is a sumary of fees by department this will provide what you want with no temporary table needed to temporarily the data from the source table. Nor is a cursor even needed.


create table dbo.DepartmentFees(
Department varchar(100),
Fees numeric(10,4)
);

insert into dbo.DepartmentFees
select
Department,
sum(Fees)
from
dbo.StudentData
group by
Department;


select
Department,
Fees
from
dbo.DepartmentFees
order by
Department;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1438808
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse