Huge Dynamic SQL

Hi,
I have a 10 page running CTAS script with partitions and the select clause takes the parameters dynamically from parameter table.
I have created a sql string and iam using execute immediate to execute the dyanamic sql.
The problem is since the sql string is huge, iam splitting and concatenating the sql_string like this
v_cmd1 || v_cmd2 || v_cmd3 || v_cmd4 etc..
But after a point it says, string buffer exceeded.
Is there a way to execute as clob or varchar2s , not sure but it is only a ctas.
Can you please help?
Thanks in advance

Pls find the portion of sql.
CREATE TABLE SAS_STAGE.SAS_CLM
SAS_CLM_ID NUMBER(38) NOT NULL ,
SAS_CLM_PROD_IND DATE NOT NULL ,
RUN_DT DATE NULL ,
LINK_NUM NUMBER(38) NULL ,
HSE_B_SEQ VARCHAR2(2 BYTE) NULL ,
NCH_CLM_TYPE_CD VARCHAR2(2 BYTE) NULL ,
BENE_CLM_NUM_EQ VARCHAR2(12 BYTE) NULL ,
HSE_CLM_FROM DATE NULL ,
HSE_CLM_THRU DATE NULL ,
HCFA_CLM_PROC DATE NULL ,
FIRST_EXPNS DATE NULL ,
LAST_EXPNS DATE NULL ,
CARR_CLM_RCPT DATE NULL ,
FICARR_IDENT_NBR VARCHAR2(5 BYTE) NULL ,
CARR_CLM_CNTL_NUM VARCHAR2(15 BYTE) NULL ,
HSE_B_PRVDR_TAX_NUM VARCHAR2(9 BYTE) NULL ,
NPI_PRFRMG VARCHAR2(12 BYTE) NULL ,
DGNS_CD VARCHAR2(5 BYTE) NULL ,
HCPCS_CD VARCHAR2(5 BYTE) NULL ,
HCPCS_INITL_MDFR_CD VARCHAR2(2 BYTE) NULL ,
HCPCS_2ND_MDFR_CD VARCHAR2(2 BYTE) NULL ,
HSE_CWFB_CLM_PMT_DNL_CD VARCHAR2(1 BYTE) NULL ,
LINE_PRCSG_IND_CD VARCHAR2(1 BYTE) NULL ,
CWFB_ALOW_CHRG_AMT NUMBER(30,2) NULL ,
BENE_BIRTH_DT DATE NULL ,
BENE_SEX_IDENT_CD VARCHAR2(1 BYTE) NULL ,
FINL_CLM_TYPE_CD VARCHAR2(20 BYTE) NULL ,
HSE_B_PLC_SRVC_CD VARCHAR2(2 BYTE) NULL ,
PTB_CLM_ID NUMBER(38) CONSTRAINT SYS_C00210166 NOT NULL ,
CARR_LINE_PRCNG_LCLTY_CD VARCHAR2(2 BYTE) NULL ,
BENE_CLM_NUM_CURR VARCHAR2(12 BYTE) NULL ,
MC_XREF_OTPT_FIL_NAME VARCHAR2(30 BYTE) NOT NULL ,
PARTN_NAME VARCHAR2(30) NOT NULL
TABLESPACE SAS_STAGE_DATA
NOLOGGING
PCTFREE 5
COMPRESS
PARTITION BY RANGE (SAS_CLM_PROD_IND)
SUBPARTITION BY LIST (PARTN_NAME)
PARTITION MEDA2007 VALUES LESS THAN ('12/31/2007')
TABLESPACE SAS_STAGE_DATA
SUBPARTITION MEDA2007_SPLIT_PRNT VALUES ('SPLIT_PRNT'),
SUBPARTITION MEDA2007_SPLIT_CHLD VALUES ('SPLIT_CHLD'),
SUBPARTITION MEDA2007_C04_07Select_IP_ER VALUES ('C04_07Select_IP_ER'),
SUBPARTITION MEDA2007_C24_Measures_Groups VALUES ('C24_Measures_Groups'),
SUBPARTITION MEDA2007_C13_14Other_Proc VALUES ('C13_14Other_Proc'),
subpartition MEDA2007_C04 values('C04_07Select_Enctr'),
subpartition MEDA2007_C05 values('C04_07Select_IP_ER'),
subpartition MEDA2007_C06 values('C04_07Select_NH_HH'),
subpartition MEDA2007_C07 values('C04_07Select_Other'),
subpartition MEDA2007_C13 values('C13_14Other_Proc'),
subpartition MEDA2007_C21 values('C21Select_QDC'),
subpartition MEDA2007_C23 values('C23Select_HCPCS'),
subpartition MEDA2007_C24 values('C24_Measures_Groups')
subpartition MEDA2007_DGNS_C01 values('DGNS_C01Cancer_Care'),
subpartition MEDA2007_DGNS_C02 values('DGNS_C02ENT'),
subpartition MEDA2007_DGNS_C03 values('DGNS_C03Other_Dgns'),
subpartition MEDA2007_DGNS_C08 values('DGNS_C08Cardiac_Care'),
subpartition MEDA2007_DGNS_C09 values('DGNS_C09Hepatitus'),
subpartition MEDA2007_DGNS_C10 values('DGNS_C10Repiratory_Care'),
subpartition MEDA2007_DGNS_C11 values('DGNS_C11Renal'),
subpartition MEDA2007_DGNS_C12 values('DGNS_C12Stroke'),
subpartition MEDA2007_DGNS_C14 values('DGNS_C14Osteoporosis'),
subpartition MEDA2007_DGNS_C15 values('DGNS_C15Depression'),
subpartition MEDA2007_DGNS_C16 values('DGNS_C16Diabetes'),
subpartition MEDA2007_DGNS_C17 values('DGNS_C17ER_Care'),
subpartition MEDA2007_DGNS_C18 values('DGNS_C18Eye_Care'),
subpartition MEDA2007_DGNS_C19 values('DGNS_C19Incontinence'),
subpartition MEDA2007_DGNS_C20 values('DGNS_C20Prostate_Cancer')
PARTITION MEDA2008 VALUES LESS THAN ('12/31/2008')
TABLESPACE SAS_STAGE_DATA
SUBPARTITION MEDA2008_SPLIT_PRNT VALUES ('SPLIT_PRNT'),
SUBPARTITION MEDA2008_SPLIT_CHLD VALUES ('SPLIT_CHLD'),
SUBPARTITION MEDA2008_C04_07Select_IP_ER VALUES ('C04_07Select_IP_ER'),
SUBPARTITION MEDA2008_C24_Measures_Groups VALUES ('C24_Measures_Groups'),
SUBPARTITION MEDA2008_C13_14Other_Proc VALUES ('C13_14Other_Proc')
subpartition MEDA2008_C04 values('C04_07Select_Enctr'),
subpartition MEDA2008_C05 values('C04_07Select_IP_ER'),
subpartition MEDA2008_C06 values('C04_07Select_NH_HH'),
subpartition MEDA2008_C07 values('C04_07Select_Other'),
subpartition MEDA2008_C13 values('C13_14Other_Proc'),
subpartition MEDA2008_C21 values('C21Select_QDC'),
subpartition MEDA2008_C23 values('C23Select_HCPCS'),
subpartition MEDA2008_C24 values('C24_Measures_Groups')
subpartition MEDA2008_DGNS_C01 values('DGNS_C01Cancer_Care'),
subpartition MEDA2008_DGNS_C02 values('DGNS_C02ENT'),
subpartition MEDA2008_DGNS_C03 values('DGNS_C03Other_Dgns'),
subpartition MEDA2008_DGNS_C08 values('DGNS_C08Cardiac_Care'),
subpartition MEDA2008_DGNS_C09 values('DGNS_C09Hepatitus'),
subpartition MEDA2008_DGNS_C10 values('DGNS_C10Repiratory_Care'),
subpartition MEDA2008_DGNS_C11 values('DGNS_C11Renal'),
subpartition MEDA2008_DGNS_C12 values('DGNS_C12Stroke'),
subpartition MEDA2008_DGNS_C14 values('DGNS_C14Osteoporosis'),
subpartition MEDA2008_DGNS_C15 values('DGNS_C15Depression'),
subpartition MEDA2008_DGNS_C16 values('DGNS_C16Diabetes'),
subpartition MEDA2008_DGNS_C17 values('DGNS_C17ER_Care'),
subpartition MEDA2008_DGNS_C18 values('DGNS_C18Eye_Care'),
subpartition MEDA2008_DGNS_C19 values('DGNS_C19Incontinence'),
subpartition MEDA2008_DGNS_C20 values('DGNS_C20Prostate_Cancer') ))
AS
WITH
SASTEMP AS
SELECT *
FROM SAS_STAGE.PART_B_FACT_TEMP
SAS_PULL_1 as
select /*+ PUSH_subq parallel(t,8) no_parallel(d) NO_PARALLEL_INDEX(d) */
sas_stage.sas_clm_seq.nextval,
to_date('01/01/2007','mm/dd/yyyy') SAS_CLM_PROD_IND,
t. run_dt,
t. link_num,
t. hse_b_seq,
t. nch_clm_type_cd,
t. bene_clm_num_eq,
t. hse_clm_from,
t. hse_clm_thru,
t. hcfa_clm_proc,
t. first_expns,
t. last_expns,
t. carr_clm_rcpt,
t. ficarr_ident_nbr,
t. carr_clm_cntl_num,
t. hse_b_prvdr_tax_num,
t. npi_prfrmg,
t. dgns_cd,
t. hcpcs_cd,
t. hcpcs_initl_mdfr_cd,
t. hcpcs_2nd_mdfr_cd,
t. hse_cwfb_clm_pmt_dnl_cd,
t. line_prcsg_ind_cd,
t. cwfb_alow_chrg_amt,
t. bene_birth,
t. BENE_SEX_IDENT_CD,
t. FINAL_CLM_TYPE_CD,
t. HSE_B_PLC_SRVC_CD,
t. ptb_clm_id,
t. carr_line_prcng_lclty_cd,
t. bene_clm_num_curr,
'SPLIT_PRNT' part_name
from
sastemp t,
part_b.diagnosis_dimension d
where
t.first_expns_dt between parm_expns_start and parm_expns_end
t.last_expns_dt between parm_expns_start and parm_expns_end
AND
( t.LINE_DGNS_ID = d.DGNS_ID
or t.dgns_seq_1_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_2_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_3_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_4_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_5_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_6_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_7_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_8_id = d.DGNS_ID -- added by RJ FR-4, FR-5
and exists
(select /*+ index(t2 IX_MEDA8_REJOIN_SPLIT_CLMS_2) NO_PARALLEL_INDEX(t2) no_parallel(t2) */
t2.LINK_NUM
from sas_stage.sas_rejoin_split_clm t2
where t.run_dt = t2.prnt_run_dt13
and t.part_b_link_num = t2.prnt_link_num13 )
SAS_PULL_2 as
select /*+ PUSH_subq parallel(t,8) no_parallel(d) NO_PARALLEL_INDEX(d) */
sas_stage.sas_clm_seq.nextval,
to_date('01/01/2007','mm/dd/yyyy') SAS_CLM_PROD_IND,
t. run_dt,
t. link_num,
t. hse_b_seq,
t. nch_clm_type_cd,
t. bene_clm_num_eq,
t. hse_clm_from,
t. hse_clm_thru,
t. hcfa_clm_proc,
t. first_expns,
t. last_expns,
t. carr_clm_rcpt,
t. ficarr_ident_nbr,
t. carr_clm_cntl_num,
t. hse_b_prvdr_tax_num,
t. npi_prfrmg,
t. dgns_cd,
t. hcpcs_cd,
t. hcpcs_initl_mdfr_cd,
t. hcpcs_2nd_mdfr_cd,
t. hse_cwfb_clm_pmt_dnl_cd,
t. line_prcsg_ind_cd,
t. cwfb_alow_chrg_amt,
t. bene_birth,
t. BENE_SEX_IDENT_CD,
t. FINAL_CLM_TYPE_CD,
t. HSE_B_PLC_SRVC_CD,
t. ptb_clm_id,
t. carr_line_prcng_lclty_cd,
t. bene_clm_num_curr,
'SPLT_CHLD' part_name
from sastemp t,
part_b.diagnosis_dimension d
where t.carr_clm_cntl_num = '551107355414810' and '500207330004410' and
t.first_expns_dt between parm_expns_start and parm_expns_end
AND t.last_expns_dt between parm_expns_start and parm_expns_end
AND
( t.LINE_DGNS_ID = d.DGNS_ID
or t.dgns_seq_1_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_2_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_3_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_4_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_5_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_6_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_7_id = d.DGNS_ID -- added by RJ FR-4, FR-5
or t.dgns_seq_8_id = d.DGNS_ID -- added by RJ FR-4, FR-5
and exists
(select /*+ index(t2 IX_MEDA8_REJOIN_SPLIT_CLMS_1) NO_PARALLEL_INDEX(t2) no_parallel(t2) */
t2.LINK_NUM
from sas_rejoin_split_clm t2
where t.run_dt = t2.chld_run_dt
and t.part_b_link_num = t2.chld_link_num )
SAS_ONLY_LN_IPHR as
select /*+ PUSH_subq parallel(t,8) no_parallel(d) NO_PARALLEL_INDEX(d) full(t) */
sas_stage.sas_clm_seq.nextval,
to_date('01/01/2007','mm/dd/yyyy') SAS_CLM_PROD_IND,
t.run_dt,
t. link_num,
t. hse_b_seq,
t. nch_clm_type_cd,
t. bene_clm_num_eq,
t. hse_clm_from,
t. hse_clm_thru,
t. hcfa_clm_proc,
t. first_expns,
t. last_expns,
t. carr_clm_rcpt,
t. ficarr_ident_nbr,
t. carr_clm_cntl_num,
t. hse_b_prvdr_tax_num,
t. npi_prfrmg,
t. dgns_cd,
t. hcpcs_cd,
t. hcpcs_initl_mdfr_cd,
t. hcpcs_2nd_mdfr_cd,
t. hse_cwfb_clm_pmt_dnl_cd,
t. line_prcsg_ind_cd,
t. cwfb_alow_chrg_amt,
t. bene_birth,
t. BENE_SEX_IDENT_CD,
t. FINAL_CLM_TYPE_CD,
t. HSE_B_PLC_SRVC_CD,
t. ptb_clm_id,
t. carr_line_prcng_lclty_cd,
t. bene_clm_num_curr,
'C04_07Select_IP_ER' part_name
from
sastemp t,
part_b.diagnosis_dimension d
where
t.first_expns_dt <= to_date('12312008', 'mmddyyyy')
and t.first_expns_dt >= to_date('11012007', 'mmddyyyy')
and t.last_expns_dt <= to_date('12312008', 'mmddyyyy')
and t.last_expns_dt >= to_date('11012007', 'mmddyyyy')
and t.LINE_DGNS_ID = d.DGNS_ID
and t.HCPCS_cd in
(select h2.mc_xref_clm_clmn_cd
from sas_stage.measure_code_xref h2
where h2.mc_xref_prod_ind = 'MEDA08'
and h2.mc_xref_otpt_fil_name = 'C04_07Select_IP_ER'
and h2.mc_xref_clm_clmn_name in ('HCPCS', 'QDC'))
SAS_ONLY_LN_C24 as
select /*+ PUSH_subq parallel(t,8) no_parallel(d) NO_PARALLEL_INDEX(d) full(t) */
sas_stage.sas_clm_seq.nextval,
to_date('01/01/2007','mm/dd/yyyy') SAS_CLM_PROD_IND,
t.run_dt,
t. link_num,
t. hse_b_seq,
t. nch_clm_type_cd,
t. bene_clm_num_eq,
t. hse_clm_from,
t. hse_clm_thru,
t. hcfa_clm_proc,
t. first_expns,
t. last_expns,
t. carr_clm_rcpt,
t. ficarr_ident_nbr,
t. carr_clm_cntl_num,
t. hse_b_prvdr_tax_num,
t. npi_prfrmg,
t. dgns_cd,
t. hcpcs_cd,
t. hcpcs_initl_mdfr_cd,
t. hcpcs_2nd_mdfr_cd,
t. hse_cwfb_clm_pmt_dnl_cd,
t. line_prcsg_ind_cd,
t. cwfb_alow_chrg_amt,
t. bene_birth,
t. BENE_SEX_IDENT_CD,
t. FINAL_CLM_TYPE_CD,
t. HSE_B_PLC_SRVC_CD,
t. ptb_clm_id,
t. carr_line_prcng_lclty_cd,
t. bene_clm_num_curr,
'C24_Measures_Groups' part_name
from
sas_temp t,
part_b.diagnosis_dimension d
where
and t.first_expns_dt <= to_date('12312008', 'mmddyyyy')
and t.first_expns_dt >= to_date('07012008', 'mmddyyyy')
and t.last_expns_dt <= to_date('12312008', 'mmddyyyy')
and t.last_expns_dt >= to_date('07012008', 'mmddyyyy')
and t.LINE_DGNS_ID = d.DGNS_ID
and t.HCPCS_cd in
(select h2.mc_xref_clm_clmn_cd
from sas_stage.measure_code_xref h2
where h2.mc_xref_prod_ind = 'MEDA08'
and h2.mc_xref_otpt_fil_name = 'C24_Measures_Groups'
and h2.mc_xref_clm_clmn_name in ('HCPCS', 'QDC')
It runs further for 10 more pages.

Similar Messages

  • Dynamic SQL Query to Find Special Characters in Table columns

    Hi,
    I am new to OTN FORUMS.
    I am trying to find the columnsi of a table which have special characters in them.
    I am planning on using this query
    select ' select INSTR('||column_name||', chr(0))
    from '||table_name||'where INSTR('||column_name||', chr(0)) >0' from user_tab_columns
    where table_name='Account'
    and spool the output to run as a script.
    Is this the right way or do u suggest any modifications to the query?
    Thanks in advance.

    Hi,
    I think your basic approach is right. Since you can't hard-code the table- or column names into the query, you'll need dynamic SQL.
    Instead SQL-from-SQL (that is, writing a pure SQL query, whose output is SQL code), you could do the whole job in PL/SQL, but I don't see any huge advantage either way.
    When you say "Special character<b>s</b>", do you really mean "one given special character" (in this case, CHR(0))?
    Will you ever want to search for multiple special characters at once?
    What if table foo has a column bar, and in 1000 rows of foo, bar contains CHR (0). Do you want 1000 rows of output, each showing the exact position of the first CHR(0)? If the purpose is to look at theese rows later, shouldn't you include the primary key in the output? What if CHR(0) occurs 2 or more times in the same string?
    If you'd rather have one row of output, that simply says that the column foo.bar sometimes contains a CHR(0), then you could do something like this:
    SELECT     'foo',     'bar'
    FROM     dual
    WHERE     EXISTS (
                SELECT  NULL
                FROM       foo
                WHERE       INSTR ( bar
                            , CHR (0)
                        ) > 0
                );

  • NVARCHAR (MAX) TO PRINT DYNAMIC SQL QUERY TO A SQL FILE

    Hi 
    I have a requirement where i need to write an SP which would construct a huge query using dynamic SQL and save the Dynamic query to a file. 
    The Dynamic SQL Variable I am using as @NVARCHAR(MAX) but since the query being built is large (>4000 characters), I am not able to select it into a table. PRINT @SQL prints the whole query but SELECT @SQL prints only upto 4000 characterrs. 
    And I need to save this huge dynamix sql to a file using the SP.
    Any thoughts as to how i can achieve this?
    Rajiv

    This is a know problem with dynamic SQL nvarchar(max) concatenation.
    See below for correct assembly of large SQL strings:
    CREATE table #temp(qry nvarchar(max));
    declare @cmd nvarchar(max);
    SELECT @cmd=CONCAT(CONVERT(nvarchar(max),N'SELECT '),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),REPLICATE (N'A',4000)),
    CONVERT(nvarchar(max),N'FROM SYS.TABLES'));
    insert into #temp SELECT @cmd;
    select * from #temp;
    select len(qry), datalength(qry) from #temp;
    -- 16022 32044
    drop table #temp;
    Dynamic SQL:  http://www.sqlusa.com/bestpractices/dynamicsql/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Design & Programming
    New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

  • NULL and dynamic SQL

    If table testrh2 has the following columns and data
    col1 --> NULL
    col2 --> 2
    and table testrh has the following columsn and data
    col1 --> NULL
    How could I write a dynamic SQL statement to join on the nulls? I've written the following block as a starting point.
    declare
    cursor c1 is select col1 from isis.testrh;
    lval varchar2(1000);
    lval2 varchar2(1000);
    begin
    for r1 in c1 loop
    lval := 'select col2 from isis.testrh2 where col1 = '||r1.col1;
    execute immediate lval into lval2;
    dbms_output.put_line(lval2);
    end loop;
    end;

    You can't compare null values with '=' in Oracle SQL.
    Null can only be compared with <column> is null .
    You can see it when you try these two queries:
    select * from dual where null is null;  -- you will see one row
    select * from dual where null=null;  -- you will see no rowsThat's why you have to write something like
    (<column1>=<column1>   or   (<column1> is null and <column2> is null))This should also work with null:
    decode(<column1>,<column2>,1,0)=1By the way, why do you use dynamic sql?
    lval := 'select col2 from isis.testrh2 where col1 = '||r1.col1;
    I think you could replace your two lines ( lval:= ... AND execute immediate) by this:
    begin
      select col2
      into lval
      from isis.testrh2
      where decode(col1,r1.col1,1,0)=1;
      dbms_output.put_line('lval='||lval);
    exception
    when no_data_found then
      dbms_output.put_line('no data found'); -- or whatever you want
    end;Edited by: hartmutm on 02.10.2010 23:54

  • How to rename C00n generic column names in a Dynamic SQL report

    I have a an interface whereby the user can select 1 to 60 (upper limit) columns out of 90 possible columns (using a shuttle) from one table before running a report.
    To construct the SQL that will eventually execute, I'm using a "PLSQL function body returning SQL query" with dynamic SQL. The only problem is that I have to use "Generic Column Names" option to be able to compile the code and end up with c001 to c060 as the column names.
    How can I use the names of the selected columns as the report headings rather than c001, C002... etc?
    I do not know beforehand which columns, or how many columns or the order of the selected columns.
    I know Denes K has a demo called Pick Columns but I can't access his code. I have a hunch though that he may be just using conditions to hide/show the apropriate columns.
    thanks in advance
    PaulP

    Hi Paul
    I would change the Heading Type in the Report Details screen to PLSQL and use the shuttle item to return the column values. e.g.
    RETURN :p1_shuttle;
    I'm assuming the shuttle already has a colon separated list of the column headings in the correct order?
    I hope that helps
    Shunt

  • Unique id in dynamic sql

    hi all,
    I am using oracle 10g version.
    I have one table but i do not have any unique id to identify the row.
    I want to have a unique id temporarily in my select statement (dynamic sql) in a stored procedure while fetching the results, so that i can return my results along with the unique id through the cursor.
    Please help me...
    Thanks in advance to all...

    it depends if you have a more than one column that you can uniquely identify and put them together by concatenating you can have a unique id. or a rowid might help you to temporarily identify a row.
    SQL> select e.* from employee e;
    FNAME           MINIT LNAME           SSN       BDATE       ADDRESS                        SEX       SALARY SUPERSSN         DNO
    John            B     Smith           123456789 09-Jan-1965 731 fONDREN, hOUSTON, TX       M       30000.00 333445555          5
    Frankin         T     Wong            333445555 08-Dec-1955 683 Voss, Houston,Tx           M       40000.00 888665555          5
    Alicia          J     Zelaya          999887777 19-Jul-1968 3321Castle, Spring, TX         F       25000.00 987654321          4
    Jennifer        S     Wallace         987654321 20-Jun-2041 291 Berry, Bellaire, TX        F       43000.00 888665555          4
    Ramesh          K     Narayan         666884444 15-Sep-1962 975 Fire Oak, Humble, TX       F       38000.00 333445555          5
    Joyce           A     English         453453453 31-Jul-1972 5631 Rice,Houston,TX           F       25000.00 333445555          5
    Ahmad           V     Jabbar          987987987 29-Mar-1969 980 Dallas,Houston, TX         M       25000.00 987654321          4
    James           E     Borg            888665555 10-Nov-2037 450 Stone, Houston, TX         M       55000.00                    1
    8 rows selected
    SQL> select rowid, e.* from employee e;
    ROWID              FNAME           MINIT LNAME           SSN       BDATE       ADDRESS                        SEX       SALARY SUPERSSN         DNO
    AAD8pbAAJAAAJ4fAAA John            B     Smith           123456789 09-Jan-1965 731 fONDREN, hOUSTON, TX       M       30000.00 333445555          5
    AAD8pbAAJAAAJ4fAAB Frankin         T     Wong            333445555 08-Dec-1955 683 Voss, Houston,Tx           M       40000.00 888665555          5
    AAD8pbAAJAAAJ4fAAC Alicia          J     Zelaya          999887777 19-Jul-1968 3321Castle, Spring, TX         F       25000.00 987654321          4
    AAD8pbAAJAAAJ4fAAD Jennifer        S     Wallace         987654321 20-Jun-2041 291 Berry, Bellaire, TX        F       43000.00 888665555          4
    AAD8pbAAJAAAJ4fAAE Ramesh          K     Narayan         666884444 15-Sep-1962 975 Fire Oak, Humble, TX       F       38000.00 333445555          5
    AAD8pbAAJAAAJ4fAAF Joyce           A     English         453453453 31-Jul-1972 5631 Rice,Houston,TX           F       25000.00 333445555          5
    AAD8pbAAJAAAJ4fAAG Ahmad           V     Jabbar          987987987 29-Mar-1969 980 Dallas,Houston, TX         M       25000.00 987654321          4
    AAD8pbAAJAAAJ4fAAH James           E     Borg            888665555 10-Nov-2037 450 Stone, Houston, TX         M       55000.00                    1
    8 rows selected
    SQL>

  • Can we use Dynamic SQL in Oracle Reports ?

    Hi ,
    Can we use Dynamic SQL in Oracle Reports ?
    If yes please give some examples .
    Thanx
    srini

    I believe the built-in package SRW.Do_Sql is what you are looking for
    Example from the document:
    /* Suppose you want to create a "table of contents" by getting the
    ** first character of a columns value, and page number on which its
    ** field fires to print. Assume that you want to put the "table of
    contents"
    ** into a table named SHIP. You could write the following construct:
    DECLARE
    PAGE_NO NUMBER;
    PAGE_FOR INDEX NUMBER;
    SORT_CHAR CHAR(1);
    CMD_LINE CHAR(200);
    BEGIN
    SORT_CHAR := :SORT_NAME ;
    IF :CALLED = Y THEN
         SRW.GET_PAGE_NUM(PAGE_FOR_INDEX);
         SRW.USER_EXIT(RWECOP PAGE_FOR_INDEX
         P_START_PAGENO);
         SRW.MESSAGE(2,TO_CHAR(:P_START_PAGENO));
    END IF;
    SRW.GET_PAGE_NUM(PAGE_NO);
    CMD_LINE := INSERT INTO SHIP VALUES
                          (||SORT_CHAR||,||TO_CHAR(PAGE_NO)||);
    SRW.MESSAGE(2,CMD_LINE);
    SRW.DO_SQL(CMD_LINE);
    COMMIT;
    EXCEPTION
      WHEN DUP_VAL_ON_INDEX THEN
            NULL;
      WHEN SRW.DO_SQL_FAILURE THEN
            SRW.MESSAGE(1,FAILED TO INSERT ROW INTO SHIP TABLE);
      WHEN OTHERS THEN
           COMMIT;
    END;

  • How to mention numbers in array while creating dynamic sql

    Hi,
    I am creating a dynamic sql and in that I am selecting some rows from the table and storing it into the array.
    I am firing this sql statement within trigger.
    But i got the error:
    ORA-06502: PL/SQL: numeric or value error: character to number conversion error
    I took the same sql statement (the one which was created dynamically) in the procedure and tried to run it but it gave me the same error.
    =========================================================
    Begin
    type NewValueArray is table of varchar2(4000);
    nVal_Array NewValueArray;
    Begin
    Select
    DISCOVERYHOSTNAME,
    DISCOVERYMACPRIMARYPHYSICAL,
    DISCOVERYSERIALNUMBER
    into
    nVal_Array(1),
    nVal_Array(2),
    nVal_Array(3)
    from ALT_42_Consolidated
    where rowid = 'AAEWNEABXAAAAkxAAA';
    EXCEPTION
    WHEN others THEN
    Raise;
    End;
    =========================================================
    I tried using to_number but still the same error.
    Can some body please inform me where I am goofing.
    Thanks!

    I got a different error, but you need to initialise your nval_array and make sure it can hold three values. The following should work:
    declare
      type NewValueArray is table of varchar2(4000);
      nVal_Array NewValueArray := NewValueArray();
    Begin
      nval_array.extend(3);
      Select col1, col2, col3
      into nVal_Array(1), nVal_Array(2), nVal_Array(3)
      from tab1
      where rowid = 'AAAGJZAAGAAAAAWAAV';
    EXCEPTION
      WHEN others THEN
        Raise;
    End;
    /

  • Dynamic SQL and MERGE

    Hi all,
    I'm under 10G r1
    I want to execute a dynamic SQL with merge in it
    when I try with insert ,update it works
    but I just want to use merge if possible
    this is the code
    CREATE OR REPLACE
    PROCEDURE I_COLUMN_CDS_FIXINGS
    v_ticker VARCHAR2,
    v_date DATE,
    v_tenor VARCHAR2,
    v_bid NUMBER,
    v_ask NUMBER)
    AS
    l_column VARCHAR2(80);
    v_column_bid VARCHAR2(80);
    v_column_ask VARCHAR2(80);
    v_source VARCHAR2(60) :='CMAN@BBG';
    v_gen_order pls_integer;
    BEGIN
    SELECT 'CDS_SPREAD_'
    ||v_TENOR
    ||'_BID'
    INTO v_column_bid
    FROM dual;
    SELECT 'CDS_SPREAD_'
    ||v_TENOR
    ||'_ASK'
    INTO v_column_ask
    FROM dual;
    dbms_output.put_line(v_ticker||' '||v_column_ask||' '||v_column_bid||' '||v_bid|| ' '||v_ask||' '||v_date);
    EXECUTE immediate 'MERGE into CDS_FIXINGS C
    using ( select v_ticker,'
    ||v_column_bid
    ||','
    ||v_column_ask
    ||',trunc(v_date) info_date ,v_bid,v_ask from dual ) B
    on (C.cds_ticker=b.v_ticker and c.info_date=b.info_date)
    WHEN MATCHED THEN
    update set '||v_column_bid||'=:1,'||v_column_ask||'=:2 using v_bid,v_ask
    WHEN NOT MATCHED THEN
    INSERT
    CDS_TICKER ,
    INFO_DATE ,
    '||v_column_bid||' ,
    '||v_column_ask||' ,
    SOURCE
    VALUES (:1, :2,:3,:4,:5) ' using v_ticker,
    v_date ,
    v_bid ,
    v_ask ,
    v_source
    ---EXCEPTION
    ---when others then raise;
    end;
    when I launch hte procedure
    exec i_column_cds_fixings('TEST',trunc(sysdate-1),'8Y',4232.01,4234.02);
    I get
    ERROR at line 1:
    ORA-00933: SQL command not properly ended
    ORA-06512: at "OWNER_HISTO.I_COLUMN_CDS_FIXINGS", line 26
    ORA-06512: at line 1
    How can I deal with this
    Thanks
    babata
    I get

    Sorry
    this is formatted one
    REATE OR REPLACE PROCEDURE i_Column_cds_FixIngs
    (v_Ticker VARCHAR2,
    v_Date DATE,
    v_Tenor VARCHAR2,
    v_Bid NUMBER,
    v_Ask NUMBER)
    AS
    l_Column VARCHAR2(80);
    v_Column_Bid VARCHAR2(80);
    v_Column_Ask VARCHAR2(80);
    v_Source VARCHAR2(60) := 'CMAN@BBG';
    v_gen_Order PLS_INTEGER;
    BEGIN
    SELECT 'CDS_SPREAD_'
    ||v_Tenor
    ||'_BID'
    INTO v_Column_Bid
    FROM Dual;
    SELECT 'CDS_SPREAD_'
    ||v_Tenor
    ||'_ASK'
    INTO v_Column_Ask
    FROM Dual;
    dbms_Output.Put_Line(v_Ticker
    ||' '
    ||v_Column_Ask
    ||' '
    ||v_Column_Bid
    ||' '
    ||v_Bid
    ||' '
    ||v_Ask
    ||' '
    ||v_Date);
    EXECUTE IMMEDIATE 'MERGE into CDS_FIXINGS C
    using ( select v_ticker,'
    ||v_Column_Bid
    ||','
    ||v_Column_Ask
    ||',trunc(v_date) info_date ,v_bid,v_ask from dual ) B
    on (C.cds_ticker=b.v_ticker and c.info_date=b.info_date)
    WHEN MATCHED THEN
    update set '
    ||v_Column_Bid
    ||'=:1,'
    ||v_Column_Ask
    ||'=:2 USING v_bid,v_ask
    WHEN NOT MATCHED THEN
    INSERT
    CDS_TICKER ,
    INFO_DATE ,
    ||v_Column_Bid
    ||' ,
    ||v_Column_Ask
    ||' ,
    SOURCE
    VALUES
    :1,
    :2,
    :3,
    :4,
    :5
    USING v_Ticker,v_Date,v_Bid,v_Ask,v_Source;
    ---EXCEPTION
    ---when others then raise;
    END;
    /

  • Dynamic sql and cursors

    We are running an oracle sql procedure that uses a LOT of dynamic sql. We are using a 3rd party package (SQR) as a sort of shell to run the sql procedure. The 3rd party package passes to us an oracle error. This error says, in effect, that there are no inactive database cursors available and that the sql program is too large to process. We conclude from this that we must increase one or more of the cursor parameters in init.ora (v$parameters). Is this the correct assumption? If not, does anyone know what we can do? We'd prefer not to break up the sql procedure into smaller pieces.

    increase the parameter for open cursors.
    check, wether all cursors in your programs are closed in time, or if you are using ref cursors from front-ends (e.g. Java JDBC) that this front-ends close these ref cursors , too.
    If you want to decrease the size of procedures get rid of comments, superfluos spaces, tabs, etc.
    keep a commented version outside vor documentation purposes.
    Hope thsi helps

  • Dynamic SQL and 255 bytes per line limit

    I would like to create a Dynamic SQL statement that is longer than 255 bytes. What do I need to do to embed a carriage return or something so that the resultant string is broken up into multiple lines?

    Please ignore. This was a stupid question!

  • Dynamic SQL and GRANT CREATE ANY TABLE

    hi gurus,
    i have a dynamic SQL in a procedure where a table will be created from an existing table without data.
    strSQL:='create table ' || strTemp || ' as select * from ' || strArc || ' where 1=2';
    execute immediate strSQL;
    without GRANT CREATE ANY TABLE for the user, *"ORA-01031: insufficient privileges"* error during execution.
    Is there a way to tackle this issue without providing GRANT CREATE ANY TABLE privilige?
    many thanks,
    Charles

    ravikumar.sv wrote:
    The problem is not because of dynamic sql...It probably has something to do with dynamic SQL or, more accurately, dynamic SQL within a stored procedure.
    From a SQL*Plus command prompt, you can create a table if your account has the CREATE TABLE privilege either granted directly to it or granted to a role that has been granted to your account. Most people probably have the CREATE TABLE privilege through a role (hopefully a custom "developer role" that has whatever privileges you grant to users that will own objects but potentially through the default RESOURCE role). That is not sufficient to create tables dynamically via a definer's rights stored procedure. Only privileges that are granted directly to the user, not those granted via a role, are visible in that case.
    I expect that the DBAs are granting the CREATE ANY TABLE privilege directly to the account in question rather than through whatever role(s) are being used which is why that appears to solve the problem.
    Justin

  • Dynamic SQL and IN CLAUSE from Pro C code

    Hi Guys,
    Tyring to embed sql in Pro C. Here I don't know in hand how many items will be there in the IN Clause of my dynamic sql. Tried this with a loop and then adding actual values to the stement and then executing it. This worked but as this hard coding makes it literal sql and hence hampers performance. Can any one help me with how to put bind variables where we don't know how many of them will be there in a dynamic sql.
    Thanks,

    Dynamic SQL supports user defined types, try passing a collection and using TABLE(CAST(collection)) in the SQL statement.
    In the current approach (creating IN clause at runtime) keep in mind that in a IN clause you can put a maximum of 255 elements...
    Max

  • Dynamic SQL and use of aggregate functions

    Hello Forum members,
    I'm trying to create dynamic SQL in a function module and return the MAX value of a field. 
    I am passing in a parameter called CREATE_FIELD_NAME, and my SQL is
    SELECT MAX(CREATE_FIELD_NAME) INTO (CREATE_DATE) FROM (TABLE_NAME).
    I also tried:
    SELECT MAX((CREATE_FIELD_NAME)) INTO (CREATE_DATE) FROM (TABLE_NAME).
    But abap is not recognizing my variable as a variable in either case, but rather, as a field name.
    Anyone know a way around this?
    Thanks in advance,
    Jeff
    Here is my program:
    FUNCTION ZJLSTEST4.
    ""Local Interface:
    *"  IMPORTING
    *"     VALUE(TABLE_NAME) LIKE  MAKT-MAKTX
    *"     VALUE(CREATE_FIELD_NAME) LIKE  MAKT-MAKTX
    *"     VALUE(UPDATE_FIELD_NAME) LIKE  MAKT-MAKTX
    *"  EXPORTING
    *"     VALUE(MAX_DATE) LIKE  SY-DATUM
    DATA: CREATE_DATE LIKE MCHA-ERSDA VALUE '19000101',
          UPDATE_DATE LIKE MCHA-LAEDA VALUE '19000101'.
    SELECT MAX(CREATE_FIELD_NAME) INTO (CREATE_DATE) FROM (TABLE_NAME).
    ENDSELECT.
    *SELECT MAX((UPDATE_FIELD_NAME)) INTO (UPDATE_DATE) FROM (TABLE_NAME).
    *ENDSELECT.
    IF CREATE_DATE > UPDATE_DATE.
       MAX_DATE = CREATE_DATE.
    ELSE.
       MAX_DATE = UPDATE_DATE.
    ENDIF.
    IF MAX_DATE = '19000101'.
    MAX_DATE = SY-DATUM.
    ENDIF.
    ENDFUNCTION.

    Max is right, you need the spaces, as in my example.
    You might like to try this:
    data: l_CREATE_FIELD_NAME) LIKE MAKT-MAKTX,
          l_UPDATE_FIELD_NAME) LIKE MAKT-MAKTX.
    DATA: CREATE_DATE LIKE MCHA-ERSDA VALUE '19000101',
    UPDATE_DATE LIKE MCHA-LAEDA VALUE '19000101'.
    concatenate 'MAX(' create_name ')' into l_create_name separated by space.
    concatenate 'MAX(' update_name ')' into l_update_name separated by space.
    SELECT SINGLE (l_CREATE_FIELD_NAME)
    INTO CREATE_DATE FROM (table_name).
    SELECT SINGLE (l_updATE_FIELD_NAME)
    INTO updATE_DATE FROM (table_name).
    IF CREATE_DATE > UPDATE_DATE.
    MAX_DATE = CREATE_DATE.
    ELSE.
    MAX_DATE = UPDATE_DATE.
    ENDIF.
    IF MAX_DATE = '19000101'.
    MAX_DATE = SY-DATUM.
    ENDIF.
    If it still fails please post the latest abap code for us to check.

  • Dynamic SQL and PL/SQL Gateway

    This question is kind of out of curiosity...I had created a procedure that used some dynamic sql (execute immediate), and was trying to use it on pl/sql gateway. I kept getting page not found errors until I removed the execute immediate statement, and reverted to using static sql statements.
    I am just curious, is dynamic sql not supported at all with pl/sql gateway?
    Thanks
    Kevin

    > Relax damorgan, no need to be condescending. Of course I read the docs ..
    Well, you're one of the few that actually read the docs.. And one of many that lacked to state any real technical details for forum members to understand the actual problem, the actual error, and what the environment is that this is happening in.
    Remember that you came to this forum for forum members to help you. In order for us to do that, you need to help us understand
    - your problem
    - your environment
    - what you have tried
    What PL/SQL Gateway do you refer to? Thus is an old term for an old product - today in Oracle there are two "gateways" into the PL/SQL engine via HTTP. Via Apache/mod_plsql and via the internal Java servlet web engine called EPG inside Oracle.
    As for what the "Gateway" access to the PL/SQL engine via HTTP.. whether it supports EXECUTE IMMEDIATE or not is like asking if a car "supports" soft drinks or not (just because a human that may consume soft drinks acts as the driver of the car). Not sensible or relevant at all.
    mod_plsql creates an Oracle session to the database instance, and executes a PL/SQL procedure in the database. This is no different from any other client connection to Oracle. Oracle has no clue that the client is mod_plsql and not TOAD or Java or VB or PHP or Perl or whatever else.
    So how can this support or not support the EXECUTE IMMEDIATE command? Does PL/SQL support EXECUTE IMMEDIATE? Well duh...
    Why do you get a generic 404? Because the PL/SQL call made by mod_plsql failed with an unhandled exception. mod_plsql gets that exception and now what? Was a valid HTP buffer created for it to stream to the web browser? If the buffer perhaps partially completed? All that mod_plsql knows is that it asked for a HTP buffer via that PL/SQL call and it got an exception in return.
    A 404 HTTP error is the only reasonable and logical response for it to pass to the web browser in this case.
    PS. to see why mod_plsql fail, refer to the access_log and error_log of that Apache httpd server

Maybe you are looking for