老实说,我并没有真正得到你写的查询。您是否尝试从查询中构建字符串,然后再次将其传递给数据库?
您可以在一个查询中将值从一个数据库传递到另一个数据库:
/*
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