0

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?

3 Answers 3

1

You need to use aliases in your sub select. For example:

 SELECT 
       [DivisionKeyLineID],
       GETDATE(),
       [DivisionKeyID],
       (SELECT TOP 1 ccd.[Guid] 
          FROM dbo.[CostCenterDefinitions] ccd 
          WHERE 
          ccd.[CostCenterCode] = dkl.[CostCenterCode]),
       [GrantAmount]
  FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] dkl

Without the alias I suspect it is just comparing the costcentrecode in CostCenterDefinitions with itself in your where clause.

Sign up to request clarification or add additional context in comments.

Comments

0

SQL doesn't know which "CostCenterCode" you're referring to... so it's doing a self-equality check with the same column/same row/same table. You need to reference the outside table do to a "correlated subquery". Something like this:

INSERT INTO [dbo].[CostAllocationKeyElements]
           ([Guid]
           ,[Created]
           ,[CostAllocationKeyID]
           ,[CostCenterDefinitionID]
           ,[Amount])
     SELECT 
           c.DivisionKeyLineID,
           GETDATE(),
           c.DivisionKeyID,
           (SELECT TOP 1 Guid from [dbo].CostCenterDefinitions where CostCenterCode = c.CostCenterCode),
           c.GrantAmount
      FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] c
GO

Comments

0

I think you need to set up aliases for your tables so that the subquery knows which CostCenterCode it's looking at in the comparison.

SELECT 
           DivisionKeyLineID,
           GETDATE(),
           DivisionKeyID,
           (SELECT TOP 1 Guid 
              from [dbo].CostCenterDefinitions ccd 
              where 
                  ccd.CostCenterCode = cslst.CostCenterCode),
           GrantAmount
      FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] cslst

If you don't use the table aliases, it's just checking CostCenterCode from CostCenterDefinitions to itself, returning all rows in that table (which you then top 1 to get the same row every time).

1 Comment

I was about to post something snarky, Donal... but our answers are so similar, I realize you must have thought you were editing your own... heh! I've upvoted yours because you beat me to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.