I have a SQL DB with tables that are created and updated programmatically.
What is the best approach to creating a new table from all the tables in my DB? The tables will be dynamic and are all the same columns.
I'm assuming Union with a Foreach, but want to make there isn't a better way.
I have only created the powershell to import the CSVs to my DB so far.
Related
I am still learning Oracle SQL, and I've been trying to find the best way to update/insert records in an OracleSQL table with the data from a CSV file.
So far, I've figured out how to load the csv into a temporary table using External Tables in Oracle, but I'm having difficulty finding a detailed guide on how to update/insert (UPSERT) the loaded data into an existing table.
What is the best way to do this, when I have 30+ fields in the table? For example, is it best to read the csv line by line with something like pandas and update each record one by one, or is it best to do it with a sql script using something like a merge statement? Not all records in the csv have a value for the primary key, in which case I need to insert rather than update. Thanks for the help!
That looks like a MERGE, indeed.
Data from external table would then be used to
update values in existing rows
create new rows in the target table
Pandas and row-by-row processing? I wouldn't do that. If you already have a powerful database, then use its capabilities. Row-by-row is usually slow-by-slow and there's rarely some benefit in doing it that way.
I want to copy and rename 100+ tables in a SQL Server database, retaining all their columns and data.
I am cloning a website and want to keep the database information all on the same database.
For example, say I have 100 tables that begin with the prefix "foo" and I want to create new tables with the prefix "anotherfoo" that contain all the same data.
How do can I do this in the most efficient way possible?
I have a number of .db files that I'd like to merge into one.
Each database has four tables.
'Associated'
'Candidate'
'Picks'
'Picks_modified'
The tables might be empty in some of the files. I would like to merge these files using python2.7.
Thanks in advance for your help,
Antoine
This is a general SQLite solution which should work for you in Python as well. Assuming you had three databases, and you wanted the contents of the second and third tables in the tables of the first database, then you could try this:
ATTACH 'database1.db' AS db1;
ATTACH 'database2.db' AS db2;
ATTACH 'database3.db' AS db3;
INSERT INTO db1.Associated
SELECT * FROM db2.Associated
UNION ALL
SELECT * FROM db3.Associated
UNION ALL
...
Repeat the above for the other three tables in your databases (Candidate, Picks, Picks_modified).
In other words, we can insert the records from the other databases into the tables of the first database. If you wanted to aggregate everything in a different, perhaps new, database, then you can easily modfiy the above code to handle this.
I've seen a few questions like this asked but can't seem to find a good solution. Let's say I have a dataset with 20 tables (all with same schema just different row data). I have a stored procedure to populate a single sql table with the data from these tables but currently the only way I've been able to do this is to iterate through the tables in the dataset and send to sql via the stored prodcedure one at a time until all of them have made it to the target table on sql.
Is there a way to do this without iterating through each table in the dataset but still using a stored procedure?
I have to copy the tables with data from one database to another using Query. I know how to copy tables with data within a database. But I was not sure about how to do the same for copying between two databases.
I have to copy huge number of tables, so I need any fast method using query...
Anybody please help out...Thanks in advance...
You can use the same way to copy the tables within one database, the SELECT INTO but use a fully qualified tables names database.schema.object_name instead like so:
USE TheOtherDB;
SELECT *
INTO NewTable
FROM TheFirstDB.Schemaname.OldTable
This will create a new table Newtable in the database TheOtherDB from the table OldTable whih belongs to the databaseTheFirstDB
Right click on the database, select tasks and click on Generate Scripts.
In the resultant pop-up, choose options as required (click advanced), to drop and create table, drop if exists, etc.
Scroll down and choose "Schema and Data" or "Data Only" or "Types of data to script (2008 R2)"as required.
Save to file and execute on the destination DB.
Advantages -
Can be executed against the destination DB, even if it is on another server / instance
Quickly script multiple tables, with data as needed
Warning - Might take quite a while to script, if the tables contain a large amount of data.
Rajan
INSERT INTO DB2.dbo.MyOtherTable (Col0, Col1)
SELECT Col0, Col1 FROM DB1.dbo.MyTable
Both table column's must have same data types..
Below SQL Query will copy SQL Server table schema & data from one database to another database. You can always table name (SampleTable) in your destination database.
SELECT * INTO DestinationDB.dbo.SampleTable FROM SourceDB.dbo.SampleTable