多个oracle查询问题

发布于 2024-11-24 08:05:43 字数 873 浏览 2 评论 0原文

我正在尝试从 .NET 页面运行查询,但似乎在进行多个查询时遇到一些问题。

我的查询类似于此

SELECT * FROM table1; SELECT * from table2

但从 .Net 页面执行此操作时,我似乎收到无效字符错误。它在 SQL Developer 中运行良好,但仅当我将其放入 .NET 页面时才会失败。

我已将 BEGINEND 添加到查询中,因为某些网站建议您需要它来运行多个查询,但随后出现以下错误

ORA-06550: 第 1 行,第 7 列: PLS-00428:此 SELECT 语句中需要 INTO 子句

有谁能解释一下这一点吗?

提前致谢!

编辑

这是一些代码

        query = conn.CreateCommand()

        query.CommandText = "SELECT * from table1; SELECT * FROM table2;"

        DataSet = New DataSet()
        DataAdapter = New DataAdapter(query)
        DataAdapter.Fill(DataSet)

        datagrid1.DataSource = DataSet.Tables(0)
        datagrid1.DataBind()

        lbl1.Text = DataSet.Tables(1).Rows(0).Item("column1").ToString()

I'm trying to run a query from a .NET page but I seem to having some problems with having multiple queries.

My query is similar to this

SELECT * FROM table1; SELECT * from table2

But i seem to get an invalid character error when executing this from a .Net page. It runs fine in SQL developer but only fails when i put it in my .NET page.

I've added the BEGIN and END to the query as some websites suggest you need this to run multiple queries but then I get the following error

ORA-06550: line 1, column 7:
PLS-00428: an INTO clause is expected in this SELECT statement

Can anyone shed any light on this one?

Thanks in advance!

EDIT

Here's some code

        query = conn.CreateCommand()

        query.CommandText = "SELECT * from table1; SELECT * FROM table2;"

        DataSet = New DataSet()
        DataAdapter = New DataAdapter(query)
        DataAdapter.Fill(DataSet)

        datagrid1.DataSource = DataSet.Tables(0)
        datagrid1.DataBind()

        lbl1.Text = DataSet.Tables(1).Rows(0).Item("column1").ToString()

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

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

发布评论

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

评论(4

阿楠 2024-12-01 08:05:43

如果您想从 2 个表中提取数据并获取可填充到 DataAdapter 中的 DataSet,则需要使用以下方法之一:

  1. 将 2 个表连接在一起(可能会也可能不会,具体取决于您的表)
  2. union the 2 个表(这可能适用于您的场景,也可能不适用于您的场景)
  3. 编写一个存储过程,该过程创建您需要的任何结果并将其返回到引用游标中。您可以在此处了解如何执行此操作。

您无法仅运行 2 个这样的 SQL 语句并在数据集中获取任何有意义的结果。

If you want to pull from 2 tables and get a DataSet that you can fill into a DataAdapter, then you need to use one of the following approaches:

  1. join the 2 tables together (may or may not be possible depending on your tables)
  2. union the 2 tables (this may or may not be applicable to your scenario)
  3. write a stored procedure which creates whatever result you need and returns it in a ref cursor. You can read about how to do that here.

You're not going to be able to just run 2 SQL statements like that and get any meaningful result into a DataSet.

蛮可爱 2024-12-01 08:05:43

为陈述显而易见的事实而道歉,但是:

1.. 进行 2 次调用

2.. 将选择放入存储过程并返回 2 个引用游标,

这是有关使用多个结果集的一个很好的链接:
http://msdn.microsoft.com/en-us/library/ms971506 .aspx#msdnorsps_topic13

Apologies for stating the obvious but:

1.. Make 2 calls

or

2.. Put the selects in a stored procedure and return 2 refcursors

here's a good link on Working with Multiple Result Sets:
http://msdn.microsoft.com/en-us/library/ms971506.aspx#msdnorsps_topic13

似狗非友 2024-12-01 08:05:43

如果您只需要 table1 和 table2 中都存在的字段,您可以这样做

SELECT field1, field2, field3 FROM table1
UNION
SELECT field1, field2, field3 FROM table2

如果字段具有不同的名称,但您可以执行相同类型的内容

SELECT tab1_id AS primary_key, tab1_name AS name, tab1_amount AS amount FROM table1
UNION
SELECT tab2_id AS primary_key, tab2_name AS name, tab2_amount AS amount FROM table2

这将为您提供包含 Primary_key、name、amount 列的结果(这只是一个随机示例)

如果两个表包含完全不同的内容,那么您确实应该使用两个单独的查询。

If you only need fields that are present in both table1 and table2 you can do

SELECT field1, field2, field3 FROM table1
UNION
SELECT field1, field2, field3 FROM table2

If the fields have different names but the same type of content you can do

SELECT tab1_id AS primary_key, tab1_name AS name, tab1_amount AS amount FROM table1
UNION
SELECT tab2_id AS primary_key, tab2_name AS name, tab2_amount AS amount FROM table2

This will give you a result with the columns primary_key, name, amount (this is just a random example)

If the two tables contain completely different content, you should really use two separate queries.

非要怀念 2024-12-01 08:05:43

可能的解决方案可能是

query.CommandText = "BEGIN OPEN :1 FOR SELECT * FROM table1; OPEN :2 FOR SELECT * FROM table2; END;"; 

...如 DataSet.Load:使用 System.Data.OracleClient.OracleDataReader 加载多个表 ...但我自己没有测试过。

另请查看 http://forums.asp.net/t/629511.aspx/1< /a>.它展示了如何,某种程度上。我添加了对 Oracle.DataAccess.Types; 的引用,但仍然有问题。不过也很接近了。

A possible solution could be

query.CommandText = "BEGIN OPEN :1 FOR SELECT * FROM table1; OPEN :2 FOR SELECT * FROM table2; END;"; 

... as found in DataSet.Load: Loading multiple tables with System.Data.OracleClient.OracleDataReader ... but not tested myself.

Also check out http://forums.asp.net/t/629511.aspx/1. It shows how, sort of. I added a reference to Oracle.DataAccess.Types;, but still had a problem. It's close though.

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