Mic*_*hal 7 sql t-sql database sql-server
我正在写一些资源管理系统.
资源是定义的实例.定义是元数据,基本上它包含属性.
这通常是我的DB:
TypeDefinition
id name
===============
1 CPU
PropertyDefinition
id name typeDefinitionId valueType
================================================
1 frequency 1 int
2 status 1 string
TypeInstance
id name typeDefinitionId
=================================
1 CPU#1 1
2 CPU#2 1
PropertyInstanceValue
id propertyDefinitionId typeInstanceId valueType intValue StringValue FloatValue
========================================================================================
1 1 1 int 10
2 2 1 string Pending
3 1 2 int 20
4 2 2 string Approved
Run Code Online (Sandbox Code Playgroud)
需求:
根据特定的属性值排序所有资源.
例如:根据状态排序所有资源- >含义CPU#2将出现在CPU#1之前,因为"已批准"在"待定"之前.
如果我们按照频率订购,CPU#1将出现在CPU#2之前,因为10在20之前.
所以我需要根据不同的列(intValue/stringValue/FloatValue/etc)对每次进行排序,具体取决于属性的valueType.
有什么建议吗?
局限性:
PIVOT目前是我们唯一想到的选择,但由于数据库很庞大,我不需要查询尽可能快.
非常感谢提前,
米哈尔.
如果问题是您不想动态构建查询,那么使用以下order by结构:
order by case @orderby
when 'status' then status
when 'frequency' then frequency
end
option (recompile)
Run Code Online (Sandbox Code Playgroud)
您将传递@orderby参数.最后recompile option是强制引擎根据传递的参数构建新计划,即假设您使用的是存储过程.