Can I use Berkeley DB sqlite3 SQL API in tmpfs?

Hi
To improve the efficiency of data access, I want to use Berkeley DB as in-memory SQL database. At the beginning, I assigned the argument of filename as “:memory:” when I called sqlite3_open(). Then I found in this way, it just built a private temporary in-memory database which could not be accessed between processes or between threads.
So I tried in the following way:
1.     Firstly, built a tmpfs on my OS:
mkdir /dev/shm/tmp
chmod 1777 /dev/shm/tmp
mount --bind /dev/shm/tmp /tmp/bdb_test
2.     Then I called the function sqlite3_open() with the argument /tmp/bdb_test/test.db, thus Berkeley DB would create the database test.db in memory, but it is transparent to BerkeleyDB.
Can I do this in the above way? Is there any potential risk?
Is there any other method to build a in-memory SQL database which could be shared between processes and threads?
Thanks!
Ps: My testbed information:
CentOS6
$ uname -a
Linux localhost.localdomain 2.6.32-71.el6.x86_64 #1 SMP Fri May 20 03:51:51 BST 2011 x86_64 x86_64 x86_64 GNU/Linux
Berkeley DB version: db-5.2.28
When I build BDB, I using the following options:
../dist/configure enable-sql_compat enable-test --with-tcl=/usr/lib64
Edited by: 882071 on 2011-9-6 下午7:27

Hi,
Then I called the function sqlite3_open() with the argument /tmp/bdb_test/test.db, thus Berkeley DB would create the database test.db in memory, but it is transparent to BerkeleyDB.
Can I do this in the above way? Is there any potential risk? I do not see any problem with using a tmpfs (temporary filesystem) to host the database. Of course, as you probably know, there are no durability guarantees with tmpfs, no recoverability for the database in this case.
Is there any other method to build a in-memory SQL database which could be shared between processes and threads?I do not have the resources to look into this now, but I would suggest trying the sqlite3_open_v2() function for opening the connections, with the shared cache mode (the SQLITE_OPEN_SHAREDCACHE flag), like this (making these calls in each process that opens a connection to the database):
#define TRUE 1
sqlite3_enable_shared_cache(TRUE);
sqlite3_open_v2(":memory:", &pDbHandle, SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "");(look into the SQLITE_OPEN_FULLMUTEX and SQLITE_OPEN_NOMUTEX flags as well, for specifying the threading model for opening the database connection)
Regards,
Andrei

Similar Messages

  • Why such error information when I used sqlite3 SQL API?

    Hi
    When I programming with sqlite3 SQL API, some error information about lock occurred:
    DB_LOCK->lock_put: Lock is no longer valid
    Following error log also occurred sometimes:
    “Locker still has locks”
    I have called the function sqlite3_threadsafe to make sure the library is threadsafe.
    Is there any way to avoid the above error logs?
    The information of my test bed is as follows:
    CentOS6
    $ uname -a
    Linux localhost.localdomain 2.6.32-71.el6.x86_64 #1 SMP Fri May 20 03:51:51 BST 2011 x86_64 x86_64 x86_64 GNU/Linux
    Berkeley DB version: db-5.2.28
    When I build BDB, I using the following options:
    ../dist/configure enable-sql_compat enable-test --with-tcl=/usr/lib64
    Can you help me with that please?
    Thanks!

    This error was already reported in another Re: Berkeley DB lost data sometimes when I ran the example ex_sql_multi_thread. Please do not post the same issue multiple times and follow the issue in the original thread, which is currently handled by one of our engineers.
    Thanks,
    Andrei

  • Can we use different Databases (Oracle & SQL Server) in one report?

    Post Author: venki5star
    CA Forum: .NET
    Hi there.
    Can we use different databases (Oracle & SQL Server) in a same report?
    If possible how?
    Another question,
    Can we change the Provider Name at runtime of the given report. If so the above question is useless...
    Thanks in Advance.

    I tried this using Oracle Provider for OLEDB (the one that supplied by Oracle Client) and Crystal Reports 9. you can drag the column into designer but the image does not appear in preview.
    I guess it's because CR does not recognized it as image, and there are no information that the blob data is an image at all.

  • Issue using the DBFS PL/SQL API

    Hi guys,
    I'm trying to use the DBFS PL/SQL APIs and create some simple functions that for example return the file size or move a file to a different path. I created a user DBFS_USER and granted DBFS_ROLE to that user. Using SQLDeveloper, when I run the following code, it works fine:
    SET SERVEROUTPUT ON
    DECLARE
    l_props DBMS_DBFS_CONTENT_PROPERTIES_T;
    l_prp dbms_dbfs_content_property_t;
    l_blob BLOB;
    l_item_type INTEGER;
    BEGIN
    DBMS_DBFS_CONTENT.getPath (
    path => '/staging_area/test_dir/IAMDBFS_USER.txt',
    properties => l_props,
    content => l_blob,
    item_type => l_item_type);
    FOR x in 1..l_props.count LOOP
    l_prp := l_props(x);
    if (l_prp.propname = 'std:length') then
    DBMS_OUTPUT.put_line(l_prp.propname || ' : ' || l_prp.propvalue);
    end if;
    END LOOP;
    END;
    However, when I try to put it in a package, I get the error message "Error(415,10): PLS-00201: identifier 'DBMS_DBFS_CONTENT_PROPERTY_T' must be declared" while trying to compile. This is what my package and its body looks like:
    create or replace
    PACKAGE MY_DBFS_PKG IS
    Function GetFileLength
    (P_FILE_PATH IN Varchar2
    ,P_ErrMsg OUT Varchar2)
    Return Number;
    END MY_DBFS_PKG
    Body:
    create or replace
    PACKAGE BODY MY_DBFS_PKG IS
    FUNCTION GetFileLength(
    P_FILE_PATH IN VARCHAR2 ,
    P_ErrMsg OUT VARCHAR2)
    RETURN NUMBER
    IS
    l_return NUMBER;
    l_props DBMS_DBFS_CONTENT_PROPERTIES_T;
    l_prop DBMS_DBFS_CONTENT_PROPERTY_T;
    l_blob BLOB;
    l_item_type INTEGER;
    BEGIN
    l_Return := 0;
    P_ErrMsg := NULL;
    DBMS_DBFS_CONTENT.getPath ( path => p_file_path, properties => l_props, content => l_blob, item_type => l_item_type);
    FOR x IN 1..l_props.count
    LOOP
    l_prop := l_props(x);
    IF (l_prop.propname = 'std:length') THEN
    l_Return := l_prop.propvalue;
    EXIT;
    END IF;
    END LOOP;
    EXCEPTION
    WHEN OTHERS THEN
    l_Return := NVL(SQLCODE, -1);
    p_ErrMsg := SQLERRM;
    RETURN l_return;
    END GetFileLength;
    END MY_DBFS_PKG;
    Am I missing an extra grant somewhere? Or have I missed a declaration in the package or package body?
    I am not a DB person so pardon the probably inefficient loop to find the length or wrong declaration somewhere. I'm open to suggestions/recommendations but want to use the DBFS PL/SQL API as I need other functions out of it.
    Thanks very much in advance.
    Cappa

    Hi guys,
    I'm trying to use the DBFS PL/SQL APIs and create some simple functions that for example return the file size or move a file to a different path. I created a user DBFS_USER and granted DBFS_ROLE to that user. Using SQLDeveloper, when I run the following code, it works fine:
    SET SERVEROUTPUT ON
    DECLARE
    l_props DBMS_DBFS_CONTENT_PROPERTIES_T;
    l_prp dbms_dbfs_content_property_t;
    l_blob BLOB;
    l_item_type INTEGER;
    BEGIN
    DBMS_DBFS_CONTENT.getPath (
    path => '/staging_area/test_dir/IAMDBFS_USER.txt',
    properties => l_props,
    content => l_blob,
    item_type => l_item_type);
    FOR x in 1..l_props.count LOOP
    l_prp := l_props(x);
    if (l_prp.propname = 'std:length') then
    DBMS_OUTPUT.put_line(l_prp.propname || ' : ' || l_prp.propvalue);
    end if;
    END LOOP;
    END;
    However, when I try to put it in a package, I get the error message "Error(415,10): PLS-00201: identifier 'DBMS_DBFS_CONTENT_PROPERTY_T' must be declared" while trying to compile. This is what my package and its body looks like:
    create or replace
    PACKAGE MY_DBFS_PKG IS
    Function GetFileLength
    (P_FILE_PATH IN Varchar2
    ,P_ErrMsg OUT Varchar2)
    Return Number;
    END MY_DBFS_PKG
    Body:
    create or replace
    PACKAGE BODY MY_DBFS_PKG IS
    FUNCTION GetFileLength(
    P_FILE_PATH IN VARCHAR2 ,
    P_ErrMsg OUT VARCHAR2)
    RETURN NUMBER
    IS
    l_return NUMBER;
    l_props DBMS_DBFS_CONTENT_PROPERTIES_T;
    l_prop DBMS_DBFS_CONTENT_PROPERTY_T;
    l_blob BLOB;
    l_item_type INTEGER;
    BEGIN
    l_Return := 0;
    P_ErrMsg := NULL;
    DBMS_DBFS_CONTENT.getPath ( path => p_file_path, properties => l_props, content => l_blob, item_type => l_item_type);
    FOR x IN 1..l_props.count
    LOOP
    l_prop := l_props(x);
    IF (l_prop.propname = 'std:length') THEN
    l_Return := l_prop.propvalue;
    EXIT;
    END IF;
    END LOOP;
    EXCEPTION
    WHEN OTHERS THEN
    l_Return := NVL(SQLCODE, -1);
    p_ErrMsg := SQLERRM;
    RETURN l_return;
    END GetFileLength;
    END MY_DBFS_PKG;
    Am I missing an extra grant somewhere? Or have I missed a declaration in the package or package body?
    I am not a DB person so pardon the probably inefficient loop to find the length or wrong declaration somewhere. I'm open to suggestions/recommendations but want to use the DBFS PL/SQL API as I need other functions out of it.
    Thanks very much in advance.
    Cappa

  • How can I use the latest Berkeley DB with SQL API in PHP app?

    Hi,
    I'm a PHP developer and I already use Berkeley DB in my applications using the [DBA Functions|http://www.php.net/manual/en/ref.dba.php] . I'm very interested in the latest Berkeley DB release. How can I write a PHP program that uses the SQL API of the latest Berkeley DB release?

    Hi Kurt,
    I'm not a PHP expert (far from it), but the simplest way to get started with the Berkeley DB SQL interface is to run Berkeley DB's <tt>configure</tt> script with the <tt>--enable-sql_compat</tt> flag.  This will create a library called <tt>libsqlite3.so</tt> that you can use as a drop-in replacement for SQLite at the API level.  Database files still need to be converted with a SQLite <tt>.dump</tt> followed by a <tt>.read</tt> with the <tt>dbsql</tt> tool.
    This should then allow the existing PHP driver and application code to switch over to Berkeley DB without any code changes by setting <tt>LD_LIBRARY_PATH</tt> to find the Berkeley DB version of <tt>libsqlite3.so</tt> ahead of SQLite. Note that any other code that relies on SQLite will also run against Berkeley DB with this configuration, which can cause problems if the system libraries use SQLite (for example, on Mac OS X). For that reason, a better long term solution would be for the PHP SQLite driver to have a mode where it loads <tt>libdbsql.so</tt> rather than <tt>sqlite3.so</tt>.
    I hope this helps, please let us know how you go because I'm sure lots of other PHP developers will be interested in the answer.
    Regards,
    Michael Cahill, Oracle Berkeley DB.

  • Berkeley DB with SQL API: Where to start?

    Hi,
    I am an Oracle DBA and just starting to look at Berkeley DB for a new project at work. I intend to use the SQL API, so I have downloaded and installed Berkeley 11g R2 (using the windows msi).
    However, I have been browsing around the documentation for 2 days now but still don't understand how to actually create a database! Is this not possible with the SQL API? Do I have to do it from "somewhere else" and just use the API to access the data?
    The strange thing is, if I open the API (dbsql) I can create a table:
    dbsql> create table test_tab(x int) ;
    dbsql> .tables
    test_tab
    dbsql> exitBut I have no idea where that table is actually created and stored? I don't see a file on disk (a "database" is a file, right?). If I open dbsql again, the table is not shown.
    Sorry for the dumb questions but I'm really an old relational-DB guy struggling to come to terms with this architecturally and functionally different idea of a database. Hope someone can help, or at least point me to a "Berkeley DB using SQL API for complete and utter dummies" type guide.
    Regards,
    El DBA

    Hi Sandra,
    Thanks very much for the information. I have been looking through the documentation and starting to get to grips with this now.
    We do have one important issue though, which comes from within the FAQ you mentioned. I see it's also been talked about in this thread:
    ado.net provider
    In the FAQ, it says:
    Does Berkeley DB support ADO.NET?
    There are some known issues related to using the public domain SQLite ADO.NET provider with Berkeley DB. We are actively working on clearly defining those issues, and hopefully resolving them. Is there any idea of a date for the ado.net support? In our development, we are using that very same provider, which is the same one as mentioned in that thread (it's system.data.sqlite.dll from http://sqlite.phxsoftware.com) but will not be able to commit to developing with Berkeley until the support is offered.
    Kind regards,
    Adam
    Edited by: El DBA on Jul 29, 2010 1:13 PM
    Edited by: El DBA on Jul 29, 2010 1:16 PM

  • How to use the EBS PL/SQL API from outside PL/SQL

    Hi,
    our attempts to call the stored procedures/functions of the EBS PL/SQL API via JDBC revealed the following issues:
    1. Logical values cannot be exchanged through boolean parameters as the OCI has no notion of this type. Instead we need to write PL/SQL wrappers converting boolean to integer and vice versa. Is that true or are we missing something?
    2. Complex values cannot be exchanged through record types as only PL/SQL code has a notion of this type. Instead we need to write PL/SQL wrappers converting record values to object type values. Is that true or are we missing something?
    3. Types must be defined outside of packages, i.e. in the schema, for whatever reason. Instead we need to write PL/SQL wrappers converting values of types defined in packages to values of types defined in the schema. Is that true or are we missing something?
    4. It is possible to let functions return tables so the results can be queried using SELECT * FROM TABLE. Such functions are called "table functions". Is there something similar for the other direction, i.e. is it possible to call a stored procedure and pass in a table value through an sql INSERT statement?
    5. Do we really have to write all those wrapper routines on our own or is there a PL/SQL library providing such? If not, is there some kind of code generation facility that can generate such wrappers?
    Thanks and Regards,
    Konrad

    Hi Helios,
    I belive its better to move your issue on Forum Home » Database » SQL and PL/SQL which you can get more quick responseYou are probably right! I was thinking about a different forum but I did not see this one. I just posted the question there...
    Still, if somebody has an idea here as well, just let me know ;-)
    Konrad

  • How can I use the Rownum/Customized SQL query in a Mapping?

    Hi,
    * I need to use a Rownum for populating one of the target field? How to create a mapping with Rownum?
    * How can I use an Dual table in OWB mapping?
    * Can I write Customized SQL query in OWB? How can I achieve this in a Mapping?
    Thanks in Advance
    Kishan

    Hi Niels,
    As I'm sure you know, the conundrum is that Reports doesn't know how many total pages there will be in the report until it is all done formatting, which is too late for your needs. So, one classical solution to this problem is to run the report twice, storing the total number of pages in the database using a format trigger, and throwing away the output from the first run when you don't know the total number of pages.
    Alternatively, you could define a report layout so that the number of pages in the output is completely predictable based upon, say, the number of rows in the main query. E.g., set a limit of one, two, ... rows per page, and then you'll know how many pages there will be simply because you can count the rows in a separate query.
    Hope this helps...
    regards,
    Stewart

  • Can i use unc or ucwa web api to build android and windows phone app ?

    is there anyway to develop custom client application using unc web api ( android, ios, windows phone ) with following features
    App to app audio / video calling
    App to app audio conference call
    App to app messaging 
     

    Instant messaging is perfectly supported already by UCWA, audio/video are not at this moment.
    UCWA can be used by any platform/device that can 'speak HTTP', so yes the platforms you mentioned are suitable UCWA clients.
    You may adopt third party solutions and combine them with UCWA within your app in order to achieve audio/video call support.
    HTH
    Massimo Prota
    .NET MCAD - SP2007 MCTS - SP2010 MCPD and MCITP
    My blog: http://blogs.ugidotnet.org/mprota - Twitter:
    @massimoprota

  • Can I use if statement in SQL???

    Hello everyone.
    I know that this is a silly question. The answer is no. Instead I should use decode function.
    But what if I dont know all possible outcomes, can I still use decode function?
    Also, I have created a view, in the where clause i want to use if statement. Is it possible to do?
    Thanks in advance,
    Sonya

    here is the view that I created:
    create or replace view import_shipment2
    ( order_no, cust_no,ORDER_SUFFIX,ship_addr1,ship_addr2, city,state,ZIP,country, terms_cd,shipvia_cd,
    freight_cd, shipto_cdoehead,shipto_cdshipto,ship_name,ship_attn,order_status, org_unit_id, order_date,
    estship_dt,confirm_status,gross_inv_value,phone,total_line_no,total_qty_order,total_qty_shipped,
    total_qty_backordered,service,hold_cd,invno)
    as select
    to_char(c.order_no)||'_'||c.ORDER_SUFFIX, c.cust_no,c.ORDER_SUFFIX,d.ship_addr1,
         d.ship_addr2, b.city, b.state, b.zip_code,b.country, d.terms_cd,d.shipvia_cd,d.freight_cd, d.shipto_cd,b.shipto_cd,
         nvl(d.ship_name,b.shipto_attn), nvl2(d.ship_attn,b.shipto_attn,e.contact),d.order_status, d.org_unit_id,
         d.order_date, d.estship_dt, c.confirm_status, f.gross_inv_value ,nvl(d.shipto_recip_phone ,g.phone_no),
         sum(c.line_no),sum(c.qty_order),sum(c.qty_shipped),sum(c.qty_bo),d.shipvia_cd ,d.hold_cd,d.invno
    from shipto_test b, oehead_test d,oedetl_test c, oesummary_test f ,CONTACT_MASTER_TEST e ,CONTACT_PHONE_TEST g
    where c.order_no=d.order_no
    and b.cust_no=d.cust_no
    and b.shipto_cd=d.shipto_cd
    and d.cust_no=f.cust_no
    and d.order_no=f.order_no
    and d.org_unit_id in ('110','120')
         and e.cust_no=d.cust_no
    and e.shipto_cd= d.shipto_cd
    and d.order_status in ('J','S')
    and g.contact_id=e.contact_id
    and g.phone_type='Business'
    Group by
    to_char(c.order_no)||'_'||c.ORDER_SUFFIX, c.cust_no,c.ORDER_SUFFIX,d.ship_addr1,
         d.ship_addr2, b.city, b.state, b.zip_code,b.country, d.terms_cd,d.shipvia_cd,d.freight_cd, d.shipto_cd,b.shipto_cd,
         nvl(d.ship_name,b.shipto_attn), nvl2(d.ship_attn,b.shipto_attn,e.contact),d.order_status, d.org_unit_id,
         d.order_date, d.estship_dt, c.confirm_status, f.gross_inv_value ,nvl(d.shipto_recip_phone ,g.phone_no),
         d.shipvia_cd ,d.hold_cd,d.invno;
    I need to do the validation of the g.phone_type. Does phone number exist at all in table?
    How do I all PL/SQL to this view??
    Thanks once agin,
    Sonya

  • Can we use if/else in sql statement?

    Hi,
    Is it possible to write a sql statement like the one below (not plsql)?
    if (select 'True' from dual) = 'True' -- 1
    then
    select 'True' from dual;  -- 2
    else
      select 'False' from dual; -- 3
    endBasically, I want to execute either sql 2 or 3 based on the result of sql 1. I'm not sure if this is possible using only sql and not having to write a stored proc.
    What are the different ways I can implement this?
    Thanks for the help.

    Billy  Verreynne  wrote:
    The case syntax is a bit funny though as there's not a single condition evaluation (like a DECODE or case structs from some other languages).
    This is what I would expect a typical case struct to look like - evaluating a single condition:
    case <condition>
    when <value-1> then return <result-1>
    when <value-n> then return <result-n>
    else
    return <return-z>
    end
    ?:| You mean like this...?
    SQL> ed
    Wrote file afiedt.buf
      1  select empno, ename, deptno
      2        ,case deptno
      3           when 10 then 'This is Department 10'
      4           when 20 then 'And department 20'
      5           when 30 then 'And of course department 30'
      6         else
      7           'Blimey it is something else!'
      8         end as dept_desc
      9* from emp
    SQL> /
         EMPNO ENAME          DEPTNO DEPT_DESC
          7369 SMITH              20 And department 20
          7499 ALLEN              30 And of course department 30
          7521 WARD               30 And of course department 30
          7566 JONES              20 And department 20
          7654 MARTIN             30 And of course department 30
          7698 BLAKE              30 And of course department 30
          7782 CLARK              10 This is Department 10
          7788 SCOTT              20 And department 20
          7839 KING               10 This is Department 10
          7844 TURNER             30 And of course department 30
          7876 ADAMS              20 And department 20
          7900 JAMES              30 And of course department 30
          7902 FORD               20 And department 20
          7934 MILLER             10 This is Department 10
    14 rows selected.

  • Why i can not use dbco to connect sql server 2008?

    Dear Experts,
        i use this mehtod to link sql server 2005 :MSSQL_SERVER=tcp:mysqlserver IP  MSSQL_DBNAME=SubConPayment,
    it is ok,when i use this method to link sql server 2008 with dbco,it is not ok,can you tell me why i can not link to sql server2008 in sap with dbco?
    looking forward to your reply.
    Best regards,
    Merry

    Hi Merry,
    Seems that you have to add the "provider" attribute to the connectionstring. Please have a look at the following link: [http://www.connectionstrings.com/sql-server-2008#p2]
    Regards,
    Ozcan.

  • Can we use repository variables in SQL statement of column prompt?

    Hi Dudes,
    Below is the query
    SELECT "- End Date"."End Fiscal Year" FROM "Consumer Sector" WHERE ("- End Date"."End Fiscal Year" = valueof (current_year)) or ("- End Date"."End Fiscal Year" = valueof (current_year) -1)
    when use this sql in criteria prompt getting error.
    Please suggest .
    thanks.sri

    Make sure your syntax is correct it should be like VALUEOF("CURRENT_YEAR")-1
    If you still have issues then VALUEOF("CURRENT_YEAR")-1 cast it to int before you subtract.
    If helps pls mark as correct else let share error message

  • Can I use ' ', ' ', '= ' and ' =' with java.sql.Timestamp objects in EJB-QL

    ie. Is this valid?
    <query>
    <description>Find data between dates</description>
    <query-method>
    <method-name>findMetricsByDate</method-name>
    <method-params>
    <method-param>java.sql.Timestamp</method-param>
    </method-params>
    <method-params>
    <method-param>java.sql.Timestamp</method-param>
    </method-params>
    </query-method>
    <result-type-mapping>Local</result-type-mapping>
    <ejb-ql>
    SELECT OBJECT (o) FROM MetricResults AS o WHERE MetricResults.date > ?1 AND MetricResults.date < ?2
    </ejb-ql>
    </query>

    No. Not with current EJB 2.0 CMP specs at least. Later revisions are supposed to fix this, but for now, this seriously limits the usefulness of EJB-QL.
    .P.

  • ็็How can i use multiple recursive in sql ?

    I have 2 tables  which have each with cte itself
    For position Table
    CREATE TABLE [Dep](
    [Depid] [int] NULL,
    [DepName] [nvarchar](50) NULL,
    [Depparent] [int] NULL
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (1, N'saleLeader', NULL, 3)
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (2, N'PurLeader', NULL, 1)
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (3, N'sale1', 1, 3)
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (4, N'Pur1', 2, 1)
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (5, N'ProductionLeader', NULL, 4)
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (6, N'Pro1', 5, 4)
    INSERT [dbo].[Position] ([poid], [poname], [poparent], [Depid]) VALUES (7, N'Pro2', 5, 4)
    WITH EmpCTE( poid , poname , poparent , Depid  , lvl)
    AS
      SELECT poid , poname , poparent , Depid  , 0
      FROM Position
      UNION ALL
      SELECT  E.poid  , E.poname , E.poparent , e.Depid   , lvl+1
      FROM Position AS E
        JOIN EmpCTE AS M
          ON E.poparent = M.poid
    SELECT * FROM EmpCTE
    For Dep Table
    CREATE TABLE [Dep](
    [Depid] [int] NULL,
    [DepName] [nvarchar](50) NULL,
    [Depparent] [int] NULL
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (1, N'Admin', 7)
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (2, N'Pur', 1)
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (3, N'Sale', 1)
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (4, N'Manufac', 7)
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (5, N'Production1', 4)
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (6, N'finishing1', 4)
    INSERT [dbo].[Dep] ([Depid], [DepName], [Depparent]) VALUES (7, N'MD', NULL)
    WITH EmpCTE( depid , depname , depparent  , lvl)
    AS
      SELECT depid , depname , depparent  , 0
      FROM Dep
      UNION ALL
      SELECT  E.depid  , E.depname , E.depparent , lvl+1
      FROM Dep AS E
        JOIN EmpCTE AS M
          ON E.depparent = M.depid
    SELECT * FROM EmpCTE
    The table position has a depid column which join to Dep datable.
    I want to put position level  in dep level.
    Like if  at positin level 0 must be under dep.

    Please see this link:
    CTEs with Multiple Recursive Members
    T-SQL Articles
    T-SQL e-book by TechNet Wiki Community
    T-SQL blog

Maybe you are looking for

  • Importing class files

    Hi, I'm new to Eclipse and need help. I have been given a folder with 5 classes and 1 class with a main method. My first question is: Is there a way to import these classes into eclipse and run the main class with main method? I don't have any java f

  • Greyed out images when opened in photoshop CS

    I ahve been happily using my photoshop but recently all my images are greyed out with small white and grey boxes so cannot view or change.  I have clicked on the imagine open section in history and it makes not difference.  What am I doing wrong?

  • I receive error A125E5 when trying to download applications

    I get an error every time I try and run the Adobe Application Manager. Its says its updating the manager then this error is displayed: We've encountered the following issues: Sorry, the program is not responding correctly (Error code: A125E5). For tr

  • How to do YTD and MTD

    Hi all, Can anyone please explain me how to get the sum of the value of measure till the current date? FOr ex jan value is 100, feb it is 200 etc now if date of today is slected all should be summed up and shown in the table view. I tried running sum

  • BT 1500 base unit and 4500 handsets

    Hi  Our BT studio 4500 base charging unit recently stopped working after a lightening storm. Not sure if the base unit itself was affected but without the functioning charging unit it's not possible to know. Rather than replace the 4500 base unit and