Copy Data from a table in one Database to another database - database

Basically I have a two databases on SQL Developer. I want to take the table data FOR A PARTICULAR RECORD from one database and copy it to another database's table. What should be the query? I don't want to use a restore to avoid data loss... Any ideas?
I got a query from google:
INSERT INTO dbo.ELLIPSE_PFPI.T_ANTENNE
(COLUMNS)
SELECT COLUMNS_IN_SAME_ORDER FROM dbo.ELLIPSE_PFPI.T_ANTENNE
What should be written in the query instead of dbo?

Try this
I havent tested it but i think it works
select * into [databaseTo].dbo.tablename from [databaseFrom].dbo.tablename

Related

Copy tables into database

I imported data from Power BI into SQL-Server. You can see how is look like imported data.
Additionally I created own database with commands below:
CREATE DATABASE MY_DW
GO
USE MY_DW
GO
Now I want to copy all this table into my base named as MY_DW. So can anybody help me how to solve this problem and copy all tables into my base ?
Please check https://www.sqlshack.com/how-to-copy-tables-from-one-database-to-another-in-sql-server/.
This link suggests various methods to copy the data tables from one database to another.
Thanks,
Rajan
Following approach could resolve your issue:
Imported Database
Generate Scripts
Introduction
Next button
Select the database objects (Tables in your case) to script
Next button
Specify how scripts should be saved
Advanced -> Types of data to script -> Schema and data
Next button
Review your selections
Next button
Script generation would take place and saved which you should run under the database,
MY_DW, you created
Another approach:
Assuming that the databases are in the same server.
The below query will create the table into your database(without constraints).
SELECT * INTO MY_DW.Table_Name
FROM ImportedDB.Table_Name
And the below query will insert the data into your database table.
INSERT INTO MY_DW.Table_Name
SELECT * FROM ImportedDB.Table_Name
Final approach:
Assuming that the databases are in the linked server.
Incase of linked server, four part database object naming convention will be applied like below.
The below query will create the table into your database(without constraints).
SELECT * INTO [DestinationServer].[MY_DW].[dbo].[Table_Name]
FROM [SourceServer].[ImportedDB].[dbo].[Table_Name]
And the below query will insert the data into your database table.
INSERT INTO [DestinationServer].[MY_DW].[dbo].[Table_Name]
SELECT * FROM [SourceServer].[ImportedDB].[dbo].[Table_Name]

Remove duplicates from a SQL server rows using DISTINCT

I need to remove SQL server duplicated rows when importing file into database with distinct method.
HallGroup is my table in database. I'm using this
Sql procedure:
SELECT DISTINCT * INTO tempdb.dbo.tmpTable
FROM HallGroup
DELETE FROM HallGroup
INSERT INTO HallGroup SELECT * FROM tempdb.dbo.tmpTable
DROP TABLE tempdb.dbo.tmpTable
With this procedure works fine duplicated rows are deleted, but the problem is when i try to import again data to SQL server rows are still duplicating. What i'm missing, So any hint?
How to remove SQL server duplicated rows properly when importing file into database with distinct method?
I am just getting back into SQL after being out for a bit but I would not have solved your problem in that way that you are trying (not that I completely understand why you are doing it that way) as I believe (even if it were working correctly) over time your process will take longer each time you do it as the size of the table increases.
It would be much more efficient if you inserted the new data based on the absence of a key (you indicate you are already using a stored proc). If you don't have a key to use (which very recently happened to me), make one. I just solved a similar problem to yours whereas I am importing data into a table from an external source and wanted to eliminate the possibility of duplicates. In my case, I associate name of the external source datafile (is distinct by dataset to import) with the data to be imported and use that to ensure I am not re-importing already imported data. I load the external data into a table using a dtsx and then run a stored proc to merge that data with an existing table. This gives me the added advantage of having a audit trail of where each record came from.
Hope this helps.

MSSQL Injection extracting column data from another database

I setup a test-bed application vulnerable to mssql injection and i wondered, how do i extract column data from another database? To extract column data from current database we do:
convert(int,(select columnnamegoeshere from tablenamegoeshere))--
and then to enumerate the other column data we do:
convert(int,(select columnnamegoeshere from tablenamegoeshere where columnnamegoeshere not in ('firstentryfromcolumn')))--
But if it's not inside the default database and we want to extract column data from another database, how do we do that? Thanks.
I would do a join... but, to keep it simple for you, here's your code using a different database column:
convert(int,(select columnnamegoeshere from tablenamegoeshere where columnnamegoeshere not in (select top 1 firstentryfromcolumn from otherdb.dbo.otherdbtable)))
It would be better to join the 2 tables together and exclude the records if that's possible... subqueries are usually slower and not the best way to go about it.

Export large amounts of binary data from one SQL database and import it into another database of the same schema

I have one database with an image table that contains just over 37,000 records. Each record contains an image in the form of binary data. I need to get all of those 37,000 records into another database containing the same table and schema that has about 12,500 records. I need to insert these images into the database with an IF NOT EXISTS approach to make sure that there are no duplicates when I am done.
I tried exporting the data into excel and format it into a script. (I have doe this before with other tables.) The thing is, excel does not support binary data.
I also tried the "generate scripts" wizard in SSMS which did not work because the .sql file was well over 18GB and my PC could not handle it.
Is there some other SQL tool to be able to do this? I have Googled for hours but to no avail. Thanks for your help!
I have used SQL Workbench/J for this.
You can either use WbExport and WbImport through text files (the binary data will be written as separate files and the text file contains the filename).
Or you can use WbCopy to copy the data directly without intermediate files.
To achieve your "if not exists" approache you could use the update/insert mode, although that would change existing row.
I don't think there is a "insert only if it does not exist mode", but you should be able to achieve this by defining a unique index and ignore errors (although that wouldn't be really fast, but should be OK for that small number of rows).
If the "exists" check is more complicated, you could copy the data into a staging table in the target database, and then use SQL to merge that into the real table.
Why don't you try the 'Export data' feature? This should work.
Right click on the source database, select 'Tasks' and then 'Export data'. Then follow the instructions. You can also save the settings and execute the task on a regular basis.
Also, the bcp.exe utility could work to read data from one database and insert into another.
However, I would recommend using the first method.
Update: In order to avoid duplicates you have to be able to compare images. Unfortunately, you cannot compare images directly. But you could cast them to varbinary(max) for comparison.
So here's my advice:
1. Copy the table to the new database under the name tmp_images
2. use the merge command to insert new images only.
INSERT INTO DB1.dbo.table_name
SELECT * FROM DB2.dbo.table_name
WHERE column_name NOT IN
(
SELECT column_name FROM DB1.dbo.table_name
)

How to fetch last added or edited records from SQL Server database?

Currently I'm working on database migration, for this I'm using Pentaho Kettle and Perl scripts.
The migration is from Tumor-registry SQL Server database to CIDER IBM DB2 database.
In this task I want to achieve two objective.
Initial migration: in this I'm migrating all the rows (e.g. 100000) from Tumor-registry (SQL Server) to CIDER (IBM DB2).
Subsequent migration: the Tumor-registry SQL Server database is constantly updating on and off.
It's constantly adding new rows or edits already existing rows.
I have figured out the first step but facing two problems in second step.
a) If Tumor-registry SQL Server database is updated with for example new 10 rows; how can I get those new 10 rows?
b) If already existing 10 rows are updated then how can I get those 10 rows and also want to know which columns are updated.
My Tumor-registry database contain approximately 50 tables.
Any help is greatly appreciated.
Create a new column with TIMESTAMP datatype.This will keep track of the latest edited records in the table.
OR
You can use CHECKSUM function in Sql server.
i think that will provide u a solution by using triger
create triger trigername on tablename
after insert
as
declare #variablename_1 datatype;
select #variablename_1 = column_name from inserted ;
if you want save data in anohter table that last inserted then create an other table
insert into tablename values (#variablename_1);
You could use IBM Change Data Capture, it will take all the DDL and DML in the source database, and replicate them appropiately in the target database.
http://www-01.ibm.com/software/data/infosphere/change-data-capture/
It seems that there are other solutions from other vendors, take a look at: http://en.wikipedia.org/wiki/Change_data_capture

Resources