create table and assign table names from a select query in sqlite - database

I have a select query that returns a single column. Is there a way in sqlite to create a new table using the results as column names?
I tried this but it did not work.
CREATE TABLE newTable (SELECT nameCol FROM oldTable);

SQLite does not support dynamic SQL so this is not possible.
The best that you can do is construct the SQL statement that you can use to create the table by using your preferred programming language:
SELECT 'CREATE TABLE newTable (' ||
(SELECT GROUP_CONCAT(nameCol) FROM oldTable) ||
');' AS sql;
The above query returns 1 row with 1 column with a string like:
CREATE TABLE newTable (column1,column2,column3);
See a simplified demo.

Related

How to get dynamically generated CREATE statement with SQL query?

In MySQL you can do SELECT CREATE TABLE .... and it will return the CREATE statement that was used to create this table. I need to do the same in SQL Server.
Is there any similar functionality in SQL Server? I have a table name test_table.
I need to run a SELECT statement that would return the CREATE TABLE string that was used to create this table. I tried this but it didn't work. How can I achieve this result in SQL Server?
You could try SELECT INTO and specify the name of a temporary table (prefixed with #). It also works with physical tables (although I've not found a use for this). Something like this
select '123' as colName into #newTable;
select * from #newTable;
colName
123

IF option in sqlite returning syntax error [duplicate]

I need to create a temp table (which is a copy of Employees table) in a SQLite database, provided the table by the name of 'Employees' exists. There are only 2 columns in Employees table - EmployeeId (integer) and EmployeeName (varchar(100)).
Is there any way to implement the above using SQLite SQL?
Pseudo-code for intended SQL, which does not work in SQlite is as below. I hope there was something as powerful as the pseudo-code below in SQLite.
--if Employees table exists then create a temp table and populate it with all
--rows from Employees table
CREATE TEMP TABLE tempEmployees if exists Employees as select * from Employees;
SQLite has almost no control logic; as an embedded database, it is designed to be used together with a 'real' programming language.
You could just try to copy the data, and ignore the errors:
try:
c.execute("CREATE TEMP TABLE tempEmployees AS SELECT * FROM Employees")
except:
pass
However, this would also suppress any other errors.
A better idea is to check explicitly whether the table exists:
c.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Employees'")
if c.fetchone():
c.execute("CREATE TEMP TABLE tempEmployees AS SELECT * FROM Employees")

How to frame insert statements from select statement data in SQL Server?

I've selected some data from a table it gives some rows in as result. Now, I want to generate insert statements from result data in SQL Server.
Please suggest me any solutions.
If the destination table is a new table,
you may use SQL SELECT INTO Statement.
We can copy all columns into the new table:
SELECT *
INTO newtable [IN externaldb]
FROM table1;
Or we can copy only the columns we want into the new table:
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
The new table will be created with the column-names and types as defined in the SELECT statement. You can apply new names using the AS clause.
Like this,
SELECT 'insert into tabledestination (col1destination,col2destination)
values (' + col1source + ',' + col2source + ')'
FROM tablesource;

How to treat unique constraint violations in a data migration script?

Here I'm trying to migrate some data from a table column to a brand new table in which destination column have an unique constraint. Basically I'm trying to:
INSERT INTO FooTable VALUES (SELECT BarTable.Code FROM BarTable)
FooTable have only 2 columns: ID and Code (column with unique constraint).
But on BarTable.Code, maybe there are some duplicate values that I need to treat and fit them in the new constraint (maybe: Code = Code + 1 or else).
Any ideas on how to do that?
I am using MS SQL Server 2008 R2.
Thank you in advance.
You can use the MERGE command and insert a different record when the codes match.
Here is an example based on your scenario:
MERGE FooTable AS T
USING BarTable AS S
ON (T.Code = S.Code)
WHEN NOT MATCHED BY TARGET
THEN INSERT(Code) VALUES(S.Code)
WHEN MATCHED
THEN INSERT(Code) VALUES(S.Code+1)
You can use Not Exists
INSERT INTO FooTable VALUES (SELECT distinct br.Code FROM BarTable br where NOT EXISTS(SELECT * FROM FooTable bs where br.code=bs.code ) )

Creating a procedure for creating a table with dynamic number of columns

I'm trying to create a procedure that can create a table with not specific number of columns.
My query returns a value of 3 meaning it needs 3 columns (has to be dynamic).
I have create a #variable to set the name string of the tables but I don't know how to formulate the CREATE TABLE statement to actually create the table with the columns from this string.
Any kind of help would be appreciated guys.
You can get the columns on a table out of the sql database with
select
bb.name,
bb.colid
from sysobjects aa
inner join syscolumns bb
on aa.id = bb.id
where aa.name ='tblMyTable'
The name is the column name, the ID the number. You could select column names from the list and use dynamic sql to build a select. Not sure how you decide what columns you are after from the table.

Resources