如何在选择动态查询中连接表

时间:2017-09-19 11:40:19

标签: sql-server select

请查看以下查询

DECLARE @DBName AS Nvarchar(MAX)
select TOP(1) @DBName=DBName from StreamLeaderMaster.dbo.TicketMasterDB ORDER BY id DESC
DECLARE @SQL AS Nvarchar(MAX)
SET @SQL=
'Select TCH.ID,MasterDB.CreatedByUserID,MasterDB.CreatedByDBName,
MasterDB.CreatedByDBName+''.dbo.tblSMSecurityUser'' as waheguru,
( select ISNULL(strFirstName,'''') as firstname from
 dbo.tblSMSecurityUser where UserID=1 ) as waheguru2,MD.DBName,
U.strFirstName,Message,CONVERT(VARCHAR(9),CONVERT(date, 
CH.CreatedDate),6) As CreatedDate,TCH.CreatedDate as CreatedDateTime,
RepliedByUserID,RepliedMasterDBID from 
StreamLeaderMaster.dbo.TicketChatHistory TCH
left join '+@DBName +'.dbo.tblSMSecurityUser U on 
U.UserID=TCH.RepliedByUserID
left join StreamLeaderMaster.dbo.TicketMasterDB MD on 
MD.ID=TCH.RepliedMasterDBID
LEFT JOIN StreamLeaderMaster.dbo.TicketMaster MasterDB on 
MasterDB.ID=TCH.TicketMasterID
where TCH.TicketMasterID=1  and (TCH.IsDeleted <> 1 or TCH.IsDeleted IS 
NULL) order by CreatedDateTime desc'
 PRINT @SQL
 Exec(@SQL)

MasterDB.CreatedByDBName 是我要在子查询中 dbo.tblSMSecurityUser 之前附加的数据库名称 即

( select ISNULL(strFirstName,'''') as firstname from

dbo.tblSMSecurityUser,其中UserID = 1)as waheguru2

我希望它如下所示:

( select ISNULL(strFirstName,'''') as firstname from

TESDB.dbo.tblSMSecurityUser,其中UserID = 1)as waheguru2

因为数据库名称是动态改变的..有什么方法可以做到这一点..

请帮帮我。

2 个答案:

答案 0 :(得分:0)

试试这个 -

DECLARE @DBName AS Nvarchar(MAX)
SELECT TOP(1) @DBName=DBName FROM StreamLeaderMaster.dbo.TicketMasterDB ORDER BY id DESC
DECLARE @SQL AS Nvarchar(MAX)
SET @SQL=
'SELECT TCH.ID, MasterDB.CreatedByUserID, MasterDB.CreatedByDBName,
MasterDB.CreatedByDBName.dbo.tblSMSecurityUser as waheguru,
( select ISNULL(strFirstName,'''') as firstname from ' + @DBName + 
'dbo.tblSMSecurityUser where UserID=1 ) as waheguru2, MD.DBName,
U.strFirstName,Message,CONVERT(VARCHAR(9),CONVERT(date, 
CH.CreatedDate),6) As CreatedDate,TCH.CreatedDate as CreatedDateTime,
RepliedByUserID,RepliedMasterDBID from 
StreamLeaderMaster.dbo.TicketChatHistory TCH
left join '+@DBName +'.dbo.tblSMSecurityUser U on 
U.UserID=TCH.RepliedByUserID
left join StreamLeaderMaster.dbo.TicketMasterDB MD on 
MD.ID=TCH.RepliedMasterDBID
LEFT JOIN StreamLeaderMaster.dbo.TicketMaster MasterDB on 
MasterDB.ID=TCH.TicketMasterID
where TCH.TicketMasterID=1  and (TCH.IsDeleted <> 1 or TCH.IsDeleted IS 
NULL) order by CreatedDateTime desc'
PRINT @SQL
Exec(@SQL)

答案 1 :(得分:0)

我不确定我是否理解你的问题。

DECLARE @DBName AS Nvarchar(MAX)
select TOP(1) @DBName=DBName from StreamLeaderMaster.dbo.TicketMasterDB 
ORDER BY id DESC
DECLARE @SQL AS Nvarchar(MAX)
SET @SQL=
'Select TCH.ID,MasterDB.CreatedByUserID,MasterDB.CreatedByDBName,
MasterDB.CreatedByDBName+''.dbo.tblSMSecurityUser'' as waheguru,
( select ISNULL(strFirstName,'''') as firstname from
 ' + @DBName + 'dbo.tblSMSecurityUser where UserID=1 ) as 
waheguru2,MD.DBName,
U.strFirstName,Message,CONVERT(VARCHAR(9),CONVERT(date, 
CH.CreatedDate),6) As CreatedDate,TCH.CreatedDate as CreatedDateTime,
RepliedByUserID,RepliedMasterDBID from 
StreamLeaderMaster.dbo.TicketChatHistory TCH
left join '+@DBName +'.dbo.tblSMSecurityUser U on 
U.UserID=TCH.RepliedByUserID
left join StreamLeaderMaster.dbo.TicketMasterDB MD on 
MD.ID=TCH.RepliedMasterDBID
LEFT JOIN StreamLeaderMaster.dbo.TicketMaster MasterDB on 
MasterDB.ID=TCH.TicketMasterID
where TCH.TicketMasterID=1  and (TCH.IsDeleted <> 1 or TCH.IsDeleted IS 
NULL) order by CreatedDateTime desc'
 PRINT @SQL
 Exec(@SQL)

如果要连接的数据库的名称是@dbname,那么它应该可以工作。如果不是只是改变参数,你应该没事。

希望这有帮助