Can I do a INSERT using select from another DB with the same table name - sql

I have a person table (sqlServer 2008r2) and are using .vbs and .bat files to sync some data from a table called person in database A to database B.
Note - The DB names are different but the table names are the same. Because the peson table has 137 fields I am looking for a way to write both an INSERT and an UPDATE statement. How can I do thsi without listing 137 fields?
At the moment I connect to database A and populate a recordSet called RS with the people table records.
Then I loop through RS and query the people table in database B
If found I update people table in database B with the people table record from database A
If not found I insert the people record from database A into the people table on database B
Now this is nice and easy but since it has 137 fields I do not want to write a MASSIVE update and insert statements. DO I have another option such as:
The table structures are identical between people on database A and people on database B but the recordset obtained in step 1 above is using one one DB connection and the query in step 2 is using a separate DB connection to a different DB instance on a different server.
Help.

You can do that by using a fully qualified name.
insert into DATABASE1.dbo.Person (columns)
select (columns) from DATABASE2.dbo.Person
Also there are others way to pump data from a database for another
You can take a look on your MSSMS it got a tool for that kind of thing.
Select one database and right button mouse to open the import/export wizard.
You can also take a look on replication, bulk copy, service broker, etc...

Related

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

Trying to figure out how a table in SQL Server is getting populated

There is no documentation and no way to get in touch with the architects of the table. Are there any queries or options in SSMS that allows me to know what the table is connected to ?
A brief explanation of my issue is that there are two tables that seem to be connected.
Table A which contain 10M distinct users and Table B 5M users. All users in Table B are in Table A but obviously Table A's users aren't in Table B. After running multiple tests on the website, I cant find the trigger that copies the user information from Table A to Table B. And that's why I would like to know how Table B gets populated.
Like Diado already said, try running a profiler for a while. It will log queries against the database (e.g. INSERT ... and UPDATE ...), the source of the connection (e.g. IP address), and sometimes the process name. That could provide clues into what's interacting with your table in question.
https://learn.microsoft.com/en-us/sql/tools/sql-server-profiler/sql-server-profiler?view=sql-server-2017

Deleting objects from SQL Server

In SQL Server Database Engine I have a table named Table A.
I deleted the table using graphical interface, but when I wanted to create a table with same name, the error shows
The object already exists
What is the remedy of this situation?
The following steps should help you track down what is going on and help you create your table:
Right-click on your database and select refresh
Verify that your table does not exist under this database.
If you table is
not shown here, then very likely your table is displayed under the
master database.
To create a table in your selected database,
first select the database and then run your query.
A better
option for number 4, just to be sure you are specifying the correct
database is to run the command use dbname; (where dbname is
the name of your database). Do this on the line above your create table code.

SQL Server bulk insert for large data set

I have 1 million rows of data in a file, I want to insert all the records into SQL Server. While inserting I am doing some comparison with existing data on the server, if the comparison satisfied I will update the existing records in the server or else I will insert the record from the file.
I'm currently doing this by looping from C#, which consume more than 3 hours to complete the work. Can anyone suggest idea to improve the performance?
Thanks,
Xavier.
Check if your database in Full or Simple recovery mode:
SELECT recovery_model_desc
FROM sys.databases
WHERE name = 'MyDataBase';
If database is SIMPLE recovery mode you can create a staging table right there. If it is in Full mode then better create Staging table in separate database with Simple model.
Use any BulkInsert operation/tool (for instance BCP, as already suggested)
Insert only those data from your staging table, which do not exist in your target table. (hope you know how to do it)

Data Agent - SELECT from one table and insert into another

Is there any type of product where I can write a SQL statement to select from one table and then insert into another database (The other database is out in the cloud). Also, it needs to be able to check to see if that record exists and then update the row if anything has changed. Then it will need to run every 10-30 minutes to check to see what has changed or if new records have been added.
The source database and the ending database have a different schema (if that matters?) I've been looking, but it seams that only products out there are ones that will just copy one table and insert into a table with the same schema.