For example I have one SQL Server database that contains a table
Id Address
1 England,London,someaddress
2 Germany,Berlin,someaddress2
And I have another SQL Server database that contains following table with scheme
Id Country City Address
I need transfer data from first database to second. Like this.
id Country City Address
1 England London someaddress
2 Germany Berlin someaddress2
How can I do that?
You can do that by creating a User Define Function
that detect Country
, City
, Address
by Splitting and than insert into second table.
Split Function :
CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (id int , items varchar(8000))
as
begin
declare @idx int
declare @id int
declare @slice varchar(8000)
set @id = 1
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
begin
insert into @temptable(id, Items) values( @id , @slice)
set @id = @id + 1
end
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
then use in Insert Query like this :
INSERT INTO NewTbl
SELECT .... ,
(SELECT ITEMS FROM dbo.Split(Address) where id = 1) as Country,
(SELECT ITEMS FROM dbo.Split(Address) where id = 2) as City,
(SELECT ITEMS FROM dbo.Split(Address) where id = 3) as Address,
...
There are a few ways to actually move the data.
1.) Linked Server http://msdn.microsoft.com/en-us/library/ms188279.aspx
2.) OpenRowSet http://msdn.microsoft.com/en-us/library/ms190312.aspx
3.) Replication http://msdn.microsoft.com/en-us/library/ms151198.aspx
Depending on your needs you need to choose the best one for you.
If you want the data from your second database to be accessable in your original databse, in the same way as it's own data, for queries, stored procedures...etc have a look at Linked Servers.
If you want a one off access to data have a look at OpenRowSet.
If you have the same table on both databases and want to keep that data in sync automatically I would look at Replication. You can set up your own replication stored procedures that can perform custom logic, like the split.
You then can impliment the split as noted in the other answer to split your data.
If you want to copy data from one server to another database server .then you must make linked server between them.
and execute like following query.
insert into [127.234.11.50].[cgwssitws].dbo.tbl_ws_hotelier(paramid,hotelid,paramname,paramvalue)
select paramid,hotelid,paramname,paramvalue from [126.32.22.30].[CGWS_Hotel].dbo.tbl_param where hotelid='22'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.