sql - Create a Cursor and loop all users data -


i have stored procedure gives me records below.

sc

now want is. want create cursor sp loops every user , fetch records.

below sp.

alter procedure get_inward_reminder_report                         begin                        select distinct                           u.first_name + ' ' + u.last_name username,                     th.*,                  case                          when tl.u_datetime < dateadd(d, -5, getdate())                          m.reporting_to                             else null                              end reporting_1                         inward_doc_tracking_trl tl                         inner join inward_doc_tracking_hdr th                             on th.mkey = tl.ref_mkey                         inner join user_mst u                             on th.user_id = u.mkey                         inner join emp_mst m                             on m.mkey = u.employee_mkey                         tl.nstatus_flag not in (5,14)                         , tl.u_datetime < dateadd(d, -3, getdate())             end go 

kindly let me know how this.

try this. dont use cursors. redundant. please keep in mind educational show how use while loop , not use cursors. sql format incorrect , syntax can improved. shows how loop only.

alter procedure get_inward_reminder_report                         begin              declare @username varchar(512)               create table #reportdata             (                username varchar(128),                mkey varchar(64),               ...................             )                        select distinct                           u.first_name + ' ' + u.last_name username,                     th.*,                  case                          when tl.u_datetime < dateadd(d, -5, getdate())                          m.reporting_to                             else null                              end reporting_1                         #data                         inward_doc_tracking_trl tl                         inner join inward_doc_tracking_hdr th                             on th.mkey = tl.ref_mkey                         inner join user_mst u                             on th.user_id = u.mkey                         inner join emp_mst m                             on m.mkey = u.employee_mkey                         tl.nstatus_flag not in (5,14)                         , tl.u_datetime < dateadd(d, -3, getdate())                while(select count(*) #data) > 0              begin                     select top 1 @username = username                     #data                      insert #reportdata                     select *                     #data                     username = @username                       delete #data                     username = @username              end                   select *                   #reportdata                   drop table #reportdata                  drop table #data             end go 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -