Help needed writing simple PL/SQL statement

Hi,
I need to run the following delete statement in a PL/SQL procedure but for it to commit every 1000 rows or so. Can anyone help?
DELETE
FROM IDIS.YPROCRULES A
WHERE 1 = 1
AND NOT EXISTS (SELECT 'X' FROM IDIS.YPROCCTRL B WHERE A.SEQ_0 = B.SEQ_0)
Thanks
Mark

The fastest most efficient way of doing the delete is to write it in one sql statement as you have done.
Commiting every 1000 rows means you have to use row by row processing. A lot of us call this slow by slow processing.
Every time you commit in a loop , you increase the risk of snapshot too old error, plus you actually slow down the processing, as well as give you issues with regards to what happens if your process fails after x iterations. (some of your data is commited, other data is not, how do you restart the process)
The correct approach is to have appropriately sized rollback/undo segments.

Similar Messages

  • Help!  Syntax Error in SQL statement

    Hello. I'm getting an error message and I'm just not seeing
    where I went wrong. The SQL statement is:
    updateSQL = "UPDATE TrainingHistory SET Status='" &
    fFormat(Request.Form(cStatus)) & "', StatusComments='" &
    fFormat(Request.Form(cStatusComments)) & " WHERE Training_ID="
    & fFormat(Request.Form(cTrainingID))
    The error message is:
    [Microsoft][ODBC Microsoft Access Driver] Syntax error in
    string in query expression '' WHERE Training_ID=9054'.
    I've been looking at it for a while. Not sure where I went
    wrong. Here is a more complete version of the code:
    <%
    Function fFormat(vText)
    fFormat = Replace(vText, "'", "''")
    End Function
    Sub sRunSQL(vSQL)
    set cExecute = Server.CreateObject("ADODB.Command")
    With cExecute
    .ActiveConnection = MM_coldsuncrea_lms_STRING
    .CommandText = vSQL
    .CommandType = 1
    .CommandTimeout = 0
    .Prepared = true
    .Execute()
    End With
    End Sub
    If Request.Form("action")="update" Then
    'Set variables for update
    Dim updateSQL, i
    Dim cTrainingID, cStatus, cStatusComments
    'Loop through records on screen and update
    For i = 1 To fFormat(Request.Form("counter"))
    'Create the proper field names to reference on the form
    cTrainingID = "Training_ID" & CStr(i)
    cStatus = "Status" & CStr(i)
    cStatusComments = "StatusComments" & CStr(i)
    'Create the update sql statement
    updateSQL = "UPDATE TrainingHistory SET Status='" &
    fFormat(Request.Form(cStatus)) & "', StatusComments='" &
    fFormat(Request.Form(cStatusComments)) & " WHERE Training_ID="
    & fFormat(Request.Form(cTrainingID))
    'Run the sql statement
    Call sRunSQL(updateSQL)
    Next
    'Refresh page
    Response.Redirect("ClassUpdateRoster.asp?Training_ID=") &
    (rsClassDetails.Fields.Item("event_ID").Value)
    End If
    %>

    You need another single quote after the double quote before
    the WHERE clause. You are not closing the single quote you used to
    delimit the value for StatusComments.

  • Help With SUBSTR in dynamic SQL statement

    Following is the dynamic SQL statement.
    EXECUTE IMMEDIATE 'UPDATE table_name pml
    SET pml.'|| con_fields.field ||' = SUBSTR(pml.'||con_fields.field||' ||'' ''||
    (SELECT pml1.'||con_fields.field||'
    FROM table_name pml1
    WHERE pml1.grp_id = '||los_concats.grp_id ||'
    AND pml1.row_id = '||los_concats.row_id||'
    AND pml1.loser_flg = ''Y''),1, '||con_fields.max_length||')
    WHERE pml.grp_id = '||los_concats.grp_id ||'
    AND pml.loser_flg IS NULL ';
    what it does is that it updates a particular field. This field is concatenated by a field of a similar record.
    My problem is with SUBSTR function. Since I am concatenating fields I do not want the field to be updated greater than max_length on that field, the reason why I use SUBSTR. the select query inside SUBSTR works alright with one of the AND condition in a WHERE clause not present. When I add that additional condition it gives me this error.
    ORA-00907: missing right parenthesis.
    Is there any way to get around this problem. Does SQL has other than SUBSTR function which can limit the character length.
    Appreciate it.

    The other alternative I thought about was to do this first
    EXECUTE IMMEDIATE 'SELECT pml.'||con_fields.field||'
    FROM table_name pml
    WHERE pml.grp_id = '||los_concats.grp_id||'
    AND pml.row_id = '||los_concats.row_id||'
    AND pml.loser_flg = ''Y''
    ' INTO v_concat_field;
    write into the variable v_concat_field and then use it into the previous script.
    But on this I get SQL Command not properly terminated, I don't get it Why?
    Donald I tried with your suggested script. It works fine with one of the conditions eliminated. I don't understand what the error trying to say?
    Thanks

  • Just a helpful SAP note for slow SQL statements

    Hi Guys,
    Just wanted to share this SAP note with you. I am sure many of you might have gone through it.
    [SAP Note 155413 - Analysis of slow SQL statements|https://websmp130.sap-ag.de/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/sapnotes/index2.htm?numm=155413]
    Cheers,
    Abu

    >
    ZAFCO ABAP wrote:
    > Hi Guys,
    >
    > Just wanted to share this SAP note with you. I am sure many of you might have gone through it.
    >
    > [SAP Note 155413 - Analysis of slow SQL statements|https://websmp130.sap-ag.de/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/sapnotes/index2.htm?numm=155413]
    >
    > Cheers,
    > Abu
    Hi,
    I would bet not as many as you might think -  many questions here suffering form not GOING through
    the analysis before they post it here. 
    Meantime I think questions to SQL statement performance here will only be answered when the execution plan on the statement  is posted with it.
    bye
    yk

  • Need help in writing a tricky sql/PL/Sql Procedure

    Hi,
    I have a Branch table with just two fields in it node1 and Node2, The table looks like this
    node1 | node2
    -------|---------
    1 | 2
    3 | 2
    3 | 4
    4 | 5
    1 | 5
    1 | 6
    5 | 6
    in the above data if you draw lines between node_1 and node_2 you will see that there are 3 different paths to get from 1 to 5, as follows
    /--------6-------\
    1----------------------5
    \2------3-------4/
    Now I need to Find all the nodes between 1 and 5 or all the possible paths from node 1 to node 5 in this case I have 3 possible paths they are as follows
    1-2-3-4-5
    1-3-5
    1-5
    Now I know if this data was stored in a hierarchical manner I can get the results i want by using CONNECT BY and SYS_CONNECT_BY_PATH.
    Is there a way to transform this data into a hierarchcal manner so that I can use the CONNECT BY or can somebody give me an Idea as to how to write a PL/SQL procedure to get the result I want.
    Any help will be greatly appreciated

    Hi!
    1.Create another table Branch1 to hold 'transformed data'.
    2.Use the following procedure to insert data into branch1 table.
    declare
    cursor n is select * from branch;
    begin
    for nrec in n loop
    if nrec.node1 = 5 then
         insert into branch1(node1,node2) values(nrec.node2,nrec.node1);
    elsif nrec.node2= 5 then
         insert into branch1(node1,node2) values(nrec.node1,nrec.node2);
    else
    if      nrec.node2> 5      then
         insert into branch1(node1,node2) values(nrec.node1,nrec.node2);
    elsif nrec.node1> 5 then
         insert into branch1(node1,node2) values(nrec.node2,nrec.node1);
    elsif (nrec.node1 < nrec.node2 ) then
         insert into branch1(node1,node2) values(nrec.node1,nrec.node2);
    else
         insert into branch1(node1,node2) values(nrec.node2,nrec.node1);
    end if;
    end if;
    end loop;
    commit;
    end;
    3.Then execute the following connect by query:
    SQL> select lpad(' ',level*2,' ')||node1 from branch1
    2 start with node1=1
    3 connect by prior node2=node1;
    Hope this is satisfactory.
    Thanks!

  • Sql gurus help needed.. simple but awkward

    Im trying to use dates stored in a char field...field called segment1
    2006/09/11 00:00:00 This is the format they are being stored as
    When i select this data im converting to date format to make it look good to_date(tppc.segment1,'YYYY-MM-dd:HH24:MI:SS')
    The problem is in the where clause, when i goto put a condition in its like it does not recognise the date format.....
    so where to_date(tppc.segment1,'YYYY-MM-dd:HH24:MI:SS') = '31-OCT-2008' ..... error - ORA-01841: (full) year must be between -4713 and +9999
    so where to_date(to_date(tppc.segment1,'YYYY-MM-dd:HH24:MI:SS'),'DD-MON-YYYY') = '31-OCT-2008' again error ORA-01841
    I have checked all the data and the format 2006/09/11 00:00:00 is the same in all records...
    Does someone know what to do ....
    Data Output ---- TO_CHAR(to_date(tppc.segment1,'YYYY-MM-dd:HH24:MI:SS'),'DD-MON-YYYY') - 11-SEP-2006

    Hi Sharky,
    Are you completely sure that all of the records in your table contain properly formated date strings in segment1?
    you can check it with a simple function:
    create or replace FUNCTION "VALIDATE_DATE"
        (   date_String VARCHAR2,
            format      VARCHAR2 := 'dd-mon-yyyy' )
        RETURN VARCHAR2
        DETERMINISTIC
    IS
        dt DATE;
    BEGIN
        dt := to_date( date_string, format );
        RETURN 'Y';
    EXCEPTION
        WHEN OTHERS THEN
            RETURN 'N';
    END;
    /which you use to select records which have invalid dates like so:
    select *
      from your_table
    where validate_date(segment1, 'YYYY-MM-dd:HH24:MI:SS')='N';The next thing to consider is that when you use a to_date function on segment1 you are converting it to a date data type, but in your comparisson you are comparing that data data type to a string constant. To perform the equality test Oracle needs to change the data type of one side of the equality to match the data type on the other side of the equality. Generally speaking you should explicitly convert one of them to match the other. For example you should wrap your string constant in a to_date function so you end up comparing two dates, or since the segment1 value is already in a canonical date format, you could change your string constant to the same canonical date format removing the to_date off of segement1 so you are just doing a string comparison.

  • Help needed in debugging dynamic SQL.

    When I pass the p_deptno =30 and run the below code the refcursor returns the rows.But when I pass the dpetno =null then it goes to the condition if p_job is not null and throws an error.Can any one please help me in debugging this code.
    declare
    p_deptno number:=null;
    p_job varchar2(30):='SALESMAN';
    v_sql varchar2(4000);
    l_ename varchar2(30);
      TYPE my_cursor IS REF CURSOR;
      RC my_cursor;
    begin
      v_sql := 'select ename
                from emp
               where 1=1';
      if p_deptno is not null then
        v_sql := v_sql||' AND deptno='||p_deptno;
      else
        if p_job is not null then
          v_sql := v_sql||' AND job='||p_job;
        end if;
      end if;
      OPEN RC FOR v_sql;
      LOOP
        FETCH RC INTO l_ename;
        EXIT WHEN RC%NOTFOUND;
        dbms_output.put_line(l_ename);
      END LOOP;
      CLOSE RC;
    END;Thanks.
    Edited by: user3565577 on Mar 6, 2010 8:44 PM

    Hi
    I've mocked up a simple test and it seems to use the indexes when I try with one or the other values being supplied, I've made an assumption on the indexes you have (i.e. single column indexes on job and deptno),
    --indexes on emp table:
    p2056@dbapw01> @indexes
    Enter value for table_name: emp
    Enter value for owner: p2056
    INDEX_OWNER                    INDEX_NAME                INDEX_TYPE                  PAR COLUMN_NAME                    STATUS
    P2056                          EMPI1                     NORMAL                      NO  DEPTNO                         VALID
    P2056                          EMPI2                     NORMAL                      NO  JOB                            VALID
    2 rows selected.
    --when accessing with a job value
    p2056@dbapw01> explain plan for select *
      2  from emp
      3  where (
      4   (null is null and job = 'SALESMAN')
      5   OR
      6   ('SALESMAN' is null and deptno = null)
      7   OR
      8   ('SALESMAN' is null and null is null)
      9         );
    Explained.
    p2056@dbapw01> @xplan
    PLAN_TABLE_OUTPUT
    Plan hash value: 1888885832
    | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT            |       |     2 |   154 |     1   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| EMP   |     2 |   154 |     1   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | EMPI2 |     2 |       |     1   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - access("JOB"='SALESMAN')
    --when accessing with a deptno value
    p2056@dbapw01> explain plan for select *
      2  from emp
      3  where (
      4   (2 is null and job = null)
      5   OR
      6   (null is null and deptno = 2)
      7   OR
      8   (null is null and 2 is null)
      9         );
    Explained.
    p2056@dbapw01> @xplan
    PLAN_TABLE_OUTPUT
    Plan hash value: 1336173234
    | Id  | Operation                   | Name  | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT            |       |     1 |    77 |     1   (0)| 00:00:01 |
    |   1 |  TABLE ACCESS BY INDEX ROWID| EMP   |     1 |    77 |     1   (0)| 00:00:01 |
    |*  2 |   INDEX RANGE SCAN          | EMPI1 |     1 |       |     1   (0)| 00:00:01 |
    Predicate Information (identified by operation id):
       2 - access("DEPTNO"=2)

  • Help needed in building a  sql query

    Hello,
    I am using Oracle 10g db.
    I have 3 tables table1 and table2 and table3
    I am writing one sql query which is like
    select table1.a a1,(select distinct b from table2,table3 where table2.id=table3.id and table1.id=table2.id) b1
    from table1
    Now the b1 value may give more then 1 values so when i am trying to execute the query its giving me error.
    What i would like to have is if it gives returns more then 1 value then add that value as a new column means if b1 gives like abc and def as values.
    Then i want the sql to return like
    acolvalue abc def as a single row.
    Is this possible to do?
    Thanks

    Hello,
    The approach which i took is i wrote a function which gives me the b values , sseparated.
    Then i am building a outer query considering the max of b so i just found there are max 10 values which one row is showing.
    select b11,b12,b13,,,b10
    from (
    select table1.a a1,func(select distinct b from table2,table3 where table2.id=table3.id and table1.id=table2.id) b1
    from table1)
    but now i am facing problem like the value of b1 is a,b,c
    i want to use the substr and instr function to get
    a as b11
    b as b12
    c as b13
    can anyone pls help me out to write a query? i am getting b11 but other values are somehow not coming.
    for b11
    i used
    substr(b1,1,instr(b1,',',1,1)-1)
    Thanks

  • Help needed in executing pl/sql programs

    hi all
    iam new to PL/SQL programming
    i have installed oracle 9i
    iam trying to practice some sample pl/sql programs
    i have opened oracle 9i and typed these commands
    sql>ed sum
    then a notepad opens where i type my pl/sql program and save it and then return back to oracle 9i and type this
    sql>@ sum
    then my pl/sql program gets executed.......but iam not getting any output
    its comig as procedure implemented succesfully
    is it compulsory to open notepad to execute pl/sql progams or we can direclty type the programs in oracle 9i
    please help in this matter ASAP
    Regards
    Suresh

    Yes, you can type the program directly at the SQL prompt, but editing will be a bit difficult.
    You should use some good editor that allows you to edit properly and then run like you did.
    You need to do:
    SQL> set serveroutput onbefore running your program to see dbms_output messages.

  • Help needed with Express and SQL Dev

    Recently was asked to make a sql server application work with Oracle. I have never worked with Oracle products and no one in my small shop has either. I downloaded the Express 10g onto a virtual machine on my dev server and oracle SQL Developer locally. I have no idea how to connect to the Express db on the prod server using sql developer. I have tried Basic and TNS connection types and all the errors are very cryptic. Any help is appreciated. Do I need to install anything client side to connect to the server?
    I figured it out. I had to use the Basic connection type and I DL'd the J2EE .
    Edited by: jt_stand on Sep 25, 2008 10:51 AM

    Had to use the Basic connection type and get the right combination of options. I can now connect to my remote Express server with my local sql developer program.

  • Help Needed on Read/Modify Line Statement

    Hi,
    I am writing a report which is having a check box as first column, followed by sales data. I am giving F4 Key to user to Select ALL the check box. I have used Modify Line to do this, but it is not working for me.
    Here is the pseudo code which i am using.
    Report <report name>.
    data cb1 value SPACE.
    Write :/ cb1 as CHECKBOX, 'X'.
    Write :/ cb1 as CHECKBOX, 'Y'.
    Write :/ cb1 as CHECKBOX, 'Z'.
    AT PF04.
       DO 3 times.
          MODIFY LINE sy-index field value cb1 from 'X'.
       ENDDO.
    This is not working and giving weird results.
    I tried the following things too.
    Trial a:
    cb1 = 'X'.
    modify line sy-index field value cb1.
    Trial b:
    modify line sy-index of current page value cb1 from 'X'.
    Trial c:
    read line sy-index....
    modify line sy-index..
    But none of them were working.
    Please let me know if my approach to select all check box is correct. Is there any alternate way to select all the checkbox in the list?..What is wrong with my modify line statement.
    Regards
    Mitesh

    Hi Anand,
    Thanks for throwing a light on the "Read Statement". This cleared up my concept of modify line too.
    When I dont use Read Line, the output will be:
    Checked X
    Checked X
    Checked X
    Let me try to answer the question as "Why it is happening".Correct me if i am wrong as i am only 15 days Old for ABAP ;-((.
    Here is how I will now defining modify line statement. Modify Line Statement modify the current line or line number n with the value of sy-lisel. With Field Addition in Modify line, you change the content of sy-lisel.
    So in our case, the sy-lisel value is (Checked,'X').
    So if we dont use read line, only first column will get updated , whereas second field will remain in the sy-lisel.
    So to get the value of other lines, we use read line. This will fill up the sy-lisel with the the line content of sy-index and so modify will work.
    So, if we are using AT LINE-SELECTION event to select line, in that case, there is no need to do a read line as sy-lisel is already in the buffer.
    Please correct me if i am wrong.
    Regards
    Mitesh

  • Help Needed Writing Query

    Hi Users,
    I have a table which has the records inserted on every Saturday (i:e on a weekly basis).
    The table "NCIM" has the following records in it
    NODE              INTERFACE      CREATEDATE
    172.29.58.195     4     1/1/2011
    172.29.60.1     11     1/1/2011
    172.16.10.44     3     1/1/2011
    172.29.58.195     4     25/12/2010
    172.29.60.1     11     25/12/2010
    172.16.10.44     3     25/12/2010
    172.29.58.195     2     18/12/2010
    172.29.60.1     8     18/12/2010
    172.16.10.44     5     18/12/2010
    172.29.58.195     6     11/12/2010
    172.29.60.1     22     11/12/2010
    172.16.10.44     7     11/12/2010The requirement is to find, what was the "INTERFACE" value for the particular NODE on the particular window period.
    1) When I run the query any time in the current week (between Jan 1st and Jan 7th 2011) then
    my output should be
    NODE             INTERFACE      CREATEDATE
    172.29.58.195     4     1/1/2011
    172.29.60.1     11     1/1/2011
    172.16.10.44     3     1/1/20112) When I run the query with the condition for the last two week from the current date (subtracting 14 days from current date 4th Jan is Dec 22nd 2010) falling between Dec 18th and Dec 25th. So the output should be
    NODE             INTERFACE       CREATEDATE
    172.29.58.195     2     18/12/2010
    172.29.60.1     8     18/12/2010
    172.16.10.44     5     18/12/2010Can some please help how to accomplish this with the query.
    Thanks.
    RR

    Hi,
    you can use the solution from frank or if you have real dates for the select, you can do it with a simple date_field <to_date('25.12.2010','DD.MM.YYYY') and date_field>=to_date('18.12.2010','DD.MM.YYYY')
    Regards
    Udo
    SQL> create table a_table (interface number,date_field date);
    Tabelle wurde angelegt.
    SQL> insert into a_table (interface,date_field) values(1,to_date('11.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(2,to_date('11.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(3,to_date('11.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(4,to_date('18.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(5,to_date('18.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(6,to_date('18.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(7,to_date('25.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(8,to_date('25.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(9,to_date('25.12.2010','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(10,to_date('01.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(11,to_date('01.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(12,to_date('01.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(13,to_date('08.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(14,to_date('08.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL> insert into a_table (interface,date_field) values(15,to_date('08.01.2011','DD.MM.YYYY'));
    1 Zeile wurde erstellt.
    SQL>
    SQL> select * from a_table where date_field <=sysdate and date_field>=sysdate-7;
    INTERFACE DATE_FIE
    10 01.01.11
    11 01.01.11
    12 01.01.11
    SQL> select * from a_table where date_field <to_date('25.12.2010','DD.MM.YYYY') and date_field>=to_date('18.12.2010','DD.MM.YYYY');
    INTERFACE DATE_FIE
    4 18.12.10
    5 18.12.10
    6 18.12.10

  • Help needed with simple image enlargement when viewing a gallery

    Hi
    I have a gallery component linked to a data thumb list. This all scrolls fine and works. When someone clicks on a thumb it shows the
    image larger. Then I would like it if you click on the larger image it fills the height of the page to see an even bigger view. I have managed to do this but some of the buttons do not work in the area were this image is even if the image is not showing and the image will not go back to its off state even though it has an interaction to do so. If I drag the image component down the layers panel so it is behind all the other bits on my page it all works, the image enlarges and disappears so the previous state is shown. The problem with this is I want the image to cover everything behind it, not be behind everything. I have included a couple of screen grabs just to give you an idea.
    sc 1 simple data list and image component
    sc2 Enlarged image but can not get it to go
    sc3 enlarged image can come and go no changes except I have moved the layer panel down
    to the bottom
    Any ideas how I can get this to work properly would be much appreciated
    many thanks

    I have sorted this out. There might be other ways of doing this but I added a button to the large image gallery component.
    constructed a rectangle the same size as the large image and set the opacity to 0. The whole area is now a button you can not see.
    One click anywhere on the image removes it with an interaction back to the first state which is off.
    hope this help anyone

  • Help needed in forming dynamic insert statement

    HI,
    I am trying to construct a insert statement as given below.
    Please help me as to how to form this and where am i going wrong?
    i want the columns names to be auto populated when a table name is given.
    output should be like:
    ==============
    insert into tablename
    col1,col2,col3..coln)
    values
    (col1, col2, col3..coln);
    declare
    v_sql varchar2(5000);
    cnt number :=0;
    col_count number;
    name varchar2(5000); 
    TYPE string_assarrtype IS TABLE OF VARCHAR2 ( 25 ) INDEX BY VARCHAR2 ( 20 );
    arr string_assarrtype;
    BEGIN
       select count(column_name) into col_count from user_tab_columns
      where
      table_name = 'PS_D_RE_BCSREGIONMAPPING';
       v_sql:='insert into tablename (' ;
         for i  in 1..arr(col_count)
          loop
           v_sql:='v_sql' ||' '||arr(i);
          DBMS_OUTPUT.PUT_lINE(v_sql);
         END LOOP;
    END;

    Below is the actual code which i am trying to write:
    Since the below trigger logic is same for around 25/30 tables i wanted to construct a generic procedure which will take input parameter as tablename and auit_table and form the insert statement which will populate the column names which are different for different tables.
    Please guide...
    CREATE OR REPLACE PROCEDURE make_trigger(  tablename  in  VARCHAR2,
                                             auditable  in   VARCHAR2                                                                                  
    AS
    v_sql varchar(10000);
    begin
    v_sql := 'DROP TRIGGER  TRD_D_AUDIT_D'||tablename;
    dbms_output.put_line (v_sql );
    v_sql:='CREATE OR REPLACE TRIGGER TRG_D_AUDIT_D_'||tablename||' '||
    'BEFORE INSERT OR UPDATE OR DELETE ON 'tablename||' '||' REFERENCING OLD AS OLD NEW AS NEW
    FOR EACH ROW';
    dbms_output.put_line (v_sql );
    v_sql:= 'DISABLE
    DECLARE
    v_Operator        psoprdefn.oprid%type;
    v_Sysdate        Date;
    v_Actn            varchar2(1);
    V_AUDIT_RECNAME    PSRECDEFN.RECNAME%type;
    BEGIN';
    dbms_output.put_line (v_sql );
    v_sql:= 'If (Inserting) Then
       v_Actn    :='||'''||'A'||'''||';'||
       'Insert into '||auditable||'
                        ('||
                       AUDIT_RECNAME,
                    AUDIT_ACTN,
                    AUDIT_OPRID,
                    AUDIT_STAMP,
                    D_USER_GROUP,
                    D_GROUP_DESC,
                    D_BCS_ID,
                    D_UBR_ID,
                    D_FILTER_ID,
                    D_NAME_ACCESS_TYPE,
                    D_GLOBAL_EMP_LIST
                        Values
                    V_AUDIT_RECNAME,
                    v_Actn,
                    v_Operator,
                    v_Sysdate,
                    :NEW.D_USER_GROUP,
                    :NEW.D_GROUP_DESC,
                    :NEW.D_BCS_ID,
                    :NEW.D_UBR_ID,
                    :NEW.D_FILTER_ID,
                    :NEW.D_NAME_ACCESS_TYPE,
                    :NEW.D_GLOBAL_EMP_LIST
    end if;
    end;Edited by: user11942774 on Oct 5, 2012 11:46 AM
    Edited by: user11942774 on Oct 5, 2012 8:09 PM
    Edited by: user11942774 on Oct 5, 2012 8:10 PM

  • Help with Group By on SQL statement

    Iu2019m trying to group by Item Code in the following query, however, I keep receiving the following error message.  Do you know why?
    Msg 306, Level 16, State 2, Line 1
    The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
    SELECT            dbo.OQUT.DocNum,
                      dbo.OQUT.U_UDF_3 AS QuoteNumber,
                      dbo.OQUT.U_QuoteDesc,
                      dbo.OQUT.U_RevQuote,
                      dbo.OQUT.U_RevDate,
                      dbo.OQUT.DocDate,
                      dbo.OQUT.DocDueDate,
                      dbo.OQUT.U_ReqDate,
                      dbo.OQUT.U_DueDate,
                      dbo.OQUT.CardCode,
                      dbo.OQUT.CardName,
                      dbo.OQUT.CntctCode,
                      dbo.QUT1.LineNum,
                      dbo.QUT1.BaseLine,
                      dbo.QUT1.ItemCode,
                      dbo.QUT1.Dscription,
                      SUM(dbo.QUT1.Quantity),
                      SUM(dbo.QUT1.OpenQty),
                      SUM(dbo.QUT1.U_Original_Qty),
                      dbo.QUT1.U_LeadTime,
                      dbo.QUT1.Price,
                      SUM(dbo.QUT1.LineTotal),
                      dbo.QUT1.unitMsr AS SalUnitMsr,
                      dbo.QUT1.U_UDF_12 AS PNQuoted,
                      dbo.QUT1.U_UDF_13 AS MFRQuoted,
                      dbo.QUT1.U_UDF_16 AS MinQty,
                      dbo.QUT1.U_UDF_17 AS MultiQty,
                      dbo.TBC_AMLPriority1.U_Mfg_Name,
                      dbo.TBC_AMLPriority1.U_Mfg_PN,
                      dbo.QUT1.U_Parent,
                      dbo.ITT1.Quantity AS BOMQty,
                      dbo.OCPR.Name
    FROM         dbo.OQUT INNER JOIN
                      dbo.QUT1 ON dbo.OQUT.DocEntry = dbo.QUT1.DocEntry LEFT OUTER JOIN
                        dbo.OCPR ON dbo.OQUT.CntctCode = dbo.OCPR.CntctCode LEFT OUTER JOIN
    dbo.ITT1 ON dbo.QUT1.ItemCode = dbo.ITT1.Code AND dbo.QUT1.U_Parent =
    dbo.ITT1.Father LEFT OUTER JOIN
                      dbo.TBC_AMLPriority1 ON dbo.QUT1.ItemCode = dbo.TBC_AMLPriority1.U_ItemCode
    WHERE     (dbo.QUT1.Quantity > 0)and dbo.OQUT.DocNum='1'
    GROUP BY    dbo.QUT1.ItemCode,
                dbo.OQUT.DocNum,
                dbo.OQUT.U_UDF_3

    Dear,
    As per your message you mentioned that u r getting following error "
    The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator."
    The error is due to group by of a field whose type is text,ntext.....
    if it is text or ntext just replace that field
    Convert(varchar(100),fieldName)...
    Hope it will help you....
    Regards
    Sanjay

Maybe you are looking for