Programing

한 데이터베이스에서 다른 데이터베이스로 테이블 값을 삽입하는 방법은 무엇입니까?

lottogame 2020. 10. 26. 07:38
반응형

한 데이터베이스에서 다른 데이터베이스로 테이블 값을 삽입하는 방법은 무엇입니까?


대상 테이블이 이미 존재하는 경우 쿼리에서 한 테이블의 레코드를 다른 데이터베이스의 다른 테이블에 삽입하고 싶습니다. 테이블 끝에 레코드를 추가해야합니다.


이것은 어떤가요:

USE TargetDatabase
GO

INSERT INTO dbo.TargetTable(field1, field2, field3)
   SELECT field1, field2, field3
     FROM SourceDatabase.dbo.SourceTable
     WHERE (some condition)

한 서버 / 데이터베이스에서 다른 데이터베이스로 테이블 값을 삽입하는 방법은 무엇입니까?

1 연결된 서버 만들기 {필요한 경우} (SQL 서버 2008 R2-2012) http://technet.microsoft.com/en-us/library/ff772782.aspx#SSMSProcedure

2 자격 증명을 사용하도록 연결된 서버 구성 a) http://technet.microsoft.com/es-es/library/ms189811(v=sql.105).aspx

EXEC sp_addlinkedsrvlogin 'NAMEOFLINKEDSERVER', 'false', null, 'REMOTEUSERNAME', 'REMOTEUSERPASSWORD'

-서버 확인

SELECT * FROM sys.servers

-연결된 서버 테스트

EXEC sp_testlinkedserver N'NAMEOFLINKEDSERVER'

새 로컬 테이블에 삽입

SELECT * INTO NEWTABLE
FROM [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE

또는

원격 테이블에 새로운 가치로 삽입

INSERT
INTO    [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE
SELECT  *
FROM    localTABLE

새로운 로컬 테이블 값으로 삽입

INSERT
INTO    localTABLE
SELECT  *
FROM    [LINKEDSERVER\INSTANCE].remoteDATABASE.remoteSCHEMA.remoteTABLE

다음은 빠르고 쉬운 방법입니다.

CREATE TABLE database1.employees
AS
SELECT * FROM database2.employees;

    --Code for same server
USE [mydb1]
GO

INSERT INTO dbo.mytable1 (
    column1
    ,column2
    ,column3
    ,column4
    )
SELECT column1
    ,column2
    ,column3
    ,column4
FROM [mydb2].dbo.mytable2 --WHERE any condition

/*
steps-
    1-  [mydb1] means our opend connection database 
    2-  mytable1 the table in mydb1 database where we want insert record
    3-  mydb2 another database.
    4-  mytable2 is database table where u fetch record from it. 
*/

--Code for different server
        USE [mydb1]

    SELECT *
    INTO mytable1
    FROM OPENDATASOURCE (
            'SQLNCLI'
            ,'Data Source=XXX.XX.XX.XXX;Initial Catalog=mydb2;User ID=XXX;Password=XXXX'
            ).[mydb2].dbo.mytable2

        /*  steps - 
            1-  [mydb1] means our opend connection database 
            2-  mytable1 means create copy table in mydb1 database where we want 
                insert record
            3-  XXX.XX.XX.XXX - another server name.
            4-  mydb2 another server database.
            5-  write User id and Password of another server credential
            6-  mytable2 is another server table where u fetch record from it. */

당신은 시도 할 수 있습니다

Insert into your_table_in_db1 select * from your_table_in_db2@db2SID 

db2SID is the sid of other DB. It will be present in tnsnames.ora file


INSERT
INTO    remotedblink.remotedatabase.remoteschema.remotetable
SELECT  *
FROM    mytable

There is no such thing as "the end of the table" in relational databases.


If both the tables have the same schema then use this query: insert into database_name.table_name select * from new_database_name.new_table_name where='condition'

Replace database_name with the name of your 1st database and table_name with the name of table you want to copy from also replace new_database_name with the name of your other database where you wanna copy and new_table_name is the name of the table.


Just Do it.....

( It will create same table structure as from table as to table with same data )

 create table toDatabaseName.toTableName as select * from fromDatabaseName.fromTableName;

참고URL : https://stackoverflow.com/questions/3502269/how-to-insert-table-values-from-one-database-to-another-database

반응형