2014-12-20 115 views
0

有一个存储过程运行良好时,我将整个东西作为SQL查询并运行它,但完全相同的过程使用相同的参数一个EXECUTE命令输出错误数据!存储过程无法通过EXECUTE正常工作,但作为一个巨大的SQL查询完美工作

EXECUTE qrm_pulsar_sandbox.[dbo].[sp_PL_Hedge] @CurrentDate = '12/17/2014', @PriorDate = '12/16/2014' 

以下是存储过程的作为查询的第一部分:

DECLARE @CURRENTDATE AS DATE 
DECLARE @PRIORDATE AS DATE 
SET @CURRENTDATE = '12/17/2014' 
SET @PRIORDATE = '12/16/2014' 

---------------Erroneous trades-------------- 
IF OBJECT_ID('tempdb..#ErrorTrades') is not null DROP TABLE #ErrorTrades 
Select * 
into #ErrorTrades 
from [qrm_pulsar].dbo.PnL_Tradeshistory 
where [CreationDt]<[email protected] and [CreationDt]>= @PriorDate 
and [DataVer]='0' and [Loan/Pool/Ticket Number]='1405200007' and [Portfolio ID] ='3' 

--------------------all trades------------------------- 
IF OBJECT_ID('tempdb..#All_Trades') is not null DROP TABLE #All_Trades 
select a.*, 
case when a.[OptType] in ('Call','Put') and a.[Instrument name] like '%T-Note%' then 'Options on Futures' 
when a.[OptType] in ('Call','Put') then 'MBS Options' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%Treasury%' then 'Bond Futures' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%Eurodollar%' then 'Eurodollar Futures' 

when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FN%' and right(rtrim(a.[Instrument Name]),1)='C' then 'Cash Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%GN%' and right(rtrim(a.[Instrument Name]),1)='C' then 'Cash Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FH%' and right(rtrim(a.[Instrument Name]),1)='C' then 'Cash Forwards' 

when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FN%' then 'MBS Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%GN%' then 'MBS Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FH%' then 'MBS Forwards' 
else' Error'end 'Type' 

,Case 
when a.[opttype]='Put' and a.[FACE]>0 then 'S' 
when a.[opttype]='Call' and a.[FACE]<0 then 'S' 
when a.[opttype]=''and a.[FACE]<0 then 'S' 
else 'B'end 'Sell/Buy' 

,case 
when a.[Loan/Pool/Ticket Number] in ('1403040003','1401060002','140724TYU1','140724TYU2','140926TYZ5') and a.[Portfolio ID]='7' then 'TRS-MSR' 
when a.[Loan/Pool/Ticket Number] in ('1401060002','1401070001','1312200001','1402040015','1403040019','140724TYU1','140724TYU2','140926TYZ6') and a.[Portfolio ID]='1' then 'PLS-MSR' 
when (rtrim(a.[Management]) <>'MSR' or a.[Management]is null)and a.[Portfolio ID]='1' then 'PLS-Pipeline' 
when (rtrim(a.[Management]) <>'MSR' or a.[Management]is null) and a.[Portfolio ID]='7' then 'TRS-Pipeline' 
when rtrim(a.[Management]) ='MSR' and a.[Portfolio ID]='1' then 'PLS-MSR' 
when rtrim(a.[Management]) ='MSR' and a.[Portfolio ID]='7' then 'TRS-MSR' 
when a.[Portfolio ID]='2' then 'TRS-Jumbo Acquisition' 
when a.[Portfolio ID]='3' then 'MBS Securities' 
when a.[Portfolio ID]='4' then 'Jumbo Securities' 
end 'Book' 

into #All_Trades 
from [qrm_pulsar].dbo.PnL_Tradeshistory a left join #ErrorTrades b 
on a.[Loan/Pool/Ticket Number]=b.[Loan/Pool/Ticket Number] and a.[Portfolio ID]=b.[Portfolio ID] 
where a.[CreationDt]<[email protected] and a.[CreationDt]>= @PriorDate 
and a.[DataVer]='0' and b.[Loan/Pool/Ticket Number] is null 

--Select * from #All_Trades where [Type]='Error' 

--------------------all trades no timing restriction to get type------------------------ 
IF OBJECT_ID('tempdb..#All_Trades2') is not null DROP TABLE #All_Trades2 
select distinct 
a.[Portfolio ID], 
a.[Loan/Pool/Ticket Number], 
case when a.[OptType] in ('Call','Put') and a.[Instrument name] like '%T-Note%' then 'Options on Futures' 
when a.[OptType] in ('Call','Put') then 'MBS Options' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%Treasury%' then 'Bond Futures' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%Eurodollar%' then 'Eurodollar Futures' 

when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FN%' and right(rtrim(a.[Instrument Name]),1)='C' then 'Cash Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%GN%' and right(rtrim(a.[Instrument Name]),1)='C' then 'Cash Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FH%' and right(rtrim(a.[Instrument Name]),1)='C' then 'Cash Forwards' 

when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FN%' then 'MBS Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%GN%' then 'MBS Forwards' 
when a.[OptType] not in ('Call','Put')and a.[Instrument name] like '%FH%' then 'MBS Forwards' 
else' Error'end 'Type' 

,Case 
when a.[opttype]='Put' and a.[FACE]>0 then 'S' 
when a.[opttype]='Call' and a.[FACE]<0 then 'S' 
when a.[opttype]=''and a.[FACE]<0 then 'S' 
else 'B'end 'Sell/Buy' 

,case 
when a.[Loan/Pool/Ticket Number] in ('1403040003','1401060002') and a.[Portfolio ID]='7' then 'TRS-MSR' 
when a.[Loan/Pool/Ticket Number] in ('1401060002','1401070001','1312200001','1402040015','1403040019') and a.[Portfolio ID]='1' then 'PLS-MSR' 
when (rtrim(a.[Management]) <>'MSR' or a.[Management]is null)and a.[Portfolio ID]='1' then 'PLS-Pipeline' 
when (rtrim(a.[Management]) <>'MSR' or a.[Management]is null) and a.[Portfolio ID]='7' then 'TRS-Pipeline' 
when rtrim(a.[Management]) ='MSR' and a.[Portfolio ID]='1' then 'PLS-MSR' 
when rtrim(a.[Management]) ='MSR' and a.[Portfolio ID]='7' then 'TRS-MSR' 
when a.[Portfolio ID]='2' then 'TRS-Jumbo Acquisition' 
when a.[Portfolio ID]='3' then 'MBS Securities' 
when a.[Portfolio ID]='4' then 'Jumbo Securities' 
end 'Book' 

into #All_Trades2 
from [qrm_pulsar].dbo.PnL_Tradeshistory a left join #ErrorTrades b 
on a.[Loan/Pool/Ticket Number]=b.[Loan/Pool/Ticket Number] and a.[Portfolio ID]=b.[Portfolio ID] 
where a.[CreationDt]<[email protected] --and a.[CreationDt]>= @PriorDate 
and a.[DataVer]='0' and b.[Loan/Pool/Ticket Number] is null 


--Select * from #All_Trades where [CreationDt]='5/16/2014' and [Loan/Pool/Ticket Number]='140429T038'--and [Portfolio ID]='1' 
--Select * from #All_Trades2 where [Loan/Pool/Ticket Number] ='140402TY01' 
--------------------all trade settlements------------------------- 


IF OBJECT_ID('tempdb..#All_Trades_PO') is not null DROP TABLE #All_Trades_PO 
select * 
,case 
when ((rtrim([In Ticket Management])<>'MSR'and rtrim([Out Ticket Management])<>'MSR') or ([In Ticket Management]is null and [Out Ticket Management]is null))and [Portfolio ID]='1' then 'PLS-Pipeline' 
when ((rtrim([In Ticket Management])<>'MSR'and rtrim([Out Ticket Management])<>'MSR') or ([In Ticket Management]is null and [Out Ticket Management]is null)) and [Portfolio ID]='7' then 'TRS-Pipeline' 
when (rtrim([In Ticket Management]) ='MSR' or rtrim([Out Ticket Management]) ='MSR')and [Portfolio ID]='1' then 'PLS-MSR' 
when (rtrim([In Ticket Management]) ='MSR' or rtrim([Out Ticket Management]) ='MSR') and [Portfolio ID]='7' then 'TRS-MSR' 
when [Portfolio ID]='2' then 'TRS-Jumbo Acquisition' 
when [Portfolio ID]='3' then 'MBS Securities' 
when [Portfolio ID]='4' then 'Jumbo Securities' 
end 'Book' 

into #All_Trades_PO 
from [qrm_pulsar].dbo.PnL_TradePairoffshistory 
where [CreationDt][email protected] 
and [DataVer]='0' 


--Select * from #All_Trades_PO where [In Ticket Number]='1403070007' 
--------------------all trade settlements Prior------------------------- 


IF OBJECT_ID('tempdb..#All_Trades_PO_Prior') is not null DROP TABLE #All_Trades_PO_Prior 
select * 
,case 
when ((rtrim([In Ticket Management])<>'MSR'and rtrim([Out Ticket Management])<>'MSR') or ([In Ticket Management]is null and [Out Ticket Management]is null))and [Portfolio ID]='1' then 'PLS-Pipeline' 
when ((rtrim([In Ticket Management])<>'MSR'and rtrim([Out Ticket Management])<>'MSR') or ([In Ticket Management]is null and [Out Ticket Management]is null)) and [Portfolio ID]='7' then 'TRS-Pipeline' 
when (rtrim([In Ticket Management]) ='MSR' or rtrim([Out Ticket Management]) ='MSR')and [Portfolio ID]='1' then 'PLS-MSR' 
when (rtrim([In Ticket Management]) ='MSR' or rtrim([Out Ticket Management]) ='MSR') and [Portfolio ID]='7' then 'TRS-MSR' 
when [Portfolio ID]='2' then 'TRS-Jumbo Acquisition' 
when [Portfolio ID]='3' then 'MBS Securities' 
when [Portfolio ID]='4' then 'Jumbo Securities' 
end 'Book' 

into #All_Trades_PO_Prior 
from [qrm_pulsar].dbo.PnL_TradePairoffshistory 
where [CreationDt][email protected] 
and [DataVer]='0' 



---------------------Open Position TL--------------------- 
IF OBJECT_ID('tempdb..#OPTL') is not null DROP TABLE #OPTL 
select 
[CreationDt], 
[DataVer], 
[Company], 
[Type], 
[Sell/Buy], 
[OptType], 
cast([Loan/Pool/Ticket Number]as varchar)'Ticket #', 
[FACE], 
abs([FACE])'absFACE', 
[Initial Value], 
[Current Value], 
[Inception Profit/Loss], 
[Instrument Name], 
[Trade Date], 
[Trade Price], 
[Counterparty Name], 
[Cost], 
[Coupon], 
[Settle], 
[Portfolio ID], 
[Management], 
[Book], 
[Forward Units] 
into #OPTL 
from #All_Trades a 
where 
[dataver]='0' 
and [CreationDt][email protected] 
Order 
by [Loan/Pool/Ticket Number] 

--Select * from #OPTL where [Ticket #]in ('1405200007') 



---------------------------------All Trades Beg vs. End --------------------- 
IF OBJECT_ID('tempdb..#TL') is not null DROP TABLE #TL 
Select 
a.[CreationDt] 
,a.[DataVer] 
,a.[Company] 
,a.[Type] 
,a.[Sell/Buy], 
case when a.[CreationDt][email protected] then 'Beg' else 'Additions' end 'Flag' 
,case when c.[Ticket #] IS not null then 'Open' else 'Settled' end 'Settled/Open' 
,a.[OptType] 
,a.[Loan/Pool/Ticket Number]'Ticket #' 
,(a.[FACE])'Beg Face' 
,(c.[FACE])'End Face' 
,abs(a.[FACE])'abs Beg Face' 
,abs(c.[FACE])'abs End Face' 
,a.[Initial Value]'Beg Initial Value' 
,a.[Current Value]'Beg Current Value' 
,a.[Inception Profit/Loss]'Beg P&L' 
,c.[Initial Value]'End Initial Value' 
,c.[Current Value]'End Current Value' 
,c.[Inception Profit/Loss]'End P&L' 
,a.[Instrument Name] 
,a.[Trade Date] 
,a.[Settle] 
,a.[Trade Price] 
,a.[Counterparty Name] 
,a.[Coupon] 
,a.[Portfolio ID], 
a.[Management] 
,a.[Book] 
,a.[Forward Units] 
into #TL 
from 
#All_Trades a left join #OPTL c on a.[Loan/Pool/Ticket Number]=c.[Ticket #]and a.[Book]=c.[Book] 
where 
(a.[DataVer]='0' 
and a.[CreationDt] in (Select MIN(CreationDt)from [qrm_pulsar].dbo.PnL_Tradeshistory b 
where a.[Loan/Pool/Ticket Number]=b.[Loan/Pool/Ticket Number] 
and a.[Portfolio ID]=b.[Portfolio ID] 
and [CreationDt]>= @PriorDate 
and [CreationDt]<= @CurrentDate 
and [DataVer]='0')) 


--select * from #TL where [Ticket #]='140529TYM2' [Type] is null --[Type] is null 
--select * from #All_Trades where [Loan/Pool/Ticket Number]='140529TYM2' and [Portfolio ID]='1' order by CreationDt--[Type] is null 
--select * from #OPTL where [Ticket #]='140307TY01' and [Portfolio ID]='1'--[Type] is null 
--select * from #TL where [Type] is null [Portfolio ID]='3'and [Portfolio ID]='1' 




-----------------------------Current Trade Settlement TL-------------------- 
IF OBJECT_ID('tempdb..#STL') is not null DROP TABLE #STL 
select * 
into #STL 
from #All_Trades_PO a 

where 
[dataver]='0' 
and 
a.[CreationDt] in (select Max(b.[CreationDt]) from [qrm_pulsar].dbo.PnL_TradePairoffshistory b 
where a.[In Ticket Number]= b.[In Ticket Number] 
and a.[Out Ticket Number]= b.[Out Ticket Number] 
and a.[Portfolio ID]=b.[Portfolio ID] 
and [CreationDt]>@PriorDate 
and [CreationDt]<[email protected] 
and [dataver]='0') 

--Select * from #STL where [In Ticket Number] ='1404040002' 

-----------------------------Trade Settlement AG join on out ticket number-------------------- 
IF OBJECT_ID('tempdb..#S1') is not null DROP TABLE #S1 
Select 
distinct 
a.[CreationDt], 
a.[DataVer], 
a.[Book], 
a.[pairoffBuySell]as 'Sell/Buy', 
b.[Type], 
'Additions' as 'Flag', 
'Settled' as 'Settled/Open', 
case when a.[OPTION_TYP]='C' then 'Call' 
when a.[OPTION_TYP]='P' then 'Put' 
else ' ' end 'OptType' 

除了在存储过程的结束只是END语句。

+0

使用EXECUTE命令的输出错误,因为数据与预期结果不符。同一个程序的输出作为一个整体运行,查询是完全正确的! – Amu

+0

即使我得到报酬来做到这一点,我也会对尝试调试这段代码感兴趣最小,使用更小的示例来重现错误并制定明确的目标,就像您现在拥有的以及您期望的那样 –

回答

1

SP和查询指向不同的数据库。 该查询被硬编码为指向qrm_pulsar数据库,但是您正在调用qrm_pulsar_sandbox数据库中的存储过程。

+1

在我看来,数据库都是使用三部分命名来指定的。 –

+0

如果使用3部分命名,并将SP应用于其他数据库,则SP仍将引用硬编码DB,而不是DB中的数据。 – Jeremy

+1

正确。所以无论是在查询还是在proc中,结果都应该是相同的。 –

1

检查存储过程创建的设置,例如如何处理NULL。如果您将数据库编写为脚本,您将在脚本的顶部看到这些脚本。然后检查运行“Giant SQL Query(tm)”时是否设置了相同的设置。

相关问题