How can I conditionally construct a table name for an SQL CREATE TABLE statement?

前端 未结 3 1907
孤独总比滥情好
孤独总比滥情好 2021-01-24 17:51

Within an SQL stored procedure, I would like to have the ability to construct a table name and create it.

Example: I just logged into my database under company 03 and a

3条回答
  •  迷失自我
    2021-01-24 18:39

    In Oracle the syntax would be something like

    BEGIN
      EXECUTE IMMEDIATE 'CREATE TABLE CUSTOMER_'||v_company_id||' (..)';
    END;
    

    However this is probably a really bad idea. Six months down the line you'll want to add a column to the table and you'll need to work out which tables you need to add it to.

    Also, stored procedures in Oracle need a fixed table name (of an existing table) or you'd have to reference everything through dynamic SQL which is a pain.

    Better to have a single customer table with the company_id as an attribute. Then use Fine Grained Access Control to securely filter on the company_id to control who see's what company's data.

提交回复
热议问题