Langsung ke konten utama

Backup of particular tables in SQL Server using T-SQL Script

Questions
I want to take a backup of particular tables available in my database in .bak file And all these should be done using T-SQL script

Answers
Backup Types are dependent on SQL Server Recovery Model. Every recovery model lets you back up whole or partial SQL Server database or individual files or filegroups of the database. Table-level backup cannot be created, there is no such option. But there is a workaround for this



Taking backup of SQL Server table possible in SQL Server. There are various alternative ways to backup a table in sql SQL Server

BCP (BULK COPY PROGRAM)
Generate Table Script with data
Make a copy of table using SELECT INTO
SAVE Table Data Directly in a Flat file
Export Data using SSIS to any destination
I am here explaining only the first one rest of you might be know

Method 1 – Backup sql table using BCP (BULK COPY PROGRAM)

To backup a SQL table named "Person.Contact", which resides in SQL Server AdventureWorks, we need to execute following script, which

 
DECLARE @table VARCHAR(128),
@file VARCHAR(255),
@cmd VARCHAR(512)
SET @table = 'AdventureWorks.Person.Contact' -- Table Name which you want to backup
SET @file = 'C:\MSSQL\Backup\' + @table + '_' + CONVERT(CHAR(8), GETDATE(), 112) -- Replace C:\MSSQL\Backup\ to destination dir where you want to place table data backup
+ '.dat'
SET @cmd = 'bcp ' + @table + ' out ' + @file + ' -n -T '
EXEC master..xp_cmdshell @cmd


Note -

You must have bulk import / export privileges
In above Script -n denotes native SQL data types, which is key during restore
-T denotes that you are connecting to SQL Server using Windows Authentication, in case you want to connect using SQL Server Authentication use -U -P
This will also tell, you speed to data transfer, in my case this was 212468.08 rows per sec.
Once this commands completes, this will create a file named "AdventureWorks.Person.Contact_20120222" is a specified destination folder
Alternatively, you can run the BCP via command prompt and type the following command in command prompt, both operation performs the same activity, but I like the above mentioned method as that’s save type in opening a command prompt and type.

bcp AdventureWorks.Person.Contact out C:\MSSQL\Backup\AdventureWorks.Person.Contact_20120222.dat -n -T


source : https://dba.stackexchange.com/questions/102745/how-can-i-take-backup-of-particular-tables-in-sql-server-2008-using-t-sql-script

Komentar

Postingan populer dari blog ini

Totolink N300RT

Panduan singkat install OpenWrt Totolink N300RT Download FrmUpg :  http://www.anphat.vn/download/FirmwareUpgradeTool.zip Putty : http://www.putty.org/ Frimware totolink v2.1.6 :  http://www.totolink.net/include/download.asp?path=down/010300&file=N300RT-V2.1.6_20160516.zip Bootloder boot96E_32M:  https://drive.google.com/open?id=1p0SreEifzElS_cyngZZRL2lmGnR-Mvvi OpenWrt  :  https://drive.google.com/open?id=1ykv6QkrqXJ_8vnX-6imib44XGFVNJZoA A. Downgrade Frimware Totolink ke v2.1.6 Dalam router dalam keadaan mati tekan Tombol Reset lalu hidupkan power. tunggu sampai lampu indikator lan berkedip. Matikan Firewall dan Buka FrmUpg.exe dan colokan LAN ke Port 1,2,3,4 Di Frimware File pilih file Totolink v2.1.6 dan klik send, Tunggu sampai prosess selesai lalu restart router, cek 192.168.1.1 apakah sudah berhasil downgrade. B. Upgrade OpenWrt Ulangi langkah no 1, Pilih file bootloder boot96E_32M. klik send. tunggu sampai selesa...

How to customize tab colors in SSMS

Applies to ApexSQL Complete Summary This article describes how to set a custom connection color to a query tab in Microsoft  SQL Server Management Studio  (SSMS) using a feature called  Tab coloring  in ApexSQL Complete. Description Tab coloring can set custom connection colors for individual instances of SQL Server, down to the database level. The user has ability to assign SQL servers, and databases to a specific environment to help quickly identify which connection a tab is currently using.

DemoHelper tool buat Coret-coret Layar Komputer

Salah satu tool untuk membatu presentasi.  Sample : Zooming Mode up zooms in down zooms out enter or left mouse click activates drawing of the zoomed area escape cancels zooming mode Zooming is also possible using the mouse wheel. Drawing Mode up/down or Ctrl+Mousewheel increases/decreases the size of the drawing brush right or Mousewheel cycles through the available brush colors 0-9 select the brush colors Shift while drawing forces horizontal/vertical drawing Control while drawing drawing straight lines right mouse-drag draws arrow lines right mouse-drag with control key pressed draws straight lines escape quits drawing mode backspace removes the last line drawn delete removes the first line drawn E removes all drawn lines M toggles the marker on/off T toggles between solid and merged drawing Z select a rectangle to zoom in C clears the screen with the background color