last day (15 days later) » 

8:13 AM
0
Q: Create cursor for temporary table in sql server 2005

N KI have some data inserted into the temporary table and I want to show COUNT in a LOOP by using CURSOR Here is the temp table:- SELECT * INTO #temp FROM ( select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME from inward_doc_tracking_trl a, user_mst b where a.CUser_id = ...

 
Could you please explain what you mean by "I want to show COUNT in a LOOP" ? Do you want to display incrementation? What is the purpose that you want to use a Cursor?
 
N K
@PKirby: I want to show the count of CUser_Id whose NStatus_flag and CStatus_flag in a cursor..currently i have taken recevied column and given 0 as count.
@PKirby: did u understand the logic ? or need some more explanation ?
 
Ok so you have the above temp table with RECEIVED column set to 0. You want the Cursor to run through each of those records (rows) and update the RECEIVED column based on a certain validation?
 
N K
@PKirby: yes you got it right now. Do let me know if you want the validation details
 
Please give me the validation detail. Would like to have a look at it.
 
N K
8:13 AM
I want condition like Where CUser_id = CUROSRID and NStatus_flag = 4 and CStatus_flag = 1 and a.U_datetime between '01/04/2016' and getdate()
@PKirby: anything else required ??
hi u their
?
 
Hi N K
 
N K
hello
done /
?
 
Im just trying to understand everything... When that validation is satisfied, does it update the RECEIVED column with 1? You mentioned a COUNT earlier. Should the validation be a COUNT?
Ive written the Cursor, just need to add the validation
 
N K
no not with 1
it will check the count from the inward_doc_tracking_trl table with NStatus_flag = 4
-- and CStatus_flag = 1
and date what I mentioned
can u show me the cursor
 
DECLARE @Count INT

DECLARE cur CURSOR
FOR SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


OPEN cur
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM cur
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN






FETCH NEXT FROM cur1 INTO @Country
END
CLOSE cur1
DEALLOCATE cur1
Still in testing phase
 
N K
8:18 AM
let me check
ok
 
@Country should also not be there
 
N K
yes
so what u want now from me ?
 
About 5mins
 
N K
aah
ok
 
Ok so in your TEMP table, you currently ONLY have the following columns : CUser_id, User_Id and NAME
 
N K
8:24 AM
yes
 
Do you need script in order to add the RECEIVED column?
 
N K
no
 
Where can I find the RECEIVED column?
 
N K
i just added
like this
SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
) AS x
after NAME
 
Ok thanks
 
N K
8:26 AM
I only created that column
 
Quickly try this :



DECLARE @Count INT

DECLARE cur CURSOR
FOR SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name AS NAME, 0 AS RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


OPEN cur
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM cur
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN

SELECT @Count = COUNT(*) FROM inward_doc_tracking_trl
WHERE CUser_id = @CUser_id
 
N K
ok
 
I could not execute on my side so please just let me know what errors you get if any
 
N K
do I need to add this in Stored procedure ?
or just simply run this
 
You can just execute if you are on the correct DB where inward_doc_tracking_trl exists
and user_mst
 
N K
8:29 AM
yup
let me check then
2 errors
Msg 137, Level 15, State 1, Line 10
Must declare the scalar variable "@Count".
Msg 137, Level 15, State 2, Line 17
Must declare the scalar variable "@Count".
sorry
that was not error
this is error
Msg 154, Level 15, State 3, Line 4
an INTO clause is not allowed in a cursor declaration.
Msg 156, Level 15, State 1, Line 12
Incorrect syntax near the keyword 'AS'.
their ?
 
DECLARE @Count INT


SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name AS NAME, 0 AS RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


DECLARE cur CURSOR
FOR SELECT * FROM #temp


OPEN cur
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM cur
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN

SELECT @Count = COUNT(*) FROM inward_doc_tracking_trl
WHERE CUser_id = @CUser_id
 
N K
1 error
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
 
Can you quickly run only the following script : DROP TABLE #temp
 
N K
ok
succesfully executed
 
Execute the whole script again
 
N K
8:36 AM
including drop statement ?
 
Drop statement should be the last line of script
 
N K
so I shd excute both. right ?
 
DECLARE @Count INT


SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name AS NAME, 0 AS RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


DECLARE cur CURSOR
FOR SELECT * FROM #temp


OPEN cur
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM cur
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN

SELECT @Count = COUNT(*) FROM inward_doc_tracking_trl
WHERE CUser_id = @CUser_id
Execute the above script as-is
 
N K
ok
Msg 16915, Level 16, State 1, Line 15
A cursor with the name 'cur' already exists.
Msg 16905, Level 16, State 1, Line 18
The cursor is already open.
Msg 16943, Level 16, State 4, Line 21
Could not complete cursor operation because the table schema changed after the cursor was declared.
Msg 16916, Level 16, State 1, Line 40
A cursor with the name 'cur1' does not exist.
Msg 16916, Level 16, State 1, Line 41
A cursor with the name 'cur1' does not exist.
errors
 
Sorry, forgot to change the CLOSE and DEALLOCATE
 
N K
8:40 AM
ok
 
DECLARE @Count INT


SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name AS NAME, 0 AS RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


DECLARE cur CURSOR
FOR SELECT * FROM #temp


OPEN cur
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM cur
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN

SELECT @Count = COUNT(*) FROM inward_doc_tracking_trl
WHERE CUser_id = @CUser_id
 
N K
(5 row(s) affected)
Msg 16915, Level 16, State 1, Line 16
A cursor with the name 'cur' already exists.
Msg 16905, Level 16, State 1, Line 19
The cursor is already open.
Msg 16943, Level 16, State 4, Line 22
Could not complete cursor operation because the table schema changed after the cursor was declared.
need to change cursor name ?
 
Try executing the following :

CLOSE cur
DEALLOCATE cur

DROP TABLE #temp
 
N K
done
Msg 16916, Level 16, State 1, Line 1
A cursor with the name 'cur' does not exist.
Msg 16916, Level 16, State 1, Line 2
A cursor with the name 'cur' does not exist.
Msg 3701, Level 11, State 5, Line 4
Cannot drop the table '#temp', because it does not exist or you do not have permission.
now got only one error
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
 
DECLARE @Count INT


SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name AS NAME, 0 AS RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp


OPEN Cur_1
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM Cur_1
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN

SELECT @Count = COUNT(*) FROM inward_doc_tracking_trl
Sorry about all the changes, I usually execute on my side by I currently cant
 
N K
8:44 AM
i can understand
no issue for that
so now
If i want to see the records
what shd I do ?
 
DECLARE @Count INT


SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name AS NAME, 0 AS RECEIVED
from inward_doc_tracking_trl a, user_mst b
where a.CUser_id = b.mkey
and CStatus_flag = 1
and NStatus_flag = 4
) AS x


DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp


OPEN Cur_1
DECLARE @CUser_id INT
DECLARE @User_Id INT
FETCH NEXT FROM Cur_1
INTO @CUser_id, @User_Id

WHILE (@@FETCH_STATUS = 0)
BEGIN

SELECT @Count = COUNT(*) FROM inward_doc_tracking_trl
So I have inserted a SELECT * FROM #temp just before I DROP the table... You will now be able to see the results
 
N K
yes
If i want to verify the received
can i verify ?
?
 
Yes, just execute the latest script I've sent
 
N K
ok
let me check and confirm u
 
Ok, holding thumbs
 
N K
8:47 AM
also will u will be availble in future.. I have 4-5 more queries related to this. :P
 
Yes, I am on stackoverflow a lot
 
N K
me too
will ping u if needed
U can update your answer as an answer
 
Cool, code working?
 
N K
I will upvote and accpet in sometime
Manager went for lunch
 
Ok thanks
 
N K
8:49 AM
i will recheck with him and update you
u welcome
U seems to be from SA ?
 
Yes... where are you from?
 
N K
INDIA
i am big fan of ABD
 
Cool... what is ABD?
 
N K
AD de villiers
i hope u might be knowing him
 
AB de villiers? haha yeah, he plays good cricket
 
N K
8:52 AM
yep
ok, i m also going for lunch
will be back in 15-20 mins
bye for now
take care
 
Cool, enjoy
bye
 
 
1 hour later…
N K
10:16 AM
hi
are u their
 
 
1 hour later…
11:30 AM
Hi N K
Sorry, was in a meeting
What changes need to be made in the script?
 
N K
ok
hello
u their ?
 
Yes I am
 
N K
ok
let me explain u
I added ur cursor in procedure
with some more columns in the SELECT part
have a look
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT
SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED

from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp

OPEN Cur_1
so I added for CLOSED
with some validations for second cursor
got it ?
 
Ok, so lets just start with the previous column RECEIVED, is this one working now 100% ?
 
N K
yes
I removed date validation from select
that was the only change in ur cursor
 
11:38 AM
Ok so now you want validations for the other columns you added?
 
N K
only for pending I want
as it needs join
need ur help on that
as I did for CLOSED
shd I explain u valdiations for PENDING ?
 
yes please
 
N K
5 mins
manager is here
 
Ok
 
N K
u theiir ?
 
11:41 AM
Yes
 
N K
all will be same
as
and CStatus_flag = 1
and NStatus_flag = 4
one more thing will come here
instead of CUser_id, I will be needing N_UserMkey = CURSORID and Statusflag from another table
N_UserMkey comes from inward_doc_tracking_trl table
and
Statusflag comes from inward_doc_tracking_hdr table
 
and what is the relationship between the tables?
 
N K
which tables?
hdr and trl ?
 
yes
 
N K
is that needed ? i just need to join from hdr
ok
i will tell u
let me check
 
11:46 AM
The two tables needs some sort of relationship.. Remember, when you JOIN a table to another you need to specify which column corresponds to which
 
N K
yes
got the relation
 
SELECT * FROM
TABLE_A AS A
INNER JOIN TABLE_B AS B ON A.somecolumn = B.somecolumn
 
N K
hdr -> mkey
trl -> ref_mkey
 
Cool thanks, give me 5-10mins... You dont need 2 cursors in your script
 
N K
ok
given 10 mins
let me know
:P
 
11:53 AM
Should the dates be removed?
and are these the exact column names:

hdr -> mkey
trl -> ref_mkey
and which table has column CStatus_flag ?
 
N K
yes column names are exact
NO, I want dates
CStatus_flag is in trl table
 
and column U_datetime ?
also trl?
 
N K
yep
 
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT

SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED
from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp

OPEN Cur_1
 
N K
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 41
Invalid column name 'ref_mkey'.
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 43
Invalid column name 'NStatus_flag'.
 
12:00 PM
Those columns does not exist in inward_doc_tracking_hdr hdr
 
N K
they are in trl
 
inward_doc_tracking_hdr*
 
N K
got it ?
 
Why are you joining these two tables when you WHERE clause only looks on the TRL table??
 
N K
i want to join only for PENDING part.
as it needs validations on that
 
12:03 PM
WHERE trl.N_UserMkey = @CUser_id
AND trl.NStatus_flag = 4
AND trl.CStatus_flag = 1
AND trl.U_datetime BETWEEN '01/04/2016' AND GETDATE()
Is that the correct validation?
 
N K
yes
but for which column u r asking ?
 
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT

SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED
from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp

OPEN Cur_1
Any errors?
 
N K
u missed the second part
for CLOSED
 
Ok wait, should I update the CLOSED column also?
 
N K
yes
i need to go by sequence :P
 
12:07 PM
Ok so I have RECEIVED and PENDING validations..
What is CLOSED validation?
 
N K
let me explain
its simple for cloased
 
Ok
 
N K
WHERE CUser_id = @CUser_id_closed
AND NStatus_flag = 5
AND U_datetime BETWEEN '01/04/2016' AND GETDATE()
this is the condition
 
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT

SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED
from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp

OPEN Cur_1
 
N K
Msg 137, Level 15, State 2, Procedure GET_EMP_REPORT, Line 41
Must declare the scalar variable "@CUser_id_closed".
it has error
 
12:13 PM
Sorry
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT

SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED
from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
FOR SELECT CUser_id, User_Id FROM #temp

OPEN Cur_1
 
N K
yes done
working
now need for WIP and transferred
 
All counts works 100% ?
 
N K
yes
it works 10000%
genius u r
shd I give for WIP ?
 
Cool, WIP validation
 
N K
ok
 
12:15 PM
Is WIP and TRANSFERRED also Counts?
 
N K
YES
all are counts
 
I have 30min to complete this before the next meeting
 
N K
ok
 
Ok
 
N K
will make it fast
for WIP - > NStatus_flag = 2, N_UserMkey = CURSORID, and hdr.Statusfalg = 2
 
12:18 PM
hdr.Statusfalg - correct spelling?
hdr.Statusflag ?
 
N K
yes
sorry my mistake
 
No problem, next one please
 
N K
ok
for transferred - > NStatus_flag = 2, Cuser_ID = CURSORID, and CSTATUS_flag != 1
note
in transferred it is != 1
 
Ok
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT
DECLARE @Count_wip INT
DECLARE @Count_transferred INT

SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED
from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
 
N K
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 60
Invalid column name 'Statusflag'.
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 0
Invalid column name 'WIP'.
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 0
Invalid column name 'TRANSFERRED'.
 
12:23 PM
Is column Statusflag in table inward_doc_tracking_hdr ?
 
N K
yes
its Status_Flag
 
ALTER PROCEDURE GET_EMP_REPORT

AS
BEGIN

DECLARE @Count INT
DECLARE @Count_closed INT
DECLARE @Count_pending INT
DECLARE @Count_wip INT
DECLARE @Count_transferred INT

SELECT *
INTO #temp
FROM (
select distinct a.CUser_id, b.User_Id, b.first_name + ' ' + b.last_name NAME,
0 RECEIVED, 0 CLOSED,
0 PENDING, 0 WIP, 0 TRANSFERRED
from inward_doc_tracking_trl a, user_mst b, inward_doc_tracking_hdr c
where a.CUser_id = b.mkey
) AS x

/**************** RECEIVED ***************************/
DECLARE Cur_1 CURSOR
 
N K
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 0
Invalid column name 'WIP'.
Msg 207, Level 16, State 1, Procedure GET_EMP_REPORT, Line 0
Invalid column name 'TRANSFERRED'.
 
Execute following script :

DROP TABLE #temp
and then execute the last ALTER PROCEDURE again please
 
N K
ok
yes done
 
12:27 PM
Working?
 
N K
ye
ss
just hold on for 5-10 mins
let me confirm w manager
 
Ok so you need to DROP the Temp table in your script after everything has been executed
otherwise this table continue to exist in memory
 
N K
OK
So, just hold on
i will be back in 10mins
 
Ok
 
N K
checking with manager
u their ?
?
hey bri
bro*
are you their
?
 
12:46 PM
Hey I am here
Have meeting in 10mins
what did your manager say?
??
 
N K
1:09 PM
he said
everything is fine
cheers
I will accpet ur answer
now i want one more procedure for department wise
as it user wise
 
 
1 hour later…
N K
2:31 PM
r u their ?
 
N K
3:13 PM
will meet u tomorrow
leaving for now
 

  last day (15 days later) »