正直なところ、私はあなたが書いたクエリを実際には得ていません。クエリから文字列を作成し、それをデータベースに再度渡そうとしていますか?
1つのクエリで1つのデータベースから別のデータベースに値を渡すことができます。
/*
maybe you need to switch off identity on your target table
to get your original id values into the target table like this:
(without comment ofc ;))
*/
--SET IDENTITY_INSERT TargetDatabase.dbo.Countries ON
INSERT INTO TargetDatabase.dbo.Countries (Id, Name)
SELECT
CountryId, CountryName
FROM SourceDatabase.dbo.Country
--SET IDENTITY_INSERT TargetDatabase.dbo.Countries OFF
または、一時テーブルを使用して、元の値を取得した後でデータベース接続を切り替えることができます。
USE SourceDatabase
DECLARE @TempTable TABLE (CountryId INT PRIMARY KEY, CountryName NVARCHAR(MAX))
INSERT INTO @TempTable (CountryId, CountryName)
SELECT
CountryId, CountryName
FROM Country
USE TargetDatabase
/*
maybe you need to switch off identity on your target table
to get your original id values into the target table like this:
(without comment ofc ;))
*/
--SET IDENTITY_INSERT Countries ON
INSERT INTO Countries (Id, Name)
SELECT
CountryId, CountryName
FROM @TempTable
--SET IDENTITY_INSERT Countries OFF
編集:前のポスターで述べたように、これが機能するには、同じサーバー上に両方のデータベースが必要です。何も言わなかったので、私はそれが事実だと思っていましたか?:D