Link to home
Start Free TrialLog in
Avatar of crazywolf2010
crazywolf2010Flag for United Kingdom of Great Britain and Northern Ireland

asked on

T-SQL help

Hi,
I am writing a small TSQL procedure and need a help.

I am not sure how to use a cursor and select records from table matching the variable in an array.

ALTER PROCEDURE test
AS
BEGIN
   CREATE TABLE #test  
(
     datenow  varchar(100)
    ,process_id  varchar(100)
    ,countrecords  varchar(100)
    ,colour  varchar(100)
)

declare @List_Process_Id table
(
process_id varchar(100)
)

insert into @List_Process_Id(process_id) values ('Process1')
insert into @List_Process_Id(process_id) values ('Process2')
insert into @List_Process_Id(process_id) values ('Process3')
insert into @List_Process_Id(process_id) values ('Process4')

select * from @List_Process_Id

declare @i int
declare @cnt int
declare @l_process_id varchar(100)

select @i = min(idx) - 1, @cnt = max(idx) from @List_Process_Id

while @i < @cnt
begin
fetch next from @List_Process_Id into @l_process_id

insert into #test(datenow,process_id,countrecords,colour) 
select convert(varchar,GETDATE(),103) date, @l_process_id,COUNT(*), case when COUNT(*) > 1000 then  'Green' else 'Red' end 
from table1
where process_id=@l_process_id

print @l_process_id

select * from #test

END 

Open in new window

SOLUTION
Avatar of Beartlaoi
Beartlaoi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial