Updating the content of a database with the content of another database - sql

im new to SQL and can't figure out why my sql script isn't working.
I've two databases, and my task is to update a column of a specific table with the content of the same table in the other database if the conditions are met. The tables and columns of both databases have the same names, just party different content. I already looked through a lot of similar questions, but couldn't make it work / figure out what i did wrong.
UPDATE TABLE1
SET COLUMN_1 = Database2.TABLE1.COLUMN_1
WHERE Database2.TABLE1.COLUMN_2 LIKE '%DIN276%';
(Im running the query on the first database)

PostgreSQL database does not support cross-database queries.
You must create in your Database1 a foreighn data wrapper for TABLE1 from Database2, then you can perform queries with your TABLE1 in Database1 together with data from TABLE1 of Database2.

Related

dropping table on mariadb crashes

I am using MariaDB (v10.4.11) and executing sql's via HeidiSQL (v11.3.0.6295).
I have a few tables set up, and I can edit some of them just fine, using either sqls or UI provided by HeidiSQL.
But, two of tables are not editable nor deletable. I tried to edit column names, delete columns, and even delete the entire tables via sqls and ui, but all failed. HeidiSQL crashes, or I have to stop the sql execution manually.
sqls I used are:
UPDATE table1
SET col1 = 'a'
WHERE col2 = 'b';
DROP TABLE table1;
When I execute above sqls on other tables, they work just fine. When I execute the same sqls above on those two tables, HeidiSQL crashes (no response).
I created a backup schema (same tables and same structures), and I could edit all the tables in the backup schema until someone else edited those tables. A co-worker inserted some data into those tables via python, and after that, the same thing happens. I cannot add new columns nor drop tables.
Another co-worker and I inserted into other tables and they are editable.
I looked into processlist and I killed queries that I made. If I kill all the queries (except root), will I be able to edit those tables?

Update with Data from One to another Database

I have two databases: db1 is server, db2 is local.
db2 I updated from the db1 database.
And added 4000 new records to db2.
And I want to update db1 (server) from db2 (local), but some new records have been added to db1 (server).
Now when I want to do Restore Database, some new records from db1 (server) will disappear.
How to make it so that when I update db1 (server) the added some records are not lost and
new 4000 data added from db2 (local)? Can anyone tell me a way or a tool to compare the data?
You need to be able to read/load the data from the local database to your server database.
This can be done via Linked Servers - if the servers are linked you can query data from the db2 from the context of db1.
It will looks like:
SELECT *
FROM [server].[db2].[schema].[table];
If this is not possible I am guessing you have another way to move the data there (via BCP command or maybe just script the table and the data using SSMS from db2 and create it in db1 (4000 rows is not much and you should be OK to just copy the generated SQL).
Now, having the data in one place, you must have a way to identify which rows to insert.
This can be done easily like this:
SELECT *
FROM db2Table
EXCEPT
SELECT *
FROM db1Table
So, if there is row in the db2Table that is not found in the db1Table it will be returned. Then, just insert these rows.
Please, note that EXCEPT with * is comparing all columns. So, if you have a column like ID INT IDENTITY(1,1) you will need to exclude it from the SELECT list.
Also, if you have a way to map the data between the two tables by ID or GUID (a lot of folks are using UNIQUEIDENTIFIER to synch data), you make perform more complex operations. For example:
If the GUID record is missing in the db1 table - insert it
if the GUID record is found in the db1 table - update it

Delete Multiple Table In single SQL Query?

I am using Room Database over SQlite and Live Data in my android application. I need to delete whole table data not database so I am going to delete table in separate Query like:
DELETE From Table_name
But I want to delete multiple tables data not drop in single Query not all tables. So please give me an appropriate solution.

Drop a selection of tables

I'm aware of the commands for dropping a single table or all tables. But in this scenario I need to be able to drop a selection of tables in SQL CE. I know it is possible to do this logic on the application level but I was wondering if it was possible to do this in a single SQL command? I have been unable to find out an answer to this question elsewhere.
I have previously attempted:
drop table table1, table2, table3
But this did not work. Is it possible to do this in a single command?
No, it is not possible in a single statement: http://msdn.microsoft.com/en-us/library/ms173418.aspx

SQL stored procedure failing in large database

I have a particular SQL file in which i copy all contents from on table in a database to another table in another database.
The traditional INSERT statements are used to perform the same operation. However this table has 8.5 Million records and it fails. The queries succeed with a smaller database.
Also in when i run the select * query for that particular table the SQL query express shows out of memory exception.
In particular there is one table that has some many records. So this table alone i want to copy from the old Db to the new Db.
What are alternate ways to achieve this?
Is there any quick work around by which we can avoid this exception and make the queries succeed?
Let me put it this way. Why would this operation fail when there are a lot of records?
I don't know if this counts as "traditional INSERT", but have you tried "INSERT INTO"?
http://www.w3schools.com/sql/sql_select_into.asp