T-SQL 使用子查询从另一个数据库插入 select
T-SQL insert into select from another database with subquery
我想通过以下查询将一些数据从一个数据库插入到另一个数据库:
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
但问题出在 CostCenterCode 上,因为我必须将 Guid 插入 CostCenterDefinitionID 字段,但是在 table 来自数据库 TestDB 的 CSLSTDIVDivisionKeyLines 中,我只有 CostCenterDefinition 的字符串代码(CostCenterCode 字段),所以我尝试 select 子查询中的 Guid,但在每一行中它 select 只是相同的,第一个来自 table 的 Guid。也许不同数据库中的列名相同是原因,但我不这么认为。谁能告诉我如何解决这个问题?
您需要在您的子程序中使用别名select。例如:
SELECT
[DivisionKeyLineID],
GETDATE(),
[DivisionKeyID],
(SELECT TOP 1 ccd.[Guid]
FROM dbo.[CostCenterDefinitions] ccd
WHERE
ccd.[CostCenterCode] = dkl.[CostCenterCode]),
[GrantAmount]
FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] dkl
没有别名,我怀疑它只是将 CostCenterDefinitions
中的 costcentrecode
与 where
子句中的自身进行比较。
我认为您需要为 table 设置别名,以便子查询知道它在比较中查看的 CostCenterCode
。
SELECT
DivisionKeyLineID,
GETDATE(),
DivisionKeyID,
(SELECT TOP 1 Guid
from [dbo].CostCenterDefinitions ccd
where
ccd.CostCenterCode = cslst.CostCenterCode),
GrantAmount
FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] cslst
如果您不使用 table 别名,它只是检查 CostCenterDefinitions 中的 CostCenterCode 到它自身,返回 table 中的所有行(然后您将其排在前 1 位以获得相同的行时间)。
SQL 不知道你指的是哪个 "CostCenterCode"...所以它正在用相同的 column/same row/same table。您需要引用外部 table 对 "correlated subquery" 的操作。像这样:
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
我想通过以下查询将一些数据从一个数据库插入到另一个数据库:
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
但问题出在 CostCenterCode 上,因为我必须将 Guid 插入 CostCenterDefinitionID 字段,但是在 table 来自数据库 TestDB 的 CSLSTDIVDivisionKeyLines 中,我只有 CostCenterDefinition 的字符串代码(CostCenterCode 字段),所以我尝试 select 子查询中的 Guid,但在每一行中它 select 只是相同的,第一个来自 table 的 Guid。也许不同数据库中的列名相同是原因,但我不这么认为。谁能告诉我如何解决这个问题?
您需要在您的子程序中使用别名select。例如:
SELECT
[DivisionKeyLineID],
GETDATE(),
[DivisionKeyID],
(SELECT TOP 1 ccd.[Guid]
FROM dbo.[CostCenterDefinitions] ccd
WHERE
ccd.[CostCenterCode] = dkl.[CostCenterCode]),
[GrantAmount]
FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] dkl
没有别名,我怀疑它只是将 CostCenterDefinitions
中的 costcentrecode
与 where
子句中的自身进行比较。
我认为您需要为 table 设置别名,以便子查询知道它在比较中查看的 CostCenterCode
。
SELECT
DivisionKeyLineID,
GETDATE(),
DivisionKeyID,
(SELECT TOP 1 Guid
from [dbo].CostCenterDefinitions ccd
where
ccd.CostCenterCode = cslst.CostCenterCode),
GrantAmount
FROM [TestDB].[dbo].[CSLSTDIVDivisionKeyLines] cslst
如果您不使用 table 别名,它只是检查 CostCenterDefinitions 中的 CostCenterCode 到它自身,返回 table 中的所有行(然后您将其排在前 1 位以获得相同的行时间)。
SQL 不知道你指的是哪个 "CostCenterCode"...所以它正在用相同的 column/same row/same table。您需要引用外部 table 对 "correlated subquery" 的操作。像这样:
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