Note that there are some explanatory texts on larger screens.

plurals
  1. POTeradata: Is there a way to generate DDL from a view or select statement?
    primarykey
    data
    text
    <p>I am using a global application user account to access database A. This user account does not have permissions to modify database A's schema (ie, create tables, modify tables, etc). This user also has access to database B, but only views. I need to run SQL to feed data from a view in database B into a table in database A.</p> <p>In a perfect world, I would be able to use this SQL:</p> <p><code>create database_a.mytable as (select * from database_b) with no data</code></p> <p>However, the user can't create tables in database A. If I could get the DDL of the select statement then I could log in under my personal account (which doesn't have any access to database B) and run the DDL in database A to create the table.</p> <p>The only other option is to manually write the SQL, but I don't want to do that, especially since this view I am wanting to copy has many columns of varying data types and sizes.</p> <p><strong>Edit:</strong> I may be getting closer. I just experimented with this:</p> <p><code>show (select * from database_b.myview)</code></p> <p>However, it generated the DLL of every single table that is used in the view itself, as well as the definition for the view. This doesn't really help me since I just want the schema of the select statement itself. In other words, I need what would be generated if I were to use the <code>create table as</code> statement mentioned above.</p> <p><strong>Edit for Rob:</strong> Perhaps "DDL" was the wrong term to use. Using <code>show view db.myview</code> just shows the definition of the view, not the schema it represents. In my above example of <code>create table as</code>, I show how you can create a table that mimics the schema of a result set returned in a select. It generates a DDL on the back end for creating a table and then executes that DDL to actually create the table. You can then say <code>show table db.newtable</code> and see the new table's DDL. I want to get <em>that</em> DDL directly from a select statement so that I can copy it, log out of the app account, into my personal account, and then execute the DDL to create the table.</p> <p>This is only to save me the headache of having to type out the DDL manually by hand to save time and reduce typing errors, especially since the source view has so many columns. That said, I think hitting up the DBA or writing some snazzy stored procedure to do dynamic stuff would be a bit over the top for my needs. I think there has to be a way to get the DDL for creating a table schema directly from a select statement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload