这是一个sql存储过程

时间:2015-04-09 18:53:10

标签: mysql sql-server

我想在Excel(SQL查询窗口)中执行此操作,我通常只执行select语句。我应该将其存入存储过程,然后在Excel中执行命令吗?

BEGIN TRY

    drop table GlobalShop.dbo.v_order_hist_dtl_Quote 
END TRY
BEGIN CATCH

END CATCH

BEGIN TRY

    drop table GlobalShop.dbo.v_order_lines_Quote 
END TRY
BEGIN CATCH

END CATCH


select * into GlobalShop.dbo.v_order_hist_dtl_Quote 
from GlobalShop.dbo.v_order_hist_dtl where isnull(USER_3,'') <> ''  
select * into GlobalShop.dbo.v_order_lines_Quote 
from GlobalShop.dbo.v_order_lines where isnull(USER_3,'') <> ''  



/* Quotes with Orders 1.0 */ 
select 
QT.QuoteNum, 
QT.custID, 
QT.QCustNum, 
QT.QCustName, 
QT.QDate, 
COUNT(OL.order_no) as #_Open_Orders, 
COUNT(OH.order_no) as #_Closed_Orders
from 
"Track Quotes".dbo.TrackQuotesHist as QT 
left outer join GlobalShop.dbo.v_order_hist_dtl_quote as OH 
on QT.QuoteNum = OH.USER_3 
left outer join GlobalShop.dbo.v_order_lines_quote as OL 
on QT.QuoteNum = OL.USER_3 
where 
QT.QDate between '03/01/2015' and '03/06/2015'
Group By  
QT.QuoteNum, 
QT.custID, 
QT.QCustNum, 
QT.QCustName, 
QT.QDate 

3 个答案:

答案 0 :(得分:0)

如果您只想在大多数时间运行select语句,我会将drop放入一个存储过程(如果您想要这样做),然后选择另一个存储过程。

答案 1 :(得分:0)

在我的世界里,我假设一个以v_开头的对象是一个视图,但你似乎将它视为一个表,如果它将它改为这样:

create proc dbo.procfiles
as
BEGIN  
    if object_id('GlobalShop.dbo.v_order_hist_dtl_Quote','U') is not null
      drop table GlobalShop.dbo.v_order_hist_dtl_Quote 

    if object_id('GlobalShop.dbo.v_order_lines_Quote','U') is not null
       drop table GlobalShop.dbo.v_order_lines_Quote 

select * into GlobalShop.dbo.v_order_hist_dtl_Quote 
from GlobalShop.dbo.v_order_hist_dtl where user_3 is not null  

select * into GlobalShop.dbo.v_order_lines_Quote 
from GlobalShop.dbo.v_order_lines where user_3 is not null    

/* Quotes with Orders 1.0 */ 
select 
QT.QuoteNum, 
QT.custID, 
QT.QCustNum, 
QT.QCustName, 
QT.QDate, 
COUNT(OL.order_no) as #_Open_Orders, 
COUNT(OH.order_no) as #_Closed_Orders
from 
"Track Quotes".dbo.TrackQuotesHist as QT 
left outer join GlobalShop.dbo.v_order_hist_dtl_quote as OH 
on QT.QuoteNum = OH.USER_3 
left outer join GlobalShop.dbo.v_order_lines_quote as OL 
on QT.QuoteNum = OL.USER_3 
where 
QT.QDate between '03/01/2015' and '03/06/2015'
Group By  
QT.QuoteNum, 
QT.custID, 
QT.QCustNum, 
QT.QCustName, 
QT.QDate 

begin catch 
select
ERROR_NUMBER(),
            ERROR_SEVERITY(),
            ERROR_STATE(),
            ERROR_PROCEDURE(),
            ERROR_LINE(),
            ERROR_MESSAGE()
end catch

end

答案 2 :(得分:0)

除非您每次运行此查询时都有非常令人信服的理由更新这些表,否则我会避免使用它们。只需在不使用表的情况下使用等效查询:

select 
    QT.QuoteNum, 
    QT.custID, 
    QT.QCustNum, 
    QT.QCustName, 
    QT.QDate, 
    COUNT(OL.order_no) as #_Open_Orders, 
    COUNT(OH.order_no) as #_Closed_Orders
from "Track Quotes".dbo.TrackQuotesHist as QT 
left outer join GlGlobalShop.dbo.v_order_hist_dtl OH 
on QT.QuoteNum = OH.USER_3
    and isnull(QT.QuoteNum,'')<>''
left outer join GlobalShop.dbo.v_order_lines OL 
on QT.QuoteNum = OL.USER_3
    and isnull(QT.QuoteNum,'')<>''
where QT.QDate between '03/01/2015' and '03/06/2015' 
Group By  
    QT.QuoteNum, 
    QT.custID, 
    QT.QCustNum, 
    QT.QCustName, 
    QT.QDate