查询要从中选择数据的 table 名称是表达式?
Query to have the table name from which data is to be selected be an expression?
我正在使用 SQL 服务器版本 12.2.9(即 SQL 服务器 2014,我认为)?
在 SQL 查询中,是否可以使用一个或多个要从中选择数据的 table 名称作为将在查询执行期间计算的表达式?例如如以下伪代码?
SELECT *
FROM MainTable AS mainTable,
(
/* Expression here that returns
a string (or what type do we return)
denoting the other table name
*/
) AS AliasFoo
WHERE AliasFoo.Id = mainTable.ExternalId;
您能否提供此类查询的样本?具体来说,我们从应该 return 引用 table / table 名称的表达式中 return 是什么数据类型?
问题的进一步发展
为了使示例更具体一些以邀请适当的帮助,这里是一个人为的示例。
假设我有以下 tables:
ActivityType
---------
Id ( int primary key, identity )
ActivityName (possible values are 'Jogging', 'Biking', and more)
ActivityLog
--------
Id ( int, primary key, identity)
DateTime
ActivityTypeId
ActivityDetailId (a primary key of one of the following activity detail tables)
ACTIVITY DETAIL TABLES
Jogging
--------
Id ( int, primary key, identity)
WhoWasJogging
ForHowLong
WhatShoesWereTheyWearing
Biking
--------
Id ( int, primary key, identity)
WhoWasBiking
WhatBikeWasThat
WhatBrand
Color
Speed
ForHowLong
鉴于上述 tables,我可以这样查询吗?
SELECT aLog.DateTime, aType.ActivityName, activityDetail.*
FROM ActivityLog AS aLog, ActivityType AS aType,
(
/*
if ActivityType.ActivityName == 'Jogging' then the 'Jogging' table,
else if ActivityType.ActivityName == 'Biking' then the 'Biking' table
*/
) AS activityDetail
WHERE aLog.ActivityTypeId = aType.Id
AND activityDetail.Id = aLog.ActivityDetailId;
好的,这是否是最佳答案取决于您在现实世界中有多少个不同的表。因此,对于少量表,left joining
是一种可能的解决方案,如下所示。您可以在 select 列中看到这增加了复杂性,但这可能会给您想要的。
select aLog.[DateTime]
, aType.ActivityName
, case when aType.ActivityName = 'Jogging' then J.WhoWasJogging else B.WhoWasBiking end WhoWas
-- And so on
from ActivityLog as aLog
inner join ActivityType as aType on aType.Id = aLog.ActivityTypeId
left join Jogging as J on aType.ActivityName = 'Jogging' and aLog.ActivityDetailId = J.Id
left join Biking as B on aType.ActivityName = 'Biking' and aLog.ActivityDetailId = B.Id
这还取决于您是否希望一次查询多个 activity 类型。
如果动态 SQL 是首选,那么以下应该有效:
declare @Sql nvarchar(max), @Activity varchar(128) = 'Biking';
set @Sql = 'select aLog.[DateTime]
, aType.ActivityName
, A.*
from ActivityLog as aLog
inner join ActivityType as aType on aType.Id = aLog.ActivityTypeId
inner join ' + @Activity + ' as A on and aLog.ActivityDetailId = A.Id
where aType.ActivityName = ''' + @Activity + '''';
exec (@sql);
我正在使用 SQL 服务器版本 12.2.9(即 SQL 服务器 2014,我认为)?
在 SQL 查询中,是否可以使用一个或多个要从中选择数据的 table 名称作为将在查询执行期间计算的表达式?例如如以下伪代码?
SELECT *
FROM MainTable AS mainTable,
(
/* Expression here that returns
a string (or what type do we return)
denoting the other table name
*/
) AS AliasFoo
WHERE AliasFoo.Id = mainTable.ExternalId;
您能否提供此类查询的样本?具体来说,我们从应该 return 引用 table / table 名称的表达式中 return 是什么数据类型?
问题的进一步发展
为了使示例更具体一些以邀请适当的帮助,这里是一个人为的示例。
假设我有以下 tables:
ActivityType
---------
Id ( int primary key, identity )
ActivityName (possible values are 'Jogging', 'Biking', and more)
ActivityLog
--------
Id ( int, primary key, identity)
DateTime
ActivityTypeId
ActivityDetailId (a primary key of one of the following activity detail tables)
ACTIVITY DETAIL TABLES
Jogging
--------
Id ( int, primary key, identity)
WhoWasJogging
ForHowLong
WhatShoesWereTheyWearing
Biking
--------
Id ( int, primary key, identity)
WhoWasBiking
WhatBikeWasThat
WhatBrand
Color
Speed
ForHowLong
鉴于上述 tables,我可以这样查询吗?
SELECT aLog.DateTime, aType.ActivityName, activityDetail.*
FROM ActivityLog AS aLog, ActivityType AS aType,
(
/*
if ActivityType.ActivityName == 'Jogging' then the 'Jogging' table,
else if ActivityType.ActivityName == 'Biking' then the 'Biking' table
*/
) AS activityDetail
WHERE aLog.ActivityTypeId = aType.Id
AND activityDetail.Id = aLog.ActivityDetailId;
好的,这是否是最佳答案取决于您在现实世界中有多少个不同的表。因此,对于少量表,left joining
是一种可能的解决方案,如下所示。您可以在 select 列中看到这增加了复杂性,但这可能会给您想要的。
select aLog.[DateTime]
, aType.ActivityName
, case when aType.ActivityName = 'Jogging' then J.WhoWasJogging else B.WhoWasBiking end WhoWas
-- And so on
from ActivityLog as aLog
inner join ActivityType as aType on aType.Id = aLog.ActivityTypeId
left join Jogging as J on aType.ActivityName = 'Jogging' and aLog.ActivityDetailId = J.Id
left join Biking as B on aType.ActivityName = 'Biking' and aLog.ActivityDetailId = B.Id
这还取决于您是否希望一次查询多个 activity 类型。
如果动态 SQL 是首选,那么以下应该有效:
declare @Sql nvarchar(max), @Activity varchar(128) = 'Biking';
set @Sql = 'select aLog.[DateTime]
, aType.ActivityName
, A.*
from ActivityLog as aLog
inner join ActivityType as aType on aType.Id = aLog.ActivityTypeId
inner join ' + @Activity + ' as A on and aLog.ActivityDetailId = A.Id
where aType.ActivityName = ''' + @Activity + '''';
exec (@sql);