sqlplus 中带有替换变量的请求之间的联合

发布于 2024-10-20 17:05:03 字数 808 浏览 3 评论 0原文

我有 14 个相似的字段,我在每个字段上搜索字符串“A”。我想在按“位置”字段排序后

-- some set in order to remove a lot of useless text
def col='col01'
select '&col' "Fieldname", 
       &col "value", 
       position
from oneTable
where &col like '%A%'
/
-- then for the second field, I only have to type two lines
def col='col02'
/
...
def col='col14'
/

写入包含“A”的所有字段。问题是这些字段不是按位置排序的。

如果我在表之间使用 UNION,则无法利用替换变量 (&col),并且我必须在 unix 中编写 bash 以便将替换返回到 ksh 中。问题当然是数据库代码必须在这个脚本中硬编码(连接不是一件容易的事情)。

如果我将 REFCURSOR 与 OPEN 一起使用,则无法将结果集分组在一起。我只有一个请求,无法将其联合起来。 (print refcursor1 union refcursor2;print refcursor1+refcursor2 引发异常,select * from refcursor1 union select * from refcursor2,也不起作用)。

如何将结果连接成一个大的“REFCURSOR”?或者在我的请求的两个不同的运行(“/”)之间使用联合,例如在键入新的变量定义时保留请求?

感谢您的任何建议。

I have 14 fields which are similar and I search the string 'A' on each of them. I would like after that order by "position" field

-- some set in order to remove a lot of useless text
def col='col01'
select '&col' "Fieldname", 
       &col "value", 
       position
from oneTable
where &col like '%A%'
/
-- then for the second field, I only have to type two lines
def col='col02'
/
...
def col='col14'
/

Write all the fields which contains 'A'. The problem is that those field are not ordered by position.

If I use UNION between table, I cannot take advantage of the substitution variables (&col), and I have to write a bash in unix in order to make the replacement back into ksh. The problem is of course that database code have to be hard-coded in this script (connection is not easy stuff).

If I use a REFCURSOR with OPEN, I cannot group the results sets together. I have only one request and cannot make an UNION of then. (print refcursor1 union refcursor2; print refcursor1+refcursor2 raise an exception, select * from refcursor1 union select * from refcursor2, does not work also).

How can concatenate results into one big "REFCURSOR"? Or use a union between two distinct run ('/') of my request, something like holding the request while typing new definition of variables?

Thank you for any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2024-10-27 17:05:03

这能回答你的问题吗?

CREATE TABLE #containingAValueTable
(
    FieldName VARCHAR(10),
    FieldValue VARCHAR(1000),
    position   int
)
def col='col01'

INSERT INTO #containingAValueTable
(
FieldName , FieldValue, position
)
SELECT '&col' "Fieldname", 
       &col "value", 
       position
FROM yourTable
        WHERE &col LIKE '%A%'

/
-- then for the second field, I only have to type two lines
def col='col02'
INSERT INTO...
/

def col='col14'
/

select * from #containingAValueTable order by postion

DROP #containingAValueTable

但我不完全确定您使用名为“col”的“替换变量”(并且我只有 SQL Server 来测试我的请求,所以我使用了显式字段名称)

编辑:抱歉“#”字符,我们使用它在 SQL Server 中经常用于临时表,我什至不知道它是 SQL Server 特定的(而且我认为它在 sql server 中创建临时表是强制性的)。不管怎样,我很高兴我能对你有用。

Does this answer your question ?

CREATE TABLE #containingAValueTable
(
    FieldName VARCHAR(10),
    FieldValue VARCHAR(1000),
    position   int
)
def col='col01'

INSERT INTO #containingAValueTable
(
FieldName , FieldValue, position
)
SELECT '&col' "Fieldname", 
       &col "value", 
       position
FROM yourTable
        WHERE &col LIKE '%A%'

/
-- then for the second field, I only have to type two lines
def col='col02'
INSERT INTO...
/

def col='col14'
/

select * from #containingAValueTable order by postion

DROP #containingAValueTable

But I'm not totally sure about your use of the 'substitution variable' called "col" (and i only have SQL Server to test my request so I used explicit field names)

edit : Sorry for the '#' charcater, we use it so often in SQL Server for temporaries, I didn't even know it was SQL Server specific (moreover I think it's mandatory in sql server for creating temporary table). Whatever, I'm happy I could be useful to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文