Sqlserver import data with the identity attribute

Source: Internet
Author: User
The data import tool provided by sqlserver has two disadvantages:
1. database replication will replicate all the data in all tables. Some large tables do not need to be imported and cannot be controlled.
2. Data Import. When there is an identity restriction in the table, you can only cancel the settings for one table, and then set the settings to go back after the import.
Today, I wrote an SQL statement to import data from some tables.
Some knowledge points are used:
1. Create a linked server
2. Set the pluggable attribute of the identity column
3. A cursor with Parameters

Create a linked server
Exec sp_addmediaserver
@ Server = 'dbvip ',
@ Srvproduct = '',
@ Provider = 'sqloledb ',
@ Datasrc = '58. 1.2.3'

Exec sp_add1_srvlogin
'Dbvip ',
'False ',
Null,
'Sa ',
'Sapwd'

// Generate the source database script and create a new database on the target server. This is the target database.
The structure is the same as that of the source database.

// Create a temporary table to import the name of the table containing the identity attribute column to the temporary table
// The identity column is determined based on colstat = 1 and is not necessarily accurate. No data has been found.
Create Table table_tmp (table_name nvarchar (50 ))
Insert into table_temp
Select name from sysobjects where ID in (
Select ID from syscolumns
Where ID in (select ID from sysobjects
Where type = 'U') and colstat = 1) Order by name

// Define a cursor to obtain all tables to export data
Declare cur_tab cursor for select table_name from table_tmp
Declare @ name nvarchar (40)
Declare @ SQL nvarchar (2000)
Begin
Open cur_tab;
Fetch cur_tab into @ name;
While (@ fetch_status = 0)
Begin
// Set the identity_insert attribute of the table to on and prepare to import data.
Set @ SQL = 'set identity_insert '+ @ name + 'on ;';
Print (@ SQL );
-- Exec (@ SQL );
// Declare a cursor with parameters to retrieve all columns of the current table, because when the table has an identity column,
// You must write all the columns in the table before importing.
Exec ('
Declare cur_col cursor
For select name from syscolumns
Where id = (
Select ID from sysobjects
Where type = ''u''
And name = ''' + @ name + ''')');
Declare @ Col nvarchar (20 );
Declare @ in_ SQL nvarchar (2000 );
Declare @ in_col nvarchar (1000 );
Set @ in_col = '';
Open cur_col;
Fetch cur_col into @ Col;
While (@ fetch_status = 0)
Begin
Set @ in_col = @ in_col + @ Col + ',';
Fetch cur_col into @ Col;
End;
Set @ in_col = substring (@ in_col, 1, Len (@ in_col)-1 );
Set @ in_ SQL = 'insert into '+ @ name +' ('+ @ in_col +') Select '+ @ in_col +' from dbvip. src_db.dbo. '+ @ name;
Print (@ in_ SQL );
-- Exec (@ in_ SQL );
Close cur_col;
Deallocate cur_col
Set @ SQL = 'set identity_insert '+ @ name + 'off ;';
Print (@ SQL );
-- Exec (@ SQL );
Fetch cur_tab into @ name;
End;
Close cur_tab;
Deallocate cur_tab
End;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.