sql - 在查询中创建要加入的浮动表
问题描述
在数据库中,我缺少一些应该提供对类型的引用的基本表;这些是硬编码在软件中的。为了不无休止地编写case
语句来更改这些值,我想在每次需要这些数据时复制粘贴一个简单的浮动表。
我对declare
类型语句完全陌生,到目前为止,在创建可以粘贴到脚本中的子查询方面还没有运气。结果应该是这样的:
| Type ID | Period |
----------------------
| 1 | day |
| 2 | week |
| 3 | month |
| 4 | quarter |
| 5 | year |
我只是想将此表声明为子查询,以便我可以join
在其他包含type ID's
解决方案
临时表可能是最简单的方法,因为您可以一步完成:
select 1 as type_id, 'day' as period
into #temp_type_period union all
select 2 as type_id, 'week' union all
select 3 as type_id, 'month' union all
select 4 as type_id, 'quarter' union all
select 5 as type_id, 'year' ;
使用declare
,您必须声明表,然后将其插入:
declare @type_period table (
type_id int,
period varchar(255)
);
insert into @type_period (type_id, period)
select 1 as type_id, 'day' as period
select 2 as type_id, 'week' union all
select 3 as type_id, 'month' union all
select 4 as type_id, 'quarter' union all
select 5 as type_id, 'year' ;
推荐阅读
- google-sheets - 从 google 表格中获取 10 个值到我的 mit 应用程序发明者应用程序
- python - 如何使用 python/beautifulsoup 向 degiro.nl 提交登录信息?
- r - 带条件的数字序列(续)。data.table 解决方案?
- python - 仅将装饰器应用于类属性
- c# - 在尊重控制反转的同时在 .NET Core 中使用依赖注入
- spring - spring-boot-starter-data-elasticsearch 导致未知设置 analyzer/search_analyzer 和 index.settings.analysis.analyzer.autocomplete
- heroku - 拒绝从 <> 应用样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型,并且启用了严格的 MIME 检查
- python - “int”对象在附加索引时没有属性“append”
- google-apps-script - copyValuesToRange 不复制值,但 copyFormatToRange 工作正常
- c - do while 循环中的分段错误(核心转储)