create an SQL schema from a select statement - tsql

I was doing the boring work of writing out a schema for a t-sql table that I will populate from a stored procedure and realized that it should be entirely possible to do most of the work of creating schema by having a query that concatenates the names of the fields in a query with the data types. I'm sure I can figure it out, but figured I'd ask here since I expect someone has already done it for me.

You can write SQL similar to this:
SELECT col1, col2, col3 INTO newTable FROM table1;

Related

temp tables in postgresql

I'm coming from a background in SQL Server where I would create temp tables using the:
select id
into #test
from table A
I've just moved into a PostGresql environment and I was hoping I could do the same, but I'm getting a syntax error. I did a search and it seems like you have to do a Create Table statement.
Is it not possible to easily create temp tables in Postgres?
Postgres supports SELECT INTO, so this should work fine:
SELECT id
INTO TEMP TABLE test
FROM a
You can also use CREATE TABLE AS:
CREATE TEMP TABLE test AS
SELECT id FROM a
This version is generally preferred, as the CREATE statement provides additional options, and can also be used in PL/pgSQL functions (where the SELECT INTO syntax has been hijacked for variable assignment).

Workaround in Redshift for "ADD COLUMN IF NOT EXISTS"

I'm trying to execute an S3 copy operation via Spark-Redshift and I'm looking to modify the Redshift table structure before running the copy command in order to add any missing columns (they should be all VARCHAR).
What I'm able to do is send an SQL query before running the copy, so ideally I would have liked to ALTER TABLE ADD COLUMN IF NOT EXISTS column_name VARCHAR(256). Unfortunately, Redshift does not offer support for ADD COLUMN IF NOT EXISTS, so I'm currently looking for a workaround.
I've tried to query the pg_table_def table to check for the existence of the column, and that works, but I'm not sure how to chain that with an ALTER TABLE statement. Here's the current state of my query, I'm open to any suggestions for accomplishing the above.
select
case when count(*) < 1 then ALTER TABLE tbl { ADD COLUMN 'test_col' VARCHAR(256) }
else 'ok'
end
from pg_table_def where schemaname = 'schema' and tablename = 'tbl' and pg_table_def.column = 'test_col'
Also, I've seen this question: Redshift: add column if not exists, however the accepted answer isn't mentioning how to actually achieve this.

Alter the column type over several tables

In a PostgreSQL db I'm working on, half of the tables have one particular column, always named the same, that is of type varchar(5). The size became a bit too restricting and I want to change it to varchar(10).
The number of tables in my particular case is actually very manageable to do it by hand. But I was wondering how one could script this with a query for larger dbs. It generally should be possible in just a few steps.
Identify all the tables in the schema, then (?) filter by condition if column present.
Create ALTER TABLE statements for each table found
I have some idea about how to write a query that identifies all tables in the schema. But I wouldn't know how to filter them. And if I didn't filter them, I assume the generated alter table statements would break.
Would be great if someone could share their knowledge on this.
Thanks to Abelisto for providing some guidance. Eventually, this is how I did it.
First, I created a query that in turn creates the ALTER TABLE statements. MyDB and MyColumn need to reflect actual values.
SELECT
'ALTER TABLE '||columns.table_name||' ALTER COLUMN '||MyColumn||' TYPE varchar(20);'
FROM
information_schema.columns
WHERE
columns.table_catalog = 'MyDB' AND
columns.table_schema = 'public' AND
columns.column_name = 'MyColumn';
Then it was just a matter of executing the output as a new query. All done.

slick insert query with forceInsertQuery

I need to copy table to another same schema table.
I would like to do something like
insert into table1 select * from table2
In slick, it seems possible to insert with queries.
There is a function with signature .insert(:Query)
In my table I defined a "id" column with auto-increment option.
However slick automatically omit auto-increment column except using forceInsert method.
In this case, column number doesn't match if I print the sql out:
val table = TableQuery[Table_X]
println(TableQuery[Table_Y].insertStatementFor( table.take(1000) ))
insert statement is lack of an "id" column, but table.take(1000) include it.
How can I solve this problem?
I see some functions called forceInsertQuery in the source code of slick on github. I am not sure whether this can help me or not

t-sql stored procedure add where clause outside of SP

I have a stored procedure that I cannot modify, but I need to add a where clause to filter it even more. What would be the best way to do this without inserting data from stored procedure to a temptable then doing a where on that temptable. Is there another way?
The store procedure is executable but filtering happens inside the select statement so you should bring the result in a table to select it.
There is no way except temp tables.
Table valued Udf also has table like temp tables.