I would like to insert some data from one database to other with this query:
USE [CostDatabase]
GO
INSERT INTO [dbo].[CostAllocationKeyElements]
([Guid]
,[Created]
,[CostAllocationKeyID]
,[CostCenterDefinitionID]
,[Amount])
SELECT
DivisionKeyLineID,
GETDATE(),
DivisionKeyID,
(SELECT TOP 1 Guid from [dbo].CostCenterDefinitions where CostCenterCode = CostCenterCode),
GrantAmount
FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines]
GO
But the problem is with CostCenterCode, because I must insert Guid into CostCenterDefinitionID field, but in table CSLSTDIVDivisionKeyLines from database TestDB I have got only string code of CostCenterDefinition (CostCenterCode field), so I try to select Guid in subquery but in every row it selects only the same, first Guid from the table. Maybe the same names of columns in diferent databases are reason of that, but I don't think so. Can somebody tell me how can I fix that?