使用在联接中使用动态查询创建的临时 Table
Using Temp Table created by using dynamic query in Joins
我有一个从另一个数据库获取某些记录的动态查询(数据库服务器和数据库名称是变量,因此使用动态查询)。
下面是查询
DECLARE @SQLString NVARCHAR(1000)
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
EXECUTE (@SQLString)
----- This is the final SQL statement (It is more complex than this)
select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
我想使用 #tmpAccDetails
加入 MainAccTable
。
- 我怎样才能做到这一点,因为临时 table 不会在动态 SQL 之外的范围内?
- 使用 Global Temp table 可以解决这个问题,但在这种情况下使用它是个好主意吗?
我的问题与此处的 Question 类似,不同之处在于我必须在 join 中使用 #tmpAccDetails
table,而不是从中选择数据一口气
如有任何帮助,我们将不胜感激。谢谢。
我想如果你先创建临时 table 就没问题....
例如……
DECLARE @SQLString NVARCHAR(1000)
CREATE TABLE #tmpAccDetails
(lAccountId int,
sAccountName NVArchar(100)
);
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
EXECUTE (@SQLString)
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
只需更改语句的顺序,如下所示:
DECLARE @SQLString NVARCHAR(1000)
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
CREATE TABLE #tmpAccDetails
(lAccountId int,
sAccountName NVArchar(100)
);
INSERT INTO #tmpAccDetails
EXEC sp_executesql @sSQL
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
BEGIN TRAN
DECLARE @SQLString NVARCHAR(1000)
CREATE TABLE #tmpAccDetails
(lAccountId int,
sAccountName NVArchar(100)
);
set @SQLString=' INSERT INTO #tmpAccDetails
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
EXECUTE (@SQLString)
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
RollBAck Tran
您是否考虑过 "OPENDATASOURCE" 从另一个数据库访问数据?
https://msdn.microsoft.com/fr-fr/library/ms179856.aspx
您可以使用此方法连接来自不同数据库的表
SELECT *
FROM MainAccTable M
INNER JOIN OPENDATASOURCE ('SQLOLEDB', 'Data Source=@myInstance;User ID=@myUserName;Password=@myPassword).XXX.dbo.AccountId AS tmp
ON tmp.AccountId = M.lAccountId
我自己解决了这个问题并将答案张贴在这里,以便其他人也能从中受益。
DECLARE @SQLString NVARCHAR(MAX)
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
from
'+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccountDetails
where
laccountID in (''10,11'')'
---- Create temp table here
CREATE TABLE #tmpAccDetails
(
AccXmlValue NVarchar(max),
AccountId int
);
---- Insert into temp table here
INSERT INTO #tmpAccDetails EXECUTE (@SQLString)
---- Select from temp table here
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
我有一个从另一个数据库获取某些记录的动态查询(数据库服务器和数据库名称是变量,因此使用动态查询)。
下面是查询
DECLARE @SQLString NVARCHAR(1000)
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
EXECUTE (@SQLString)
----- This is the final SQL statement (It is more complex than this)
select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
我想使用 #tmpAccDetails
加入 MainAccTable
。
- 我怎样才能做到这一点,因为临时 table 不会在动态 SQL 之外的范围内?
- 使用 Global Temp table 可以解决这个问题,但在这种情况下使用它是个好主意吗?
我的问题与此处的 Question 类似,不同之处在于我必须在 join 中使用 #tmpAccDetails
table,而不是从中选择数据一口气
如有任何帮助,我们将不胜感激。谢谢。
我想如果你先创建临时 table 就没问题....
例如……
DECLARE @SQLString NVARCHAR(1000)
CREATE TABLE #tmpAccDetails
(lAccountId int,
sAccountName NVArchar(100)
);
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
EXECUTE (@SQLString)
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
只需更改语句的顺序,如下所示:
DECLARE @SQLString NVARCHAR(1000)
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
CREATE TABLE #tmpAccDetails
(lAccountId int,
sAccountName NVArchar(100)
);
INSERT INTO #tmpAccDetails
EXEC sp_executesql @sSQL
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
BEGIN TRAN
DECLARE @SQLString NVARCHAR(1000)
CREATE TABLE #tmpAccDetails
(lAccountId int,
sAccountName NVArchar(100)
);
set @SQLString=' INSERT INTO #tmpAccDetails
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
into
#tmpAccDetails
from
AccountDetails
where
AccountDetails.laccountID in (''10,11'')'
EXECUTE (@SQLString)
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId
RollBAck Tran
您是否考虑过 "OPENDATASOURCE" 从另一个数据库访问数据? https://msdn.microsoft.com/fr-fr/library/ms179856.aspx
您可以使用此方法连接来自不同数据库的表
SELECT *
FROM MainAccTable M
INNER JOIN OPENDATASOURCE ('SQLOLEDB', 'Data Source=@myInstance;User ID=@myUserName;Password=@myPassword).XXX.dbo.AccountId AS tmp
ON tmp.AccountId = M.lAccountId
我自己解决了这个问题并将答案张贴在这里,以便其他人也能从中受益。
DECLARE @SQLString NVARCHAR(MAX)
set @SQLString='
select distinct(select distinct
(
select * from
(
------- Inner query (It is more complex than this)
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccDetails
where lAccountId = 10
union
select lAccountId, sAccountName
from '+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccHistoryDetails
where lAccountId = 10
) A
for xml raw(''Account''), ROOT(''Accounts''), ELEMENTS
)) as AccXmlValue,
lAccountId as AccountId
from
'+@DatabaseServer+'.'+@DatabaseName+'.dbo.AccountDetails
where
laccountID in (''10,11'')'
---- Create temp table here
CREATE TABLE #tmpAccDetails
(
AccXmlValue NVarchar(max),
AccountId int
);
---- Insert into temp table here
INSERT INTO #tmpAccDetails EXECUTE (@SQLString)
---- Select from temp table here
Select * from
MainAccTable M
inner join #tmpAccDetails tmp on M.lAccountId = tmp.AccountId