有没有办法在Oracle SQL查询中引用其中包含空格的别名?我还没有在任何地方找到它.通常我不使用空格来命名数据库元素,所以这对我来说是个新的基础.
这是一个示例查询:
SELECT t.id AS "Ticket ID",(SELECT w.workgroup_title FROM workgroups w WHERE w.operator_id = t.operator_id AND ROWNUM < 2) AS "Workgroup Title" FROM tickets t
如何在未来的比较操作中使用“Ticket ID”或“Workgroup Title”?例如:
“机票ID”== 9
要么
“工作组标题”==’工作组#1′
我猜这是一个简单的方法,但还没有找到它.
谢谢!
PS这是传给我的遗产. sql中的别名动态地绑定到其他各种代码中,并且都有空格.因此,删除空格并不是一件简单的事情,我目前还没有为预算编制时间.否则,这就是我要做的.
编辑澄清
使用上面的示例,如何在“工作组标题”始终引用别名的情况下执行此类操作:
SELECT t.id AS "Ticket ID",(SELECT w.workgroup_title FROM workgroups w WHERE w.operator_id = t.operator_id AND ROWNUM < 2) AS "Workgroup Title",(SELECT s.support_tier_id FROM support_tiers s WHERE s.workgroup_title == "Workgroup Title") AS "Support Tier ID" FROM tickets t
解决方法
是的,你可以这样做,但你必须用双引号括起这些名字:
-- sql Example SELECT "Just one" FROM ( SELECT 1 AS "Just one" FROM dual );
输出:
Just one ---------- 1
-- PL/sql Example BEGIN FOR v_rec IN (SELECT 1 AS "Just one" FROM dual) LOOP dbms_output.put_line(v_rec."Just one"); END LOOP; END; /
输出:
1
还有一点好奇心:
BEGIN FOR v_rec IN (SELECT COUNT(1) * 10 FROM dual) LOOP dbms_output.put_line(v_rec."COUNT(1)*10"); END LOOP; END;
这适用..并打印:
10
编辑
您可以在查询的其他位置引用别名列,例如:
SELECT 'It is just one' FROM ( SELECT "Just one" FROM ( SELECT 1 AS "Just one" FROM dual ) t WHERE t."Just one" = 1 -- prefixing with t. is not necessary in this particular example );
输出:
It is just one