Select 中的 MySQL 过程?
我有一个像这样工作的过程:
mysql> call Ticket_FiscalTotals(100307);
+---------+--------+----------+------------+------------+
| Service | Items | SalesTax | eTaxAmount | GrandTotal |
+---------+--------+----------+------------+------------+
| 75.00 | 325.00 | 25.19 | 8.00 | 433.19 |
+---------+--------+----------+------------+------------+
1 row in set (0.08 sec)
我想从选择中调用这个过程,如下所示:
SELECT Ticket.TicketID as `Ticket`,
Ticket.DtCheckOut as `Checkout Date / Time`,
CONCAT(Customer.FirstName, ' ', Customer.LastName) as `Full Name`,
Customer.PrimaryPhone as `Phone`,
(CALL Ticket_FiscalTotals(Ticket.TicketID)).Service as `Service`
FROM Ticket
INNER JOIN Customer ON Ticket.CustomerID = Customer.CustomerID
ORDER BY Ticket.SiteHomeLocation, Ticket.TicketID
但是我知道这是错误的。有人可以指出我正确的方向吗?我将需要访问最终选择中要(加入?)的过程中的所有列。该过程中的 SQL 代码相当痛苦,这就是它的首要原因!
I have a procedure that works like this:
mysql> call Ticket_FiscalTotals(100307);
+---------+--------+----------+------------+------------+
| Service | Items | SalesTax | eTaxAmount | GrandTotal |
+---------+--------+----------+------------+------------+
| 75.00 | 325.00 | 25.19 | 8.00 | 433.19 |
+---------+--------+----------+------------+------------+
1 row in set (0.08 sec)
I would like to call this procedure from within a select, like so:
SELECT Ticket.TicketID as `Ticket`,
Ticket.DtCheckOut as `Checkout Date / Time`,
CONCAT(Customer.FirstName, ' ', Customer.LastName) as `Full Name`,
Customer.PrimaryPhone as `Phone`,
(CALL Ticket_FiscalTotals(Ticket.TicketID)).Service as `Service`
FROM Ticket
INNER JOIN Customer ON Ticket.CustomerID = Customer.CustomerID
ORDER BY Ticket.SiteHomeLocation, Ticket.TicketID
However I know that this is painfully wrong. Can someone please point me in the proper direction? I will need access to all of the columns from the procedure to be (joined?) in the final Select. The SQL code within that procedure is rather painful, hence the reason for it in the first place!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ticket_FiscalTotals 过程返回一个包含一些字段的数据集,但您只需要其中一个字段 -
Service
。将您的过程重写为存储函数 -Get_Ticket_FiscalTotals_Service
。另一种方法是在过程中创建并填充临时表,并将该临时表添加到查询中,例如:
The Ticket_FiscalTotals procedure returns a data set with some fields, but you need just one of them -
Service
. Rewrite your procedure to stored function -Get_Ticket_FiscalTotals_Service
.Another way is to create and fill temporary table in the procedure, and add this temporary to a query, e.g.:
您不能直接加入存储过程。您可以联接到此存储过程填充的临时表:
当然,这不是一条线解决方案。
我想到的另一种方法(在我看来更糟糕)是在 SP 结果集中拥有与列一样多的 UDF,这可能看起来像下面的代码:
You can't join directly to stored procedure. You can join to temporary table that this stored procedure fills:
Of course it is not one line solution.
The other way (worse in my opinion) I think of is to have as many UDF as columns in SP result set, this might look like fallowing code: