将我的查询更改为存储过程?

时间:2012-08-30 20:04:15

标签: stored-procedures

这是查询(MS SQL):

use DB_432799_satv

select [DB_432799_satv].[dbo].ac_Orders.OrderNumber, OrderDate, PaymentDate, 
    CompletedDate, ShipDate,DateTimeFinishedPicking,DateTimeShipped, 
    ShipMethodName, PaymentMethodName
from [DB_432799_satv].[dbo].ac_Orders
Inner join [DB_432799_satv].[dbo].ac_Payments on 
    [DB_432799_satv].[dbo].ac_Orders.OrderId  = 
    [DB_432799_satv].[dbo].ac_Payments.OrderId
Inner join [DB_432799_satv].[dbo].ac_OrderShipments on  
    [DB_432799_satv].[dbo].ac_Orders.OrderId = 
    [DB_432799_satv].[dbo].ac_OrderShipments.OrderId
Inner join [DB_432799_satv].[dbo].[ac_Transactions] on 
    [DB_432799_satv].[dbo].ac_Payments.paymentid = 
    [DB_432799_satv].[dbo].ac_Transactions.paymentid
Inner join [SuperATV].[dbo].[tblPartsBoxHeader] on 
    [DB_432799_satv].[dbo].[ac_transactions].[ProviderTransactionId] = 
    [SuperATV].[dbo].[tblPartsBoxHeader].[ordernumber]

我如何将其更改为存储过程?

1 个答案:

答案 0 :(得分:1)

我怀疑你想要的是“视图”会给你什么。具体来说,将该查询存储在数据库中并从中进行选择:

SELECT * FOM myLongQueryView WHERE OrderNumber = 1234;

我没有开发MSSQL,但是快速搜索this doc page on msdn。我想它会是这样的:

CREATE VIEW [DB_432799_satv].myLongQueryView
AS 
select [DB_432799_satv].[dbo].ac_Orders.OrderNumber, OrderDate, PaymentDate, 
    CompletedDate, ShipDate,DateTimeFinishedPicking,DateTimeShipped, 
    ShipMethodName, PaymentMethodName
from [DB_432799_satv].[dbo].ac_Orders
...

只需使用CREATE VIEW [DB_432799_satv].myLongQueryView AS添加您的查询,然后使用文档页面决定是否需要任何选项。

重要的是要注意存储查询,而不是它的当前结果。因此它可以与您的数据保持同步。它是可连接的,可聚合的等等......