How to run a cursor autmatic

I have a requirement where i need to run a cursor automatically at midnight (on change of date) to insert closing balances of inventory stock to a table.
Thanks
Ahon

you have to create a database procedure to execute your statement.
Then create a job in sql*plus like this:
variable sqlplus_jobno number;
declare
cursor get_cur_jobno is
select uj.job
from   user_jobs uj
where what = lower ( 'your_procedure_name' );
begin
open  get_cur_jobno;
fetch get_cur_jobno into :sqlplus_jobno;
close get_cur_jobno;
if
  :sqlplus_jobno is not null
then
  dbms_job.remove ( :sqlplus_jobno );
end if;
dbms_job.submit ( :sqlplus_jobno,
                  'your_procedure_name;',
                  sysdate,
                  'trunc ( sysdate, ''hh24'' ) + (1)', false );
end;
commit;
print sqlplus_jobno This will execute your procedure every day at midnight and onetime immediatly.
To wake up the the job process in your database, you have to look on two
system parameters called job_queue_processes and job_queue_interval .
set job_queue_processes to minimum 1 (one process is started)
and job_queue_interval to 60 (check every minute for a job)

Similar Messages

  • How to run the cursor

    HI
    can i knw how to run the cursor from the package. The cursor name is "cursor fet_rec".
    procedure update_hdr is
    cursor fet_rec is
    i rhave the run procdure but i am unable to run the cursor.
    procedure update_hdr is
    cursor fet_rec is
    select status, request_hdr_id
    from xxkfupm01.kfupm_mcr_request_header
    where status = 'Approved'
    for update;
    cnt1 NUMBER := 0;
    cnt2 NUMBER := 0;
    BEGIN
    for rec in fet_rec Loop
    select count(*)
    into cnt1
    from XXKFUPM01.kfupm_mcr_request_details
    where request_hdr_id = REC.REQUEST_HDR_ID;
    select count(*)
    into cnt2
    from XXKFUPM01.kfupm_mcr_request_details
    where request_hdr_id = REC.REQUEST_HDR_ID
    and status = 'Complete';
    if (cnt1 = cnt2) THEN
    update xxkfupm01.kfupm_mcr_request_header
    set status = 'Complete'
    where current of fet_rec;
    END IF;
    END LOOP;
    Regards
    Arif
    Edited by: user11309801 on Jan 3, 2011 6:56 AM
    Edited by: user11309801 on Jan 3, 2011 7:00 AM

    http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
    http://www.oracle.com/pls/db102/homepage
    http://www.oracle.com/pls/db112/homepage

  • How to run the cursor loop once even if it is true for many times - urgent

    Hi,
    Say
    loop
    tdate='12-JAN-2005'
    loop
    cursor.......
    if '10-JAN-2005'<=tdate then
    fetch
    end loop
    tdate=tdate+1 month
    end loop;
    in the above query i want my cursor to run the loop only once at the first true of my condition....in the second run the tdate will be = '12-FEB-2005'....but my cursor loop should not go in..it has to comeout....
    how to do this....plz help very urgent

    in the above query i want my cursor to run the loop only once at the first true of
    my condition....in the second run the tdate will be = '12-FEB-2005'....but my
    cursor loop should not go in..it has to comeout....Simply control has been the cursor processed or not.
    Something like that
    SQL> declare
      2    tdate date := to_date('12-JAN-2005','DD-MON-YYYY');
      3    cursor cr is select * from dual;
      4    rt dual%rowtype;
      5    cursor_has_been_processed boolean := false;
      6  begin
      7   loop
      8     if to_date('10-JAN-2005','DD-MON-YYYY') <=tdate then
      9             if not cursor_has_been_processed then
    10                     open cr;
    11                     loop
    12                     fetch cr into rt;
    13                     exit when cr%notfound;
    14                     end loop;
    15                     close cr;
    16                     cursor_has_been_processed := true;
    17                     dbms_output.put_line('Cursor has been processed');
    18             end if;
    19     --DO something
    20     null;
    21          end if;
    22     dbms_output.put_line(tdate);
    23     tdate := add_months(tdate,1);
    24     exit when tdate >= to_date('12-MAY-2005','DD-MON-YYYY');
    25   end loop;
    26  end;
    27  /
    Cursor has been processed
    12-JAN-05
    12-FEB-05
    12-MAR-05
    12-APR-05
    PL/SQL procedure successfully completed.Rgds.

  • When I run the cursor over a template it shows several pages but when i select the document it only brings up the first two pages to edit. How do I see them all?

    When I run the cursor over a template it shows several pages but when i select the document it only brings up the first two pages to edit. How do I see them all?

    Skimming the templates in the Template Chooser shows you all of the pages available in the template once you open it. You can insert the one(s) you want, even more than one of any of them. Most users don't want or need all of the different pages, especially when they start the document.

  • How to run a stored procedure in TOAD 9.0

    Dear Friends,
    I am using TOAD 9.0 .
    Running a procedure but getting the error ..
    create or replace PROCEDURE UPD_TBL(I_table_name IN VARCHAR2) IS
    CURSOR c_columns is select column_name from ALL_TAB_COLUMNS where
    TABLE_NAME = I_table_name;
    BEGIN
    FOR rec in c_columns
    LOOP
    IF ( rec.COLUMN_NAME = 'X_NNM_TYPE_CD' ) THEN
    UPDATE I_table_name set X_NNM_TYPE_CD = REPLACE (X_NNM_TYPE_CD, '-', '_')
    WHERE INSTR (X_NNM_TYPE_CD, '-') > 0 ;
    ELSIF ( rec.COLUMN_NAME = 'X_REVENUE_TYPE_CD' ) THEN
    UPDATE I_table_name set X_REVENUE_TYPE_CD = REPLACE (X_REVENUE_TYPE_CD, '-', '_')
    WHERE INSTR (X_REVENUE_TYPE_CD, '-') > 0 ;
    END IF;
    END LOOP;
    EXCEPTION
    when OTHERS then
    NULL;
    END UPD_TBL;
    ERROR: TABLE OR VIEW DOES NOT EXIST.
    Please let me know how to run the procedure in toad.
    thanks/kumar

    Kumar,
    At a minimum, you would need to remove both the exception handler in your procedure and the exception handler in your anonymous block. As has been mentioned before, the code
    EXCEPTION
      WHEN OTHERS THEN
        NULL;
    END;is almost always a bug. In your case, your code is almost certainly generating an exception and your exception handler is silently catching and ignoring that exception. If you care whether or not any rows are updated, that is a bug.
    Once you remove the exception handlers, you'll very likely discover that the SQL statements you are generating and executing dynamically are malformed. Off the top of my head, it would appear that there is no space between the table name and the SET keyword in the string you are constructing. That means that if I_TABLE_NAME is 'EMP', for example, you'd construct a statement like
    UPDATE empSET x_nnm_typ_cd = ...'Oracle would interpret empSET as the table name, it would interpret X_NNM_TYP_CD as a table alias, and then it would encounter the equals sign unexpectedly and generate a syntax error. You'd need to insert a space after the table name and before the SET keyword (note where folks inserted extra spaces in their sample code).
    In general, you are probably better served by having a local variable that you use to build up the SQL statement. That way, you can print (or log) the SQL statement that is constructed. When you encounter a syntax error at runtime, that will generally make debugging easier. So you'd probably want something like
      l_sql_stmt VARCHAR2(1000);
    BEGIN
      FOR rec IN cur_c_columns LOOP
        IF (rec.column_name = 'X_NNM_TYPE_CD') THEN
          l_sql_stmt :=
             'UPDATE '||i_table_name||
             ' SET x_nnm_type_cd = REPLACE(x_nnm_type_cd, ''-'', ''_'')'||
             'WHERE INSTR(x_nnm_type_cd, ''-'') > 0';
          dbms_output.put_line( l_sql_stmt );
          EXECUTE IMMEDIATE l_sql_stmt;
    ...When you get a syntax error, you can then take a look at the SQL statement that was generated and debug the problem more easily.
    Justin

  • Is there any limit on how many records a cursor can hold?

    Hi Everyone,
    This is Amit here. I want to know whether there is any limit on how many records a cursor can hold.
    I have a program in which i am creating a cursor and passing it to another procedure as an input parameter. But the count of cursor query is more than 15 Lakhs. The program is running forever.
    Just wanted to know whether the huge data is the problem.
    Thanks ....
    Regards,
    Amit

    user13079404 wrote:
    Just wanted to know whether the huge data is the problem.What do you think? How long does your code typically need to wait for the data to leave the magnetic platter of the harddisk, travel across wires and into the memory buffer of your application - for a single row?
    Now multiply that waiting for I/O time with a million - for a million rows. Or by a billion, for a billion rows.
    Is "+huge data+" a problem? Not really - it simple needs more work to get that amount of data from disk. More work means slower performance. It is that simple.
    Which is why the row-by-row approach used by many developers is wrong. You do not pull a million rows from disk and process it in PL/SQL or Java or .Net. Heck, you do not even pull 10,000 rows like that.
    The correct approach is to think data sets and use SQL to process that for you - and only return the bare minimum of data to the application layer. Maximize SQL. Minimize PL/SQL and Java and .Net.

  • How to determine the cursor record count before the "open cursor"?

    Is it possible to determine the record count of an explicit cursor without running a count()? Say, my cursor definition is something like this,
    CURSOR cur_vehicle
    IS
    SELECT os.order_id, os.order_item, vs.part_id
    vs.part_num,
    vs.iso_num,
    vs.model_yr
    vs.dealer_cde,
    vs.cust_cde,
    px.plant_cd
    FROM parts_source vs,
    orders_source os,
    plant_tbl_crossref px
    wHERE os.order_id = vs.order_id
    AND vs.part_id = os.part_id
    AND vs.plant_cde = px.plant_cde
    ORDER BY os.order_id;
    I want to log the count of records returned by the above cursor prior to the first fetch, without running a count(1) for the query in cursor select.
    I know adding " Count(1) over(order by null) " in the cursor SELECT will bring it. But that does not help me log the record count to some log file or table before opening the cursor for processing.
    To conclude, my objective is to update the record count of cursor in some table before processing

    sarvan wrote:
    Is it possible to determine the record count of an explicit cursor without running a count()?
    ..snipped..No. The only way to do it correctly is inside that select.
    Each select is a consistent read. Which means that if this is done as 2 select statements, the 1st select can see a different version of the data than the 2nd select statement. does. So if you want a count and that to be consistent and on the same version of the data than the select, it has to be done as part of the select.
    Also consider what a cursor is. It is not a result set of sorts that is created in memory upfront - with a convenient interface that tells you the size/number of rows of that result set.
    A cursor is basically a program that reads database data as input and produce output. A fetch from a cursor is an instruction for this program to execute and output data.
    There's no data set that is created by the cursor from which the count can be determined. The cursor program does have state variables - like +%RowCount+ that specifies how many rows the cursor has thus far output. And you need to run that cursor to the end (no more output) in order to determine the total number of rows output by the cursor.

  • How to run long running report in the background

    Oracle Application server 10g
    I need to run the long running reports in the background because while the report is running the cursor is loading and the control doesn't return to the user
    this feature exist in oracle 6i by set the following parameter
    RUN_PRODUCT( REPORTS, 'r_1', ASYNCHRONOUS, RUNTIME, FILESYSTEM, pl_id, NULL);
    How can I accomplish the same in OAS 10g?

    Hi,
    I've done this in 11g. I think it will be the same in 10g as well
    Following are the steps in 11g using run_report_object.
    1. Read metalink note: Using the Reports Server Queue PL/SQL Table and API - RW_SERVER_JOB_QUEUE [ID 72531.1]
    2. Implement RW_SERVER_JOB_QUEUE table as per above notes.
    3. When submitting report run it as background
    a. SET_REPORT_OBJECT_PROPERTY(lo_report_object, REPORT_EXECUTION_MODE, ASYNCHRONOUS); -
    SET_REPORT_OBJECT_PROPERTY(lo_report_object, REPORT_COMM_MODE , BATCH);
    4. Display the job_id to user (notification).
    5. Create a new form to view reports base on on RW_SERVER_JOB_QUEUE which provides you the status, etc.... (you may have to create a way to identify the username who submitted the job. This way not all users will see the jobs in RW_SERVER_JOB_QUEUE )
    6. Optionally you can use NOTIFYSUCCESS=email to notify user when the report is finished.
    Cheers
    LS

  • How to run execute immdiate with variables

    Hi friends,
    How to run execute immdiate with variables in v_stmt below?
    I dont know how to declare the value of i here.
    Set serveroutput on;
    DECLARE
       i        VARCHAR (20);
       v_stmt   VARCHAR2 (100);
       CURSOR c
       IS
          SELECT sqlid FROM temp1;
    BEGIN
       OPEN c;
       LOOP
          FETCH c INTO i;
          EXIT WHEN c%NOTFOUND;
          DBMS_OUTPUT.put_line (i);
          v_stmt := 'select * from table(dbms_xplan.display_cursor('&i',null))'
          execute immediate v_stmt;
       END LOOP;
       CLOSE c;
    END;
    /Regds,
    Kunwar.

    You first need to use a bind variable (named ':v' in SQL statement in my example):
    set serveroutput on;
    DECLARE
       i        VARCHAR (20);
       v_stmt   VARCHAR2 (100);
       CURSOR c
       IS
           -- modified for a quick test
          SELECT sql_id FROM v$sql where child_number > 2;
    BEGIN
       OPEN c;
       LOOP
          FETCH c INTO i;
          EXIT WHEN c%NOTFOUND;
          DBMS_OUTPUT.put_line (i);
          v_stmt := 'select * from table(dbms_xplan.display_cursor(:v,null))';
          execute immediate v_stmt using i;
       END LOOP;
       CLOSE c;
    END;
    /However because your SELECT statement returns several rows you need to adapt your code to process all rows returned (as already suggested in first reply to your message).
    Instead of using PL/SQL, I recommend to generate a SQL file using only SQL and then to run the generated SQL file.
    For example:
    spool edx.sql
    set serveroutput on
    declare
    v_stmt varchar2(100);
    v_q char(1):='''';
    begin
    dbms_output.put_line('spool edx.log');
    for s in (select sql_id from v$sql where child_number >2)
    loop
      dbms_output.put_line('select * from table(dbms_xplan.display_cursor(' || v_q || s.sql_id || v_q || ',null));');
    end loop;
    dbms_output.put_line('exit');
    end;
    spool ofThis generates a SQL file similar to :
    spool edx.log
    select * from table(dbms_xplan.display_cursor('5rygsj4dbw6jt',null));
    select * from table(dbms_xplan.display_cursor('5rygsj4dbw6jt',null));
    select * from table(dbms_xplan.display_cursor('5rygsj4dbw6jt',null));
    select * from table(dbms_xplan.display_cursor('fsbqktj5vw6n9',null));
    select * from table(dbms_xplan.display_cursor('6q42j0018w7t8',null));
    select * from table(dbms_xplan.display_cursor('a5mmhrrnpwjsc',null));
    select * from table(dbms_xplan.display_cursor('3c1kubcdjnppq',null));
    select * from table(dbms_xplan.display_cursor('3c1kubcdjnppq',null));
    select * from table(dbms_xplan.display_cursor('9gkq7rruycsjp',null));
    select * from table(dbms_xplan.display_cursor('f0wj261bm8snd',null));
    select * from table(dbms_xplan.display_cursor('ab3swhv5g138y',null));
    select * from table(dbms_xplan.display_cursor('6vgvyh4xw9c5g',null));
    select * from table(dbms_xplan.display_cursor('ak5crjygnpk60',null));
    select * from table(dbms_xplan.display_cursor('9p6bq1v54k13j',null));
    select * from table(dbms_xplan.display_cursor('19x1189chq3xd',null));
    select * from table(dbms_xplan.display_cursor('7sx5p1ug5ag12',null));
    select * from table(dbms_xplan.display_cursor('730vdzhng6m6g',null));
    select * from table(dbms_xplan.display_cursor('730vdzhng6m6g',null));
    select * from table(dbms_xplan.display_cursor('0v3dvmc22qnam',null));
    select * from table(dbms_xplan.display_cursor('0v3dvmc22qnam',null));
    select * from table(dbms_xplan.display_cursor('a1zv6wju3ftgv',null));
    select * from table(dbms_xplan.display_cursor('7ng34ruy5awxq',null));
    select * from table(dbms_xplan.display_cursor('7ng34ruy5awxq',null));
    select * from table(dbms_xplan.display_cursor('b2gnxm5z6r51n',null));
    select * from table(dbms_xplan.display_cursor('b2gnxm5z6r51n',null));
    select * from table(dbms_xplan.display_cursor('g4gp07gt2z920',null));
    select * from table(dbms_xplan.display_cursor('1gu8t96d0bdmu',null));
    select * from table(dbms_xplan.display_cursor('g00cj285jmgsw',null));
    select * from table(dbms_xplan.display_cursor('g00cj285jmgsw',null));
    select * from table(dbms_xplan.display_cursor('g00cj285jmgsw',null));
    select * from table(dbms_xplan.display_cursor('bn4b3vjw2mj3u',null));
    select * from table(dbms_xplan.display_cursor('38243c4tqrkxm',null));
    select * from table(dbms_xplan.display_cursor('2abjfnvy5rkyg',null));
    select * from table(dbms_xplan.display_cursor('350f5yrnnmshs',null));
    select * from table(dbms_xplan.display_cursor('350f5yrnnmshs',null));
    select * from table(dbms_xplan.display_cursor('3s1yukp05bzg6',null));
    select * from table(dbms_xplan.display_cursor('3s1yukp05bzg6',null));
    select * from table(dbms_xplan.display_cursor('1tgukkrqj3zhw',null));
    exit
    PL/SQL procedure successfully completed.Edited by: P. Forstmann on 20 mars 2013 19:06
    Edited by: P. Forstmann on 20 mars 2013 19:33

  • How to run a function

    Hi,
    can anyone tell me how to run this functions while generating reports for labels.
    please i need some guidance.
    create or replace function get_label_nextval(p_label varchar2) return integer
    is
    l_last_num integer;
    cursor c_label is
    select last_num from label_id where label = upper(trim(p_label)) for update wait 10;
    pragma autonomous_transaction;
    begin
    open c_label;
    fetch c_label into l_last_num;
    if l_last_num is not null then
    l_last_num := l_last_num + 1;
    update label_id set last_num = l_last_num where current of c_label;
    commit;
    end if;
    close c_label;
    return l_last_num;
    end;
    Thanks and Regards

    ORA-06519: active autonomous transaction detected and rolled back
    Cause: Before returning from an autonomous PL/SQL block, all autonomous transactions started within the block must be completed (either committed or rolled back). If not, the active autonomous transaction is implicitly rolled back and this error is raised.
    Action: Ensure that before returning from an autonomous PL/SQL block, any active autonomous transactions are explicitly committed or rolled back.
    http://download.oracle.com/docs/cd/E11882_01/server.112/e10880/e4100.htm#sthref2110
    I think your COMMIT should be moved outside your IF.
    Commit just before your RETURN, after your CLOSE CURSOR statement.
    Edited by: hoek on Jan 4, 2010 1:28 PM typo

  • How to run FNDMLSUB - Multiple languages - from PL/SQL

    Hi,
    i would like to submit a FNDMLSUB program (Multi Language request submission) from PL/SQL (need to print multiple invoice programs at once, there is no MLS support for request sets).
    What's the pl/sql procedure for submission? I can submit it, but i don't know how to link it with actual request (parameters ect).
    l_request_id := fnd_request.submit_request( 'FND', 'FNDMLSUB' , '' , null, FALSE, 20003, 191349, 'Y' );
    Thanks,
    Regards,
    Kris

    Workaround:
    Running requests manualy for every language needed (cursor with MLS function)
    fnd_request.set_options(
    implicit => 'NO',
    protected => 'YES',
    language => 'LANGUAGE',
    territory => 'TERRITORY',
    datagroup => '',
    numeric_characters => ',.');
    fnd_request.submit_request(...)
    But still, if anybody knows how to run FNDMLSUB it would be much appreciated.
    Regards,
    Kris

  • How to detect the cursor is in which tableviewcell?

    i have custom tableviewcell which has a textfield inside for user input text, now how can i find cursor is in which cell?
    tableview willBeginEditingRowAtIndexPath and tableview willSelectRowAtIndexPath does not return the correct index.
    in editingStyleForRowAtIndexPath i want to retun UITableViewCellEditingStyleDelete just for cell that cursor is inside. how can i do that??
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
        if(self.uiTableView.editing)
         //i want to return delete style just for cell that cursor is inside.
            return UITableViewCellEditingStyleDelete;
        else
            return UITableViewCellEditingStyleNone;
    So thanks inadvance

    Hi
    follow the below steps
    goto SM37 --> find out your job
    double click on job ---> click on job details(you will find this option in top)
    In the next screen --> you will get application server name and Work process ID
    This two are required if you want to cancel any running job.
    Note: You will find the server name and WPID only for active jobs(which are runnig at present)
    In Your case if you don't know the server name which you have, just select "Home(this is the first option)" system will run the job based on server availability.
    If you want to know all the servers in your system, goto SM50
    Regards,
    Venkatesh

  • HOW TO RUN ADOBE READER IN MACBOOK PRO

    HOW TO RUN ADOBE READER IN MACBOOK PRO

    Please stop using caps.  It is considered shouting and rude.
    You need to download the program. The reader runs when needed.
    Barry

  • How to write a cursor to check every row of a table which has millions of rows

    Hello every one.
    I need help. please... Below is the script (sample data), You can run directly on sql server management studio.
    Here we need to update PPTA_Status column in Donation table. There WILL BE 3 statuses, A1, A2 and Q.
    Here we need to update PPTA_status of January month donations only. We need to write a cursor. Here as this is a sample data we have only some donations (rows), but in the real table we have millions of rows. Need to check every row.
    If i run the cursor for January, cursor should take every row, row by row all the rows of January.
    we have donations in don_sample table, i need to check the test_results in the result_sample table for that donations and needs to update PPTA_status COLUMN.
    We need to check all the donations of January month one by one. For every donation, we need to check for the 2 previous donations. For the previous donations, we need to in the following way. check
    If we want to find previous donations of a donation, first look for the donor of that donation, then we can find previous donations of that donor. Like this we need to check for 2 previous donations.
    If there are 2 previous donations and if they have test results, we need to update PPTA_STATUS column of this donatioh as 'Q'.
    If 2 previous donation_numbers  has  test_code column in result_sample table as (9,10,11) values, then it means those donations has result.
    BWX72 donor in the sample data I gave is example of above scenario
    For the donation we are checking, if it has only 1 previous donation and it has a result in result_sample table, then set this donation Status as A2, after checking the result of this donation also.
    ZBW24 donor in the sample data I gave is example of above scenario
    For the donation we are checking, if it has only 1 previous donation and it DO NOT have a result in result_sample table, then set this donation Status as A1. after checking the result of this donation also.
    PGH56 donor in the sample data I gave is example of above scenario
    like this we need to check all the donations in don_sample table, it has millions of rows per every month.
    we need to join don_sample and result_sample by donation_number. And we need to check for test_code column for result.
    -- creating table
    CREATE TABLE [dbo].[DON_SAMPLE](
    [donation_number] [varchar](15) NOT NULL,
    [donation_date] [datetime] NULL,
    [donor_number] [varchar](12) NULL,
    [ppta_status] [varchar](5) NULL,
    [first_time_donation] [bit] NULL,
    [days_since_last_donation] [int] NULL
    ) ON [PRIMARY]
    --inserting values
    Insert into [dbo].[DON_SAMPLE] ([donation_number],[donation_date],[donor_number],[ppta_status],[first_time_donation],[days_since_last_donation])
    Select '27567167','2013-12-11 00:00:00.000','BWX72','A',1,0
    Union ALL
    Select '36543897','2014-12-26 00:00:00.000','BWX72','A',0,32
    Union ALL
    Select '47536542','2014-01-07 00:00:00.000','BWX72','A',0,120
    Union ALL
    Select '54312654','2014-12-09 00:00:00.000','JPZ41','A',1,0
    Union ALL
    Select '73276321','2014-12-17 00:00:00.000','JPZ41','A',0,64
    Union ALL
    Select '83642176','2014-01-15 00:00:00.000','JPZ41','A',0,45
    Union ALL
    Select '94527541','2014-12-11 00:00:00.000','ZBW24','A',0,120
    Union ALL
    Select '63497874','2014-01-13 00:00:00.000','ZBW24','A',1,0
    Union ALL
    Select '95786348','2014-12-17 00:00:00.000','PGH56','A',1,0
    Union ALL
    Select '87234156','2014-01-27 00:00:00.000','PGH56','A',1,0
    --- creating table
    CREATE TABLE [dbo].[RESULT_SAMPLE](
    [test_result_id] [int] IDENTITY(1,1) NOT NULL,
    [donation_number] [varchar](15) NOT NULL,
    [donation_date] [datetime] NULL,
    [test_code] [varchar](5) NULL,
    [test_result_date] [datetime] NULL,
    [test_result] [varchar](50) NULL,
    [donor_number] [varchar](12) NULL
    ) ON [PRIMARY]
    ---SET IDENTITY_INSERT dbo.[RESULT_SAMPLE] ON
    ---- inserting values
    Insert into [dbo].RESULT_SAMPLE( [test_result_id], [donation_number], [donation_date], [test_code], [test_result_date], [test_result], [donor_number])
    Select 278453,'27567167','2013-12-11 00:00:00.000','0009','2014-01-20 00:00:00.000','N','BWX72'
    Union ALL
    Select 278454,'27567167','2013-12-11 00:00:00.000','0010','2014-01-20 00:00:00.000','NEG','BWX72'
    Union ALL
    Select 278455,'27567167','2013-12-11 00:00:00.000','0011','2014-01-20 00:00:00.000','N','BWX72'
    Union ALL
    Select 387653,'36543897','2014-12-26 00:00:00.000','0009','2014-01-24 00:00:00.000','N','BWX72'
    Union ALL
    Select 387654,'36543897','2014-12-26 00:00:00.000','0081','2014-01-24 00:00:00.000','NEG','BWX72'
    Union ALL
    Select 387655,'36543897','2014-12-26 00:00:00.000','0082','2014-01-24 00:00:00.000','N','BWX72'
    UNION ALL
    Select 378245,'73276321','2014-12-17 00:00:00.000','0009','2014-01-30 00:00:00.000','N','JPZ41'
    Union ALL
    Select 378246,'73276321','2014-12-17 00:00:00.000','0010','2014-01-30 00:00:00.000','NEG','JPZ41'
    Union ALL
    Select 378247,'73276321','2014-12-17 00:00:00.000','0011','2014-01-30 00:00:00.000','NEG','JPZ41'
    UNION ALL
    Select 561234,'83642176','2014-01-15 00:00:00.000','0081','2014-01-19 00:00:00.000','N','JPZ41'
    Union ALL
    Select 561235,'83642176','2014-01-15 00:00:00.000','0082','2014-01-19 00:00:00.000','NEG','JPZ41'
    Union ALL
    Select 561236,'83642176','2014-01-15 00:00:00.000','0083','2014-01-19 00:00:00.000','NEG','JPZ41'
    Union ALL
    Select 457834,'94527541','2014-12-11 00:00:00.000','0009','2014-01-30 00:00:00.000','N','ZBW24'
    Union ALL
    Select 457835,'94527541','2014-12-11 00:00:00.000','0010','2014-01-30 00:00:00.000','NEG','ZBW24'
    Union ALL
    Select 457836,'94527541','2014-12-11 00:00:00.000','0011','2014-01-30 00:00:00.000','NEG','ZBW24'
    Union ALL
    Select 587345,'63497874','2014-01-13 00:00:00.000','0009','2014-01-29 00:00:00.000','N','ZBW24'
    Union ALL
    Select 587346,'63497874','2014-01-13 00:00:00.000','0010','2014-01-29 00:00:00.000','NEG','ZBW24'
    Union ALL
    Select 587347,'63497874','2014-01-13 00:00:00.000','0011','2014-01-29 00:00:00.000','NEG','ZBW24'
    Union ALL
    Select 524876,'87234156','2014-01-27 00:00:00.000','0081','2014-02-03 00:00:00.000','N','PGH56'
    Union ALL
    Select 524877,'87234156','2014-01-27 00:00:00.000','0082','2014-02-03 00:00:00.000','N','PGH56'
    Union ALL
    Select 524878,'87234156','2014-01-27 00:00:00.000','0083','2014-02-03 00:00:00.000','N','PGH56'
    select * from DON_SAMPLE
    order by donor_number
    select * from RESULT_SAMPLE
    order by donor_number

    You didn't mention the version of SQL Server.  It's important, because SQL Server 2012 makes the job much easier (and will also run much faster, by dodging a self join).  (As Kalman said, the OVER clause contributes to this answer).  
    Both approaches below avoid needing the cursor at all.  (There was part of your explanation I didn't understand fully, but I think these suggestions work regardless)
    Here's a SQL 2012 answer, using LAG() to lookup the previous 1 and 2 donation codes by Donor:  (EDIT: I overlooked a couple things in this post: please refer to my follow-up post for the final/fixed answer.  I'm leaving this post with my overlooked
    items, for posterity).
    With Results_Interim as
    Select *
    , count('x') over(partition by donor_number) as Ct_Donations
    , Lag(test_code, 1) over(partition by donor_number order by donation_date ) as PrevDon1
    , Lag(test_code, 2) over(partition by donor_number order by donation_date ) as PrevDon2
    from RESULT_SAMPLE
    Select *
    , case when PrevDon1 in (9, 10, 11) and PrevDon2 in (9, 10, 11) then 'Q'
    when PrevDon1 in (9, 10, 11) then 'A2'
    when PrevDon1 is not null then 'A1'
    End as NEWSTATUS
    from Results_Interim
    Where Test_result_Date >= '2014-01' and Test_result_Date < '2014-02'
    Order by Donor_Number, donation_date
    And a SQL 2005 or greater version, not using SQL 2012 new features
    With Results_Temp as
    Select *
    , count('x') over(partition by donor_number) as Ct_Donations
    , Row_Number() over(partition by donor_number order by donation_date ) as RN_Donor
    from RESULT_SAMPLE
    , Results_Interim as
    Select R1.*, P1.test_code as PrevDon1, P2.Test_Code as PrevDon2
    From Results_Temp R1
    left join Results_Temp P1 on P1.Donor_Number = R1.Donor_Number and P1.Rn_Donor = R1.RN_Donor - 1
    left join Results_Temp P2 on P2.Donor_Number = R1.Donor_Number and P2.Rn_Donor = R1.RN_Donor - 2
    Select *
    , case when PrevDon1 in (9, 10, 11) and PrevDon2 in (9, 10, 11) then 'Q'
    when PrevDon1 in (9, 10, 11) then 'A2'
    when PrevDon1 is not null then 'A1'
    End as NEWSTATUS
    from Results_Interim
    Where Test_result_Date >= '2014-01' and Test_result_Date < '2014-02'
    Order by Donor_Number, donation_date

  • How to run 'Get-AssignedAccess' or 'Set-AssignedAccess' power shell commands in c# Application

    Hi,
    I have console application using which i am trying to run power shell command  like 'Get-AssignedAccess' or 'Set-AssignedAccess'.
    i am using below code for this it is throwing exception 'Get-AssignedAccess' doesn't exist in cmdlet which is correct because these commands belongs to function category.
    using (PowerShell pwInstance = PowerShell.Create())
                            pwInstance .AddScript("Get-AssignedAccess");
                            var result = pwInstance .Invoke();
    How can we execute this kind of command using c#?
    Thanks,

    Hi prakashlight,
    Thank you for comming back and tell us the result. For more information about how to run PowerShell script in C# language, you can refer to this blogpost here:
    Executing PowerShell scripts from C#
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

Maybe you are looking for