Copy hiearchical data using SQL only.

Is there anyway to copy hiearchical data that use ID's without using a pl/sql loop procedure or a global temporary table?
Example
CREATE TABLE DUCK_TREE
FIRST_NAME VARCHAR2(100 BYTE),
LAST_NAME VARCHAR2(100 BYTE),
FAMILY_ID NUMBER,
PARENT_ID NUMBER
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Louie', 'Duck', 207, 203);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Dewey', 'Duck', 206, 203);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Huey', 'Duck', 205, 203);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Donald', 'Duck', 204, 201);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Thelma', 'Duck', 203, 201);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Daphne', 'Duck', 202, 200);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Quackmore', 'Duck', 201, 200);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Grandma', 'Duck', 200, NULL);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Hortense', 'McDuck', 103, 100);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Scrooge', 'McDuck', 102, 100);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Matilda', 'McDuck', 101, 100);
INSERT INTO DUCK_TREE ( FIRST_NAME, LAST_NAME, FAMILY_ID, PARENT_ID ) VALUES (
'Scotty', 'McDuck', 100, NULL);
COMMIT;
Now you would like to copy the hierachical data starting say at Grandma and all children?

I can't really see a way to get away from iterative PL/SQL or the use of temporary tables (global or otherwise).
The best solution I've managed to come up with so far is use at least 1 GTT to store a mapping of current FAMILY_IDs to new FAMILY_IDs, then use the map table to translate the old IDs to new IDs during the copy phase.
DROP TABLE id_map;
CREATE global TEMPORARY TABLE ID_MAP
    (   old_id NUMBER,
        new_id NUMBER
    ON COMMIT DELETE ROWS;
DROP SEQUENCE duck_tree_seq;
CREATE SEQUENCE duck_tree_seq INCREMENT BY 1
    START WITH 208;
create or replace procedure ADD_DUCK_WITH_DESCENDENTS
      (fName varchar2, lName varchar2, pid number, RootID number) is
    new_id NUMBER;
begin
    -- Create new Ancestor
    insert into duck_tree values (fname, lname, duck_tree_seq.nextval, pid)
    returning family_id into new_id;
    -- Add root mapping
    insert into id_map values (rootid, new_id);
    -- Map lineage
    INSERT INTO id_map
    WITH t AS
         SELECT d.*
           FROM duck_tree d
          WHERE level <> 1 -- exclude root ancestor
        CONNECT BY d.parent_id = PRIOR d.family_id
          START WITH d.first_name = 'Grandma'
     SELECT family_id, DUCK_TREE_SEQ.nextval newid FROM t;
    -- Copy descendents
    insert into duck_tree (
        first_name,
        last_name,
        family_id,
        parent_id)
    WITH t AS
        (SELECT d.*
           FROM duck_tree d
          WHERE level <> 1 -- exclude root ancestor
        CONNECT BY d.parent_id = PRIOR d.family_id
          START WITH d.first_name = 'Grandma'
     SELECT t.first_name
          , t.last_name
          , m1.NEW_ID
          , m2.NEW_ID
       FROM t
    JOIN id_map m1
         ON t.family_id = m1.old_id
    LEFT JOIN id_map m2
         ON t.parent_id = m2.old_id;
    -- Clear out the map table
    delete from id_map;
end;
call add_duck_with_descendents('Grandpa','Duck',null,200);
/

Similar Messages

  • Count rows from multiple tables using SQL only

    Hi, I know this has probably been answered before, but I couldn't find the answer anywhere. Please help.
    I'd like count(*) [rows] for all tables in database using SQL only - no PL/SQL
    The result should be something like:
    Table RowCount
    DBA_TABLES 1000
    DBA_USERS 50
    etc.
    Thanks!

    offcource write this script:
    create or replace procedure count_tables (ip_schema VARCHAR2)
    is
    lv_owner VARCHAR2(100);
    lv_table_name VARCHAR2(100);
    lv_sql_statement VARCHAR2(2000);
    lv_count_table NUMBER;
    CURSOR c1 IS
    SELECT owner, table_name
    FROM all_tables
    WHERE owner = ip_schema
    ORDER BY table_name;
    begin
    dbms_output.put_line ('+--------------------------------------------------------------------+');
    dbms_output.put_line ('¦ | | ¦');
    dbms_output.put_line ('¦ Schema Name | Table Name | Number of Rows ¦');
    dbms_output.put_line ('¦ | | ¦');
    dbms_output.put_line ('¦------------------------------------------------------------------¦');
    OPEN c1;
    LOOP
    FETCH c1 INTO lv_owner , lv_table_name;
    EXIT WHEN c1%NOTFOUND;
    lv_sql_statement := 'SELECT count(*) FROM ' || lv_owner || '.' || lv_table_name;
    EXECUTE IMMEDIATE lv_sql_statement INTO lv_count_table;
    IF lv_count_table > 0 THEN
    dbms_output.put_line ('| '||rpad(lv_owner, 14, ' ')||'| '|| rpad(lv_table_name, 32, ' ')||'| '|| rpad(lv_count_table, 16, ' ')||' |');
    -- dbms_output.put_line ('|---------------|---------------------------------|------------------|');
    END IF;
    END LOOP;
    CLOSE c1;
    dbms_output.put_line ('+--------------------------------------------------------------------+');
    exception
    WHEN OTHERS THEN
    dbms_output.put_line ('owner: '||lv_owner||' - table: '||lv_table_name||' - '||sqlerrm);
    end count_tables;
    set serveroutput on size 1000000
    exec count_tables
    drop procedure count_tables;

  • How to view clob data using sql

    Hi,
    In our database, we have one table that is having one column of CLOB datatype but now i want to view the data using sql select query but it throws error: "Datatype not supported".
    Could any one please let me know how to view the clob data using select query.
    Oracle DB version : 10.2.0.3
    Thanks

    h5.
    use read procedure
    PROCEDURE READ (
    lobsrc IN BFILE|BLOB|CLOB ,
    amount IN OUT BINARY_INTEGER,
    offset IN INTEGER,
    buffer OUT RAW|VARCHAR2 );
    example  Updating LOB by Using DBMS_LOB in PL/SQL
    DECLARE
    lobloc CLOB; -- serves as the LOB locator
    text VARCHAR2(32767):='Resigned: 5 August 2000';
    amount NUMBER ; -- amount to be written
    offset INTEGER; -- where to start writing
    BEGIN
    SELECT resume INTO lobloc
    FROM employees
    WHERE employee_id = 405 FOR UPDATE;
    offset := DBMS_LOB.GETLENGTH(lobloc) + 2;
    amount := length(text);
    DBMS_LOB.WRITE (lobloc, amount, offset, text );
    text := ' Resigned: 30 September 2000';
    SELECT resume INTO lobloc
    FROM employees
    WHERE employee_id = 170 FOR UPDATE;
    amount := length(text);
    DBMS_LOB.WRITEAPPEND(lobloc, amount, text);
    COMMIT;
    END;

  • Can we change the system date using Sql

    can we change the system date using Sql statement..
    can anyone help
    Tariq.

    There is an initialization parameter called FIXED_DATE which lets you set a fixed sysdate for testing purposes, and you can alter that date using the ALTER SYSTEM command, but as implied by the name you are setting a data/time that doesn't change. Since the normal sysdate is derived from the O/S it cannot be altered through SQL

  • How can I validate a date using sql

    How can I validate a date using sql or pl/sql
    select to_date('01/01/2009','mm/dd/yyyy') from dual this is a good date
    but how can I check for a bad date
    select to_date('0a/01/2009','mm/dd/yyyy') from dual
    Howard

    William Robertson wrote:
    It'll be complicated in pure SQL, as you'll have to parse out day, month and year and then validate the day against the month and year bearing in mind the rules for leap years. It would be simpler to write a PL/SQL function and call that.Nah, not that complicated, you just need to generate a calender to validate against.
    SQL> ed
    Wrote file afiedt.buf
      1  with yrs as (select rownum-1 as yr from dual connect by rownum <= 100)
      2      ,mnth as (select rownum as mn, case when rownum in (4,6,9,11) then 30
      3                            when rownum = 2 then 28
      4                       else 31
      5                       end as dy
      6                from dual
      7                connect by rownum <= 12)
      8      ,cent as (select (rownum-1) as cen from dual connect by rownum <= 21)
      9      ,cal as (select cen, yr, mn,
    10                      case when ((yr = 0 and mod(cen,400) = 0)
    11                             or (mod(yr,4) = 0 and yr > 0))
    12                            and mn = 2 then dy+1
    13                      else dy
    14                      end as dy
    15               from cent, yrs, mnth)
    16  --
    17      ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    18  --
    19  select case when cal.cen is null then 'Invalid Date'
    20              when not regexp_like(dt,'^[0-9]{1,2}[\/.-_][0-9]{1,2}[\/.-_][0-9]{4}$') then 'Invalid Date'
    21         else dt
    22         end as dt
    23  from dt left outer join
    24               cal on (to_number(regexp_substr(dt,'[0-9]+')) between 1 and cal.dy
    25                   and to_number(regexp_substr(dt,'[0-9]+',1,2)) = cal.mn
    26                   and floor(to_number(regexp_substr(dt,'[0-9]+',1,3))/100) = cal.cen
    27*                  and to_number(substr(regexp_substr(dt,'[0-9]+',1,3),-2)) = cal.yr)
    SQL> /
    Enter value for date_dd_mm_yyyy: a1/02/2008
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select 'a1/02/2008' as dt from dual)
    DT
    Invalid Date
    SQL> /
    Enter value for date_dd_mm_yyyy: 01/02/2008
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '01/02/2008' as dt from dual)
    DT
    01/02/2008
    SQL> /
    Enter value for date_dd_mm_yyyy: 29/02/2008
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '29/02/2008' as dt from dual)
    DT
    29/02/2008
    SQL> /
    Enter value for date_dd_mm_yyyy: 30/02/2008
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '30/02/2008' as dt from dual)
    DT
    Invalid Date
    SQL> /
    Enter value for date_dd_mm_yyyy: 29/02/2009
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '29/02/2009' as dt from dual)
    DT
    Invalid Date
    SQL> /
    Enter value for date_dd_mm_yyyy: 28/02/2009
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '28/02/2009' as dt from dual)
    DT
    28/02/2009
    SQL> /
    Enter value for date_dd_mm_yyyy: 0a/01/2009
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '0a/01/2009' as dt from dual)
    DT
    Invalid Date
    SQL> /
    Enter value for date_dd_mm_yyyy: 00/01/2009
    old  17:     ,dt as (select '&date_dd_mm_yyyy' as dt from dual)
    new  17:     ,dt as (select '00/01/2009' as dt from dual)
    DT
    Invalid Date
    SQL>

  • Loading data with dates using SQL*Loader

    Dear everyone
    I am currently trying to load some data containing dates using SQL*Loader.
    For termination of fields I have been using ^ because I have some book titles which contain " and ' as part of their title. I found that the TO_DATE function did not seem to work using ^ instead of ". Would I be correct? I think the Oracle manual says that " must be used.
    After some Web research I eventually amended my control file to as follows:
    load data
    infile 'h:\insert_statements\22_insert_into_SCAN_FILE_INFO.txt'
    REPLACE
    into table SCAN_FILE_INFO
    fields terminated by "," optionally enclosed by '^'
    TRAILING NULLCOLS
    (scan_id, scan_filename
    file_format_id
    orig_scanning_resolution_dpi
    scanner_id, scanner_operator_id
    scanning_date "TO_DATE (:scanning_date, 'YYYY-MM-DD')"
    original_map_publication_id
    reprint_publication_id)
    A simple line of data is as follow:
    280001, ^1910 - London^, 270001, 400, 250001, 260001, "TO_DATE('2007-06-06', 'YYYY-MM-DD')", 200019,
    The final column being null.
    However when I attempt that I get the following error message:
    Record 1: Rejected - Error on table SCAN_FILE_INFO, column SCANNING_DATE.
    ORA-01841: (full) year must be between -4713 and +9999, and not be 0
    If I change the scanning_date part to:
    scanning_date "EXPRESSION TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    or
    scanning_date "CONSTANT TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    I get the error message:
    Record 1: Rejected - Error on table SCAN_FILE_INFO, column SCANNING_DATE.
    ORA-00917: missing comma
    As soon as I do the following:
    scanning_date "EXPRESSION, TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    or
    scanning_date "CONSTANT, TO_DATE (:scanning_date, 'YYYY-MM-DD')",
    I get too many values error message:
    Record 1: Rejected - Error on table SCAN_FILE_INFO.
    ORA-00913: too many values
    I also tested out scanning_date DATE "YYYY-MM-DD", but that just gave the same ORA-01841 error message as above.
    I must be doing something very simple which is wrong but I cannot figure it out.
    Kind regards
    Tim

    And why do you have scanning date as "TO_DATE('2007-06-06', 'YYYY-MM-DD')" in your infile? All you need is 2007-06-06. If you can not change infile generation code, use:
    load data
    infile 'h:\insert_statements\22_insert_into_SCAN_FILE_INFO.txt'
    REPLACE
    into table SCAN_FILE_INFO
    fields terminated by "," optionally enclosed by '^'
    TRAILING NULLCOLS
    (scan_id, scan_filename
    file_format_id
    orig_scanning_resolution_dpi
    scanner_id, scanner_operator_id
    scanning_date "TO_DATE(REPLACE(REPLACE(:scanning_date,'TO_DATE('),'''YYYY-MM-DD'')'), 'YYYY-MM-DD')"
    original_map_publication_id
    reprint_publication_id)SY.

  • Get a date using Sql

    hi
    how i can get the following date using sql, ie i want to get '01-APR-2005'
    if the sysdate is between (01/04/2005 - 31/3/2006). in the same way if it is
    01/04/2004 - 31/3/2005 i want to get '01-apr-2004'...in the same way for any year..
    how i can get this...
    thx in adv
    Kris
    Message was edited by:
    Kris

    SQL> select dt, add_months(trunc(add_months(dt, -3), 'yyyy'), 3) new_dt
      2    from t
      3   order by dt;
    DT        NEW_DT
    01-APR-04 01-APR-04
    31-DEC-04 01-APR-04
    01-JAN-05 01-APR-04
    31-MAR-05 01-APR-04
    01-APR-05 01-APR-05
    31-DEC-05 01-APR-05
    01-JAN-06 01-APR-05
    31-MAR-06 01-APR-05
    8 rows selected.
    SQL> select add_months(trunc(add_months(sysdate, -3), 'yyyy'), 3) from dual;
    ADD_MONTH
    01-APR-05

  • Copy the data from Sql Server to edirectory using java

    Hi ,
    I am new to e directory.I don't know how it works.Can some one assist
    me on this ,I have to extract data from SQL Server and update these
    data in edirectory.This is needs to be done using java.
    If any one can provide me the sample code or please suggest how to
    proceed .
    Thanks in advance
    dukewarm
    dukewarm's Profile: http://forums.novell.com/member.php?userid=53430
    View this thread: http://forums.novell.com/showthread.php?t=373051

    dukewarm;1792481 Wrote:
    > Hi ,
    >
    > I am new to e directory.I don't know how it works.Can some one assist
    > me on this ,I have to extract data from SQL Server and update these
    > data in edirectory.This is needs to be done using java.
    >
    > If any one can provide me the sample code or please suggest how to
    > proceed .
    >
    > Thanks in advance
    Read the values from SQL server and use LDAP to update the data in
    eDirectory.
    Thomas
    thsundel
    thsundel's Profile: http://forums.novell.com/member.php?userid=128
    View this thread: http://forums.novell.com/showthread.php?t=373051

  • Filtering data using sql query

    I am pulling data from a database which is a log of events type data. It consists of entries at a certain time, entry text, and even has an entry type. One of those types is known as "open item." This entry has the potential to be present on multiple days over and over. The problem is I only want the "open item" entries to appear that occur on the most recent day of a date range that is input by the user, but also not excluding all other entries from all of the other days that are not "open item." The "is_open=0" is what is excluding the "open items" from all of the entries, but now there needs to be a way to include (ie. "is_open=1") the "open items" on the current day. My query is as follows:
    SELECT     b.log_title, a.log_id, MIN(a.shift_date) AS shift_date,a.shift_id,a.operator_position_descr,a.is_open,a.user_id, MAX(a.annotation_number) AS annotation_number,
                          a.entry_text, MIN(a.entry_time) AS entry_time,a.font_num
    FROM         log_entries AS a INNER JOIN
                          log_list AS b ON a.log_id = b.log_id
    WHERE ((entry_type_code NOT LIKE '%NLOG%'
                AND entry_type_code <> 'daily'
                AND entry_type_code <> 'shift act') or entry_type_code is null)
              AND (is_open = 0  AND is_standing_order = 0 AND is_rolled_forward is null) 
    GROUP BY b.log_title, a.log_id, a.user_id,a.shift_id,a.operator_position_descr,a.is_open, a.entry_text,a.font_num
    Order by entry_time

    Change you WHERE clause to something like this... (You didn't say what kind of DB you were using so I used SQL Server syntax)
    WHERE ((entry_type_code NOT LIKE '%NLOG%'
    AND entry_type_code IN ('daily', 'shift act')) OR entry_type_code is null)
    AND (is_open = 0 OR (is_open = 1 AND a.shift_date >= DateAdd(dd, DateDiff(dd, 0, GetDate()), 0)))
    AND is_standing_order = 0
    AND is_rolled_forward is null
    The biggest change is this... AND (is_open = 0 OR (is_open = 1 AND a.shift_date >= DateAdd(dd, DateDiff(dd, 0, GetDate()), 0))).
    It allows is_open = 1 if the shift_date is =&gt; midnight of the of the current day.
    HTH,
    Jason

  • How to export&import data using sql *loader

    Hi all,
    How to export&import data from sql*loader. Give me the clear steps..
    Thanks in Advance

    Hi did you already exported data from SQL SERVER? if not using SQL*LOADER you cannot export data. SQL*LOADER is only mean for importing data from flat files(usually text files) into ORACLE tables.
    for importing data into oracle tables using sql*loader use below steps
    1) create a sql*loader control file.
    it looks like as follows
    LOAD DATA
    INFILE 'sample.dat'
    BADFILE 'sample.bad'
    DISCARDFILE 'sample.dsc'
    APPEND
    INTO TABLE emp
    TRAILING NULLCOLS
    or for sample script of control file search google.
    2) at command prompt issue following
    $ sqlldr test/test
    enter control file=<give control file name which you create earlier>
    debug any errors (if occured)

  • How to load data using sql to an Oracle database in 9i

    <p>I just started using Hyperion Analytic Admin Services and waswondering if there is a way to load data through a sql call ratherthan a flat file.  Any help is greatly appreciated.</p>

    You need to create a Rules Files: <BR>- right click at Rules Files under Database name / Create Rules File<BR>- File / Open SQL Data Source -> you need to copy and paste your SQL code<BR>- Field / Properties / Data Load Properties -> define each collumn of the result set with an especific dimension.<BR><BR>I hope that it helps you !<BR><BR>Regards<BR><BR>Andrea Crespo<BR>

  • Error in loading data using SQL loader

    I am getting a error like ‘SQL*Loader -350 syntax error of illegal combination of non-alphanumeric characters’ while loading a file using SQL loader in RHEL. The command used to run SQL*Loader is:
    Sqlldr userid=<username>/<password> control =data.ctl
    The control file, data.ctl is :
    LOAD data
    infile '/home/oraprod/data.txt'
    append  into table test
    empid terminated by ',',
    fname terminated by ',',
    lname terminated by ',',
    salary terminated by whitespace
    The data.txt file is:
    1,Kaushal,halani,5000
    2,Chetan,halani,1000
    I hope, my question is clear.
    Please revert with the reply to my query.
    Regards

    Replace ''{" by "(" in your control file
    LOAD data
    infile 'c:\data.txt'
    append  into table emp_t
    empid terminated by ',',
    fname terminated by ',',
    lname terminated by ',',
    salary terminated by whitespace
    C:\>sqlldr user/pwd@database control=c.ctl
    SQL*Loader: Release 10.2.0.3.0 - Production on Wed Nov 13 10:10:24 2013
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    Commit point reached - logical record count 1
    Commit point reached - logical record count 2
    SQL> select * from emp_t;
         EMPID FNAME                LNAME                    SALARY
             1 Kaushal              halani                     5000
             2 Chetan               halani                     1000
    Best regards
    Mohamed Houri

  • ORA-01841 Error when value for date col is NULL in .dat (using SQL Loader)

    Hello Gurus,
    I have some data in .dat file which needs to be loaded into oracle table. I am using SQL * Loader to do the job. Although "NULLIF col_name =BLANKS" works for character datatype, but when value for date col is NULL then I get ORA-01841 error. I have to make NULL for all rows withour value for date column
    Early reply will be highly appreciated
    Farooq

    Hi,
    May be this problem is not with the NULLIF. The value for the date column is not in proper date format.
    create table:
    create table kk (empno number, ename varchar2(20), deptno number, hiredate date)
    Control file:
    LOAD DATA
    INFILE 'd:\kk\empdata.dat'
    insert into TABLE kk ( empno position (1:2) integer external,
    ename position(4:5) char NULLIF ename=BLANKS,
    deptno position (7:8) integer external NULLIF deptno=BLANKS,
    hiredate position (10:20) date NULLIF hiredate=BLANKS)
    data file:
    10 KK 01-jan-2005
    20 10
    SELECT * FROM KK;
    EMPNO ENAME DEPTNO HIREDATE
    10 KK 01-JAN-05
    20 10
    Verify the data file.
    Hope it will help

  • Loading leap year date using SQL*Loader

    Hello,
    I have a problem loading a date '29/02/2000' using SQL*Loader. This date is on a leap year. I'm getting an error message from SQL*Loader as 'ORA-01839: date not valid for month specified'. My colleague and I have tried using various date functions to convert the data into date, but no luck.
    I would appreciate any helps,
    Bruce

    Thanks for your help, I found the bug on my control file. I was using the RTRIM function to remove bad timestamp such as '29/02/2000 0:00:00'. So instead of using this statement:
    LOG_DATE DATE "DD/MM/RRRR" "RTRIM(:LOG_DATE,'0:00:00')"
    I was using the statement below with a space before the '0:00:00' string literal, with the intention to remove a space also:
    LOG_DATE DATE "DD/MM/RRRR" "RTRIM(:LOG_DATE,' 0:00:00')"
    Well, it turned out that if there was a space before the string literal, RTRIM function would trim the matching string plus any '0' characters from the right, including the '000' that belongs to '2000'. Thus, the error.
    Thanks again,
    Bruce

  • Retrieve Data using SQL Data Reader

    Hello Team, I am working on a program where we have to read the data from sql database for specific barcode. Please have a look at the code below.
    If SqlConnection.State = ConnectionState.Closed Then
    SqlConnection.Open()
    End If
    Try
    Dim SQLCommand As New SqlClient.SqlCommand("SELECT * FROM tblProducts WHERE Barcode= @Barcode", SqlConnection)
    SQLCommand.Parameters.AddWithValue("@Barcode", txtPurchaseBarcode.Text)
    frmNoBarcode.lblBarcode.Text = txtPurchaseBarcode.Text
    Dim SQLReader As SqlClient.SqlDataReader
    SQLReader = SQLCommand.ExecuteReader
    If SQLReader.Read Then ' Barcode Exist
    ' Load Product Name
    lblFetchProduct.Text = SQLReader.Item("ItemName").ToString
    SQLReader.Close()
    End If
    Catch ex As Exception
    End Try
    If SqlConnection.State = ConnectionState.Open Then
    SqlConnection.Close()
    End If
    So It display the records, I just want that, in some case, if the program is unable to search for the barcode,  then program should give me a message that the Item does not exist and Exit Sub the application. Can somebody help me up ?

    Sir, can you please help me by giving me a code sample so I can understand in a better way, thanks
    If you are referring to me, the way I would write the whole code: (just a suggestion...)
    Private Function GetItemNameFromTblProductsByBarcode(ByVal Barcode As String) As String
    If SqlConnection.State = ConnectionState.Closed Then
    SqlConnection.Open()
    End If
    Try
    Dim SQLCommand As New SqlClient.SqlCommand("SELECT ItemName FROM tblProducts WHERE Barcode= @Barcode", SqlConnection)
    Dim Value As Object
    SQLCommand.Parameters.AddWithValue("@Barcode", Barcode)
    Value = SQLCommand.ExecuteScalar
    Return If(Value Is Nothing, Nothing, Value.ToString)
    Finally
    SqlConnection.Close()
    End Try
    End Function
    '' Call:
    Dim Itemname As String
    Itemname = GetItemNameFromTblProductsByBarcode(txtPurchaseBarcode.Text)
    If Itemname Is Nothing Then
    MessageBox.Show( _
    "The item with the barcode " & txtPurchaseBarcode.Text & " does not exist.", "", _
    MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
    lblFetchProduct.Text = Itemname
    frmNoBarcode.lblBarcode.Text = Itemname
    End If
    I do not know whe meaning of lblFetchProduct and frmNoBarcode.lblBarcode, so I just set them both in case the barcode has been found.
    Armin

Maybe you are looking for

  • Open Items Count not matching in FBL3N

    Hi All, Kindly help with below urgent issue: My client is using the Bank Reconciliation process (Manual). They have done the reconciliation for the SBI bank till 10th April 2014. They are using only three accounts : Main bank, Payment  and Receipt Ac

  • How do I delete multiple items at once instead of one at a time?

    How do I delete multiple items at once instead of one at a time? I have several duplicate items in my library and would like to delete the duplicates. Thanks!

  • Acc.Seq and InterCompany billing

    Hi everyone. I have the following problem. 1.The process: I have made an return order process in which I implemented an material status code. This status code is implemented in pricing in form of a condition type which value is defined in Transaction

  • How to convince parents to buy a macbook pro

    i want  a macbook pro extemely bad ! im in high school and all im taking is AP and IB classes . my mother doesnt seem to think i need one . please help

  • Reg User Defaults in CUP

    Dear Gurus , We are using SAP HR triggers to create SAP Userids for newly joined employees. It is working properly. we observed that Decimal notation is not getting captured properly. We want to default the same in auto - provisioning. Please guide h