Copy tables selected from sys.tables to new DB - sql-server

Note:- I have a Database(DB) with Thousands of Tables.
I want to Copy all Tables with Name like 'TableType1%' to a new Database(DB).
I can easily get a list of the tables:
select * from sys.tables t
where schema_name(t.schema_id) = 'S1' AND [name] LIKE 'TableType1%'
But how do I copy them to a new Database(DB)?
I CANNOT do this manually, as there are too many Tables.
I would like to use
INSERT INTO....
Type statement, but don't know how to put it into Select Statement above.

Select * into DestinationDB from SourceDB.sys.tables where name like 'TableType1%'
Note:- In database (DestinationDB) the physical tables are created with same name in source databse (SourceDB)

Related

Can I export table properties using DBeaver?

Request
I want to list all the table names in a schema in association with the list of that table attributes. I'm using DBeaver, but I can download other software if it works. I don't mind about the output data extension: txt, excel, csv etc., but I'm not searching for an ER diagram.
Example
If the database schema contains these three tables
TABLE_A
ATTRIBUTE_A_1
ATTRIBUTE_A_2
ATTRIBUTE_A_3
TABLE_B
ATTRIBUTE_B_1
TABLE_C
ATTRIBUTE_C_1
ATTRIBUTE_C_2
I want to extract something like this:
TABLE_A
ATTRIBUTE_A_1
ATTRIBUTE_A_2
ATTRIBUTE_A_3
TABLE_B
ATTRIBUTE_B_1
TABLE_C
ATTRIBUTE_C_1
ATTRIBUTE_C_2
Thanks in advance!
I've find out that, in Oracle, it's possible to list all the columns visible by the user who executes this query:
SELECT
TABLE_NAME,
COLUMN_NAME
FROM
ALL_TAB_COLUMNS
ORDER BY
TABLE_NAME ASC
DBeaver doesn't support "info" and "describe" commands as SQLDeveloper does. You can use a query like "SELECT ... FROM user_tab_columns".

Propagate Indexes when CREATE TABLE from two tables in PostgreSQL

I create a table by linking two different tables in PostgreSQL. I do this with a sentence like this:
CREATE TABLE t_aux_prop_delay
AS
SELECT r.cell, r.kpi_name, r.the_geom, d.max as pd_end_point, round(k.avg_samples, 2) AS avg_samples
from t_cell_regions r, kpi_definition d,
(
SELECT cell, kpi_name, avg(kpi_value) AS avg_samples
from t_prop_delay_values
GROUP BY cell, kpi_name
) k
WHERE r.kpi_name = d.kpi_name
AND r.cell = k.cell
AND r.kpi_name = k.kpi_name
ORDER BY r.cell, d.max;
I want to know if it is possible to propagate the indexes from one of those tables to the new table so I do not have to create them all over again.
Maybe this select can help you:
SELECT REPLACE(indexdef, indexname, indexname||1)
FROM pg_indexes
WHERE TABLENAME = 'YOUR_TABLE_NAME';
It will give you a "CREATE INDEX" code with a different name (that is why I have added/concatenated 1 at the end). You are creating a new object so it has to have a different name...
And here is another code where you can also get the "CREATE INDEX" code with the different table name.
SELECT REPLACE(REPLACE(indexdef, indexname, indexname||1), tablename, 'your_tablename')
FROM pg_indexes
WHERE TABLENAME = 'personeel_medspec';

How to check multiple records at once in SQL using where clause?

I have a list of Usernames. How do I check if those usernames already exists in a SQL table column?
For eg:
Select * from tblPerson where Username in ('Jack', 'Jill', 'Mary');
I want to check list of Usernames. About more than 1000 Usernames.
Inserting 1000 Usernames is too time consuming.
I think you're looking for the IN condition. This can check against a list of usernames at once.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Update
If you have a lot of columns, you will want to add an index to column_name. This allows your database to lookup each value of the IN with O(1) time. This will depend on your database, of course. Not all SQL databases are created equal.
You will also want to use bind variables. This allows the database to optimize performance. More info here: https://stackoverflow.com/a/1013959/11352813
If you have a list of usernames, you can use IN instead of =. For example:
select * form tblPerson where Username in ('Jack', 'Jill', 'Alice', 'Bob')
If you have the list of usernames already existing in another table, you can also use the IN operator, but replace the hard coded list of usernames with a subquery.
So for example, if you have another table called tblOtherPerson, with the usernames stored in a column called OtherUsername, you could do:
select * from tblPerson where Username in (select OtherUsername from tblOtherPerson)
The other way (often preferred) is to JOIN the two tables together:
select
tblPerson.*
from
tblPerson
inner join tblOtherPerson
on (tblPerson.Username = tblOtherPerson.OtherUsername)

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 I can save all the names of my tables into a new table from sql?

I have a huge database in Sql-Server and I need to get all the names of the tables into one new table that I have made. This can be done?
I appreciate your help.
The new table has the fields ID, TableName, Status. Id is the identity and status for now will be 1, not null
Use this query below to get all tables name from your database
SELECT name FROM sys.tables
Then you can do a insert query like -
insert into newtable(name) select * from sys.tables

Resources