Thursday, January 13, 2022

.::: Insert Into table from Linkedserver to Linkedserver with Open Query & dbname(database_id) :::.

A. Linked Server (MSSQL)

1. Check Database ID on Linked Server
select @@Servername as LocalServer,@@servername as ServerName_Link, name as dbname,database_id  from sys.databases


2. Check Result Query on Linked Server
SELECT @@Servername as LocalServer, @@Servername as ServerName_Link,db_name('5')  as [DBName], * from teguhth.dbo.barang



B. LocalServer
1. Check Database ID Linked Server from LocalServer

select @@servername as LocalServer, * from openquery(labvm,'select @@servername as ServerName_Link, name as dbname,database_id  from sys.databases');

2. Check Result Query Linked Server from Local Server

select @@servername as LocalServer, * from openquery(labvm,'SELECT @@Servername as ServerName_Link,db_name(''5'')  as [DBName], * from teguhth.dbo.barang');


C. Create Database n Table on LocalServer
1. create database dbatool & table barang_resume
create database dbatool
go;

use dbatool
go;

CREATE TABLE [dbo].[barang_resume](
    [LocalServer] [nvarchar](128) NULL,
    [ServerName_Link] [nvarchar](128) NULL,
    [DBName] [nvarchar](128) NULL,
    [KODE_BARANG] [char](6) NOT NULL,
    [NAMA_BARANG] [varchar](25) NULL,
    [SATUAN_BARANG] [varchar](20) NULL,
    [STOK_BARANG] [decimal](4, 0) NULL
) ON [PRIMARY]
GO


2. check result create database & table
select * from INFORMATION_SCHEMA.TABLES;
exec sp_columns barang_resume;
SELECT  * from dbatool.dbo.barang_resume


D. Execute

1. Insert table, row from Linkedserver to LocalServer
insert into dbatool.dbo.barang_resume select @@servername as LocalServer, * from openquery(labvm,'SELECT @@Servername as ServerName_Link,db_name(''5'')  as [DBName], * from teguhth.dbo.barang');




2. check result on Localserver
SELECT  * from dbatool.dbo.barang_resume;
SELECT @@SERVERNAME , * from dbatool.dbo.barang_resume;

 


No comments:

Post a Comment

Popular Posts