copy table from one SQL server 2005 to another SQL Server 2005 table - sql

I wish to copy a single table where a particular row has a value of '76'. I then need to copy this table to another server in a separate domain.
I tried to use the export tool, but I can't restrict it to rows that have a value of 76 only
What's the best way to go about it?

Right Click your database:
Your server and database is already selected.
The easiest way if its not a regular thing then you could probably create a file.
Lets say abc.txt on your desktop.
Choose the flat file destination and then select the file.abc.txt in this case.
Format your wish. I like to use delimited and text qualifier " and since there are no column names and no data in our file uncheck the column names in the first row. Next select write a query to specify the data to transfer.
Lets assume you have a table TblUsers with columns username, password, value.
your query will be:
Select * from tblUsers where value = '76'
Next (Make changes if you wish, I like to leave the defaults) > Click edit mappings > Next > Finish!
Then go to your destination server and database and then do almost the same thing but import.
Thats it!

Related

Backup a single table with data in SQL Server [duplicate]

I want to get a backup of a single table with its data from a database in SQL Server using a script.
How can I do that?
SELECT * INTO mytable_backup FROM mytable
This makes a copy of table mytable, and every row in it, called mytable_backup. It will not copy any indices, constraints, etc., just the structure and data.
Note that this will not work if you have an existing table named mytable_backup, so if you want to use this code regularly (for example, to backup daily or monthly), you'll need to run drop mytable_backup first.
You can use the "Generate script for database objects" feature on SSMS.
Right click on the target database
Select Tasks > Generate Scripts
Choose desired table or specific object
Hit the Advanced button
Under General, choose value on the Types of data to script. You can select Data only, Schema only, and Schema and data. Schema and data includes both table creation and actual data on the generated script.
Click Next until wizard is done
There are many ways you can take back of table.
BCP (BULK COPY PROGRAM)
Generate Table Script with data
Make a copy of table using SELECT INTO, example here
SAVE Table Data Directly in a Flat file
Export Data using SSIS to any destination
You can create table script along with its data using following steps:
Right click on the database.
Select Tasks > Generate scripts ...
Click next.
Click next.
In Table/View Options, set Script Data to True; then click next.
Select the Tables checkbox and click next.
Select your table name and click next.
Click next until the wizard is done.
For more information, see Eric Johnson's blog.
Put the table in its own filegroup. You can then use regular SQL Server built in backup to backup the filegroup in which in effect backs up the table.
To backup a filegroup see:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-files-and-filegroups-sql-server
To create a table on a non-default filegroup (its easy) see:
Create a table on a filegroup other than the default
Another approach you can take if you need to back up a single table out of multiple tables in a database is:
Generate script of specific table(s) from a database (Right-click database, click Task > Generate Scripts...
Run the script in the query editor. You must change/add the first line (USE DatabaseName) in the script to a new database, to avoid getting the "Database already exists" error.
Right-click on the newly created database, and click on Task > Back Up...
The backup will contain the selected table(s) from the original database.
To get a copy in a file on the local file-system, this rickety utility from the Windows start button menu worked:
"C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\DTSWizard.exe"

Copy data from one data base to another database

I have tow database server
Server: a , Database name: FMIS , table name: employee
Server: b, Database name : KPS, table name: employee.
I need a SQL command which which will copy data from employee table of FMIS to employee table of KPS.
I have written a formula like below, but its not working.
insert into a.fmis.employee(Column1, Column2,...)
select Column1, Column2,...
from b.kps.employee
what will be the correct SQL command?
If they were on the same server then it would be easy to copy from one database to the other, but since they aren't you'll need to transfer it somewhat manually.
You should be able to use
SHOW CREATE TABLE
to get the table structure, and
SELECT * FROM tablename INTO OUTFILE '/path/to/save.tsv'
(make sure the file does NOT exist and the user has FILES permissions) to export the data.
Then copy this information to the other server.
You can just run the CREATE TABLE you got from the first step, and then use
LOAD DATA INFILE 'path/to/save.tsv' INTO TABLE tablename
to import the data. Note that the target table name need not be the same as the original.
You can add your server b as a linked server on server a and run your query that you have written. This link might help https://msdn.microsoft.com/en-in/library/ff772782.aspx

How can I extract the value of a varbinary(max) column?

I have a varbinary(max) column that is storing images in an SQL database.
I am working on a newdb script, where an application creates a new instance of the db and populates a few of the tables. One of those tables I am working on is initializing that image column.
In order to do this, I printed the contents of the column using a select statement and pasted the content into the insert statement of the newdb script. This appeared to work initially, but the image didn't load correctly.
So I compared the DATALENTH() of the original data (5469988) and the new data (21839). It appears the Microsoft SQL Server management Studio - 2014 cut off the data why I copied it from the original db at a certain point. I need to be able to get the entire content of the column. Any ideas?
select cast(convert(varchar(max), VarBinaryMaxColumn, 1) as xml) from Table
Instead of copying/pasting, right-click on the results and do 'Save Results As...', and that should export the full contents. Funny thing is setting the query output to text or file explicitly will still truncate long data values.
If you copy and paste your limited to the query result options. Mostly columns will be cut of after a certain lenght (often 256 chars).
You can select in the top bar "save result as..." which will prompt you an dialog for data export.
You can use the data export wizard too.

copy tables with data to another database in SQL Server 2008

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

Transfer data from one database to another database

How to fetch the data from one database and insert in to another database table? I can't to do this. Please help me in transferring data from one to another.
There are several ways to do this, below are two options:
Option 1
- Right click on the database you want to copy
Choose 'Tasks' > 'Generate scripts'
'Select specific database objects'
Check 'Tables'
Mark 'Save to new query window'
Click 'Advanced'
Set 'Types of data to script' to 'Schema and data'
Next, Next
You can now run the generated query on the new database.
Option 2
Right click on the database you want to copy
'Tasks' > 'Export Data'
Next, Next
Choose the database to copy the tables to
Mark 'Copy data from one or more tables or views'
Choose the tables you want to copy
Finish
Example for insert into values in One database table into another database table running on the same SQL Server
insert into dbo.onedatabase.FolderStatus
(
[FolderStatusId],
[code],
[title],
[last_modified]
)
select [FolderStatusId], [code], [title], [last_modified]
from dbo.Twodatabase.f_file_stat
For those on Azure, follow these modified instructions from Virus:
Open SSMS.
Right-click the Database you wish to copy data from.
Select Generate Scripts >> Select Specific Database Objects >> Choose the tables/object you wish to transfer.
strong text
In the "Save to file" pane, click Advanced
Set "Types of data to script" to Schema and data
Set "Script DROP and CREATE" to Script DROP and CREATE
Under "Table/View Options" set relevant items to TRUE. Though I recommend setting all to TRUE just in case. You can always modify the script after it generates.
Set filepath >> Next >> Next
Open newly created SQL file. Remove "Use" from top of file.
Open new query window on destination database, paste script contents (without using) and execute.
if both databases are on same server and you want to transfer entire table (make copy of it) then use simple select into statement ...
select * into anotherDatabase..copyOfTable from oneDatabase..tableName
You can then write cursor top of sysobjects and copy entire set of tables that way.
If you want more complex data extraction & transformation, then use SSIS and build appropriate ETL in it.
You can use visual studio 2015. Go to Tools => SQL server => New Data comparison
Select source and target Database.
You can backup and restore the database using Management Studio.
Again from Management Studio you can use "copy database".
you can even do it manually if there is a reason to do so. I mean manually create the target db and manually copying data by sql statements...
can you clarify why you ask this? Is it that you dont have expierience in doing it or something else?
There are multiple options and depend on your needs.
See the following links:
Copying Data Between Servers
Copy tables from one database to another in SQL Server.
These solutions are working in case when target database is blank.
In case when both databases already have some data you need something more complicated http://byalexblog.net/merge-sql-databases
This can be achieved by a T-SQL statement, if you are aware that FROM clause can specify database for table name.
insert into database1..MyTable
select from database2..MyTable
Here is how Microsoft explains the syntax:
https://learn.microsoft.com/en-us/sql/t-sql/queries/from-transact-sql?view=sql-server-ver15
If the table or view exists in another database on the same instance of SQL Server, use a fully qualified name in the form database.schema.object_name.
schema_name can be omitted, like above, which means the default schema of the current user. By default, it's dbo.
Add any filtering to columns/rows if you want to. Be sure to create any new table before moving data.
Doing this programmatically between two different databases could involve a scheduled job using a linked server. But linked servers require DBA-level knowledge to set up. If you can't use a linked server, just write a program that 1. Reads a row from the source table and 2. Inserts it into the target table. The programmer just needs to use a connection string that has INSERT privileges into the target database table. I have solved this problems using both approaches.