使用 UNION ALL SQL 查询填充类型数据集

发布于 2024-09-12 06:37:47 字数 1046 浏览 5 评论 0原文

我这里有一个奇怪的情况,我希望有人可以帮助我。使用非类型化数据集时我没有遇到同样的问题。

好的。我有一个类型化数据集 MyDS 和一个类型化数据表 MyTable(TableID、标题、消息)。

该表使用 UNION ALL 填充了两个表的结果。


Select
  TableAID,
  TableATitle,
  Message
FROM TableA

UNION ALL

Select
  TableBID,
  TableBTitle,
  Message
FROM TableB

Dim cmd As SqlCommand = New SqlCommand("SQL", conn)

Dim da AS SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(MyDS.MyTable)

该查询在 SQL Server 上运行良好,但当我单步执行代码时,我发现不正确的 TableAID 或 TableAID。

表 A 有 1 行,表 B 有 15 行。所以我的数据集返回 16 行(所以基本上这是一个主从情况)。除了 TableAID 和 TableBID 之外,所有列都返回正确的相应数据。

TableAID 的值为 100,TableBID 的值为 10, 11, 12, 13, .........24,但是当我单步执行时,

MyDS.MyTable(i).TableID
remains 100 throughout the 16 records, instead of 100, 10, 11, 12, 13, .........24. The Title, Message data is correct. TableAID and TableBID are my primary keys.

i = 0 到 15

这就是类型化数据集处理 UNION 的方式吗?我不想创建两个类型化数据表并为每个数据表创建单独的 SQL 并创建关系。

I have a strange situation here and I hope someone can help me. I don't have the same problem when using an untyped dataset.

OK. I have a typed dataset MyDS with a typed datatable MyTable (TableID, Title, Message).

The table gets filled with results from two tables, using a UNION ALL


Select
  TableAID,
  TableATitle,
  Message
FROM TableA

UNION ALL

Select
  TableBID,
  TableBTitle,
  Message
FROM TableB

Dim cmd As SqlCommand = New SqlCommand("SQL", conn)

Dim da AS SqlDataAdapter = New SqlDataAdapter(cmd)
da.Fill(MyDS.MyTable)

The query runs well on SQL Server, but when I step through the code, I find incorrect TableAID or TableAID.

TableA has 1 row and TableB has 15 rows. So my dataset returns 16 rows (so basically it's a master-detail situation). All the columns return correct respective data, except the TableAID and TableBID.

TableAID has value = 100 and TableBID has 10, 11, 12, 13, .........24, but when I step through, the

MyDS.MyTable(i).TableID

remains 100 throughout the 16 records, instead of 100, 10, 11, 12, 13, .........24. The Title, Message data is correct. TableAID and TableBID are my primary keys.

i = 0 to 15

Is this how typed datasets handles UNION? I didn't want to create two typed datatables and create separate SQL for each and create a relation.

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

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

发布评论

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

评论(1

久随 2024-09-19 06:37:47

我设法修复它。它根本不是数据集。我实际上使用的是从视图中进行选择的存储过程,并且我有 2 个几乎相同的视图(view1 和 view2)。所以上面的sql来自view2,但存储过程使用view1。我使用视图是因为语句比那复杂。所以我的动机是,如果我需要在存储过程中使用一个复杂的 SELECT sql,请将其放在视图中并使用一个简单的过程。
代码

            
 Dim cmd As SqlCommand = New SqlCommand("StoredProcedureName", conn)
 cmd.CommandType = CommandType.StoredProcedure

不是


Dim cmd As SqlCommand = New SqlCommand("SQL", conn)

我几乎想用非类型化数据集替换我的类型化数据集,因为我已经没有时间了。截止日期。哈哈
感谢您让我认为这不是数据集,因为您无法弄清楚可能是什么问题。哈哈

I managed to fix it. It wasn't the dataset at all. I was actually using a stored proc that was selecting from a view, and I had 2 views (view1 and view2) that were almost the same. So the above sql was from view2, but the stored proc was using view1. I used views because the statement was complicated than that. So my moto is, if I have a complicated SELECT sql that I need to use in my stored proc, put it on view and have a simple proc to work with.
The code is

            
 Dim cmd As SqlCommand = New SqlCommand("StoredProcedureName", conn)
 cmd.CommandType = CommandType.StoredProcedure

Not


Dim cmd As SqlCommand = New SqlCommand("SQL", conn)

I almost wanted to replace my typed dataset with untyped one, because I was running out of time. Deadlines. LOL
Thank you for making me think that it was not the dataset since you could not figure out what could be the problem. LOL

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