Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:11:09+00:00 2026-06-01T22:11:09+00:00

I have 2 tables: \d folder Table public.folder Column | Type | Modifiers ———+———————–+—————————————————-

  • 0

I have 2 tables:

\d folder

                              Table "public.folder"
Column   |         Type          |                      Modifiers                      
---------+-----------------------+----------------------------------------------------
id        | integer               | not null default nextval('folder_id_seq'::regclass)
name      | character varying(20) | 
parent_id | integer               | 

Indexes:
  "folder_pkey" PRIMARY KEY, btree (id)

Foreign-key constraints:
  "fk_folder_1" FOREIGN KEY (parent_id) REFERENCES folder(id)

Referenced by:
  TABLE "files" CONSTRAINT "fk_files_1" FOREIGN KEY (folder_id) REFERENCES folder(id)
  TABLE "folder" CONSTRAINT "fk_folder_1" FOREIGN KEY (parent_id) REFERENCES folder(id)

\d files

                              Table "public.files"
Column    |         Type          |                     Modifiers                      
----------+-----------------------+---------------------------------------------------
id        | integer               | not null default nextval('files_id_seq'::regclass)
name      | character varying(20) | 
folder_id | integer               | 

Indexes:
  "files_pkey" PRIMARY KEY, btree (id)

Foreign-key constraints:
  "fk_files_1" FOREIGN KEY (folder_id) REFERENCES folder(id)


select * from folder;
 id |  name   | parent_id 
----+---------+-----------
  1 | home    |          
  2 | folder2 |         1
  3 | folder3 |         1
  4 | folder4 |         2
  5 | folder5 |         4
  6 | folder6 |         5

(6 rows)



select * from files;
 id | name  | folder_id 
----+-------+-----------
  1 | file1 |         4
  2 | file2 |         4
  3 | file3 |         5
  4 | file4 |         6
  5 | file5 |         6
  6 | file6 |         2
(6 rows)

Now I need a function or cursor or any thing which will get two inputs,folder to copy and destination folder to copy, the function should copy the folder and its child folders into the same table with new id and parent id as below, same time when folder is copied and inserted the file in files table is also to be inserted, plz help me get the below result..

if I am coping folder5 to folder3 my output should be like this:

select * from folder;
 id |  name   | parent_id 
----+---------+-----------
  1 | home    |          
  2 | folder2 |         1
  3 | folder3 |         1
  4 | folder4 |         2
  5 | folder5 |         4
  6 | folder6 |         5
  7 | folder5 |         3
  8 | folder6 |         7
(8 rows)

and files table is also to be updated when folder is copied and inserted like this:

 select * from files;
 id | name  | folder_id 
----+-------+-----------
  1 | file1 |         4
  2 | file2 |         4
  3 | file3 |         5
  4 | file4 |         6
  5 | file5 |         6
  6 | file6 |         2
  7 | file3 |         7
  8 | file4 |         8
  9 | file5 |         8
(9 rows)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:11:11+00:00Added an answer on June 1, 2026 at 10:11 pm
    CREATE OR REPLACE FUNCTION tree_copy(INTEGER,INTEGER) RETURNS VOID AS $$
    DECLARE
        a ALIAS FOR $1; --ROOT FOLDER TO BE COPIED
        b ALIAS FOR $2; --DESTINATION FOLDER
        i INTEGER;
        j INTEGER;
        g INTEGER;
    BEGIN
    --DROP TABLE IF EXISTS temp1;
    
    CREATE TEMPORARY TABLE temp1 AS(
    WITH RECURSIVE CTE AS(
    SELECT *, NEXTVAL('folder_id_seq') new_id FROM folder WHERE id = a
    UNION ALL
    SELECT folder.*,NEXTVAL('folder_id_seq') new_id FROM CTE
    JOIN folder ON CTE.id = folder.parent_id)
    SELECT C1.id, C1.new_id, C1.parent_id, 
    C2.new_id new_parent_id FROM CTE C1 LEFT JOIN
    CTE C2 ON C1.parent_id = C2.id);
    
    FOR i IN (WITH RECURSIVE  t AS(SELECT id, parent_id FROM folder WHERE id = a
    UNION SELECT f.id,f.parent_id FROM folder f, t AS t1 WHERE f.parent_id = t1.id)
    SELECT id FROM t)
    LOOP
        SELECT new_parent_id INTO g FROM temp1 WHERE id = i;
    
        INSERT INTO folder(id,name,parent_id)VALUES(
        (SELECT new_id FROM temp1 WHERE id = i),
        (SELECT name FROM folder WHERE id = i),COALESCE(g,b));
    
        FOR j IN (SELECT id FROM files WHERE folder_id = i)
        LOOP
            INSERT INTO files(id,name,folder_id) VALUES (
            NEXTVAL('files_id_seq'),(SELECT name FROM files WHERE id = j),
            (SELECT new_id FROM temp1 WHERE id = i));
        END LOOP;
    END LOOP;
    DROP TABLE temp1;
    END;
    $$ LANGUAGE PLPGSQL; 
    

    this will do as i thought…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a table FOLDERS ,everytime a folder is added through a php page
I have tables linked by FK, I query on the first table using entity
I have a folder hierarchy represented by the following class: public class Folder {
I have a Mysql table where it combines three tables. This table stores some
I have one issue with retrieval Parent/Child relationship of type Folder Hierarchy Ideally, in
I have 3 .apk files in the Android folder on my xoom tablet but
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables named news and tags_news. Given a news id I have to
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.