Thursday, February 24, 2011

SQL: Copying A Table From Another Server

Maybe when we build a database or access a table needs to join tables from another server. Then how to copy a table from another server? Here I give you the magical code.

select a.princname as Perusahaan, a.prodgrp as [Grup Product],
  a.prcod as [Code Product], a.Singkatan, a.prdesc as Product,a.uosf as [Isi per Box],
  a.uom as Satuan into MasterProduct
  from openrowset('MSDASQL','Driver={SQL Server};Server=10.126.119.253;UID=sa;PWD=indomarcocoid', pps_product.dbo.cmdmaster) as a



Then how to read that magic code? Lets check it out!
  • a.princname as Perusahaan : which means showed princname column as Perusahaan on label's column; a in front of princname refers to new table that builded.
  • openrowset('providername','accesskey','tablename') : is used to open database and table from another server
This code is for copying a table from your own server but from another database.
select a.*
  from openrowset('MSDASQL','Driver={SQL Server};Server=10.126.119.253;UID=sa;PWD=indomarcocoid',
  pps_product.dbo.cmdmaster) as a

    No comments: