存储过程在visual studio中提供错误,但不是sql server

时间:2013-03-07 21:50:34

标签: sql-server-2005 visual-studio-2008 datetime stored-procedures

我有一个奇怪的问题,我甚至不确定如何排除故障。我正在使用VS2008和SQL Server 2005(是的,我知道它的2013年: - ))。

我们在Visual Studio中有以下代码,基本上传入一个变量,然后使用存储过程创建一个字符串,然后使用作为参数传递的变量执行SP。

奇怪的是,我们收到错误陈述

  

从字符串转换日期时间

时转换失败

在大多数情况下,这不是一个不寻常的错误,但在这里它变得有趣。

如果我取destCmd.Commandtext字符串值并将其粘贴到SQL Server并执行,它会快速处理并且没有任何错误。更具讽刺意味的是,存储过程不会返回任何记录(这是正确的)。

回顾一下,一个存储过程,当在SQL Server中运行时正确处理并且不返回任何记录(准确),但是从Visual Studio执行时相同的SP以某种方式发现数据转换错误(即使SP不应该&#39) ; t返回任何记录)。这种行为不是我之前见过的,也不知道如何解决问题。

我意识到我可以通过重新执行我的SP来解决这个问题,但是由于SP在SQL Server中工作,我想知道为什么我在SP时遇到错误的根本原因从VS2008处理。

存储过程的副本添加在下面的c#代码下面。

for (int ii = 0; ii < tmpCountryList.Length; ii++)
                    {
                        SuppLog.WriteLog(_logGuid, "Calibrating premium", "Start");
                        info1 = "Calibrating premium";
                        destCmd.CommandText = "exec proc_s6_to_pcw_calibration " + tmpCountryList[ii];
                        destCmd.ExecuteNonQuery();
                        SuppLog.WriteLog(_logGuid, "Calibrating premium", "Finish");
                    }

存储过程:

CREATE procedure [dbo].[proc_s6_to_pcw_calibration] @countryKey int 
as

--declare @countryKey int
--set @countryKey = 100026

delete a 
from [MPAG Calibration] a 
    inner join [master policy allocation group] b 
on a.[mpag key] = b.[mp allocation group key]
where b.[country key] = @countryKey


INSERT INTO [MPAG Calibration]
([MPAG Key],[Book Date Key],[Collected Premium LOCL],[PCW Date Key],[PCW GWP],[calibration],[Is Capped])

    select s6.[mp allocation group key]
        ,s6.[book date key], s6.TOTAL_LOCL
        ,s6.[pcw date key], PCW_GWP = isnull(pcw.GWP,0)
        ,calibration = 
                case 
                    when s6.TOTAL_LOCL = 0  
                        then 1 
                    else 
                        case when isnull(pcw.GWP,0) / s6.TOTAL_LOCL > s6.[scaling cap] then 1 else isnull(pcw.GWP,0) / s6.TOTAL_LOCL end 
                 end
        ,capped = 
                case 
                    when s6.TOTAL_LOCL = 0  
                        then 1 
                    else 
                        case when isnull(pcw.GWP,0) / s6.TOTAL_LOCL > s6.[scaling cap] then 1 else 0 end 
                 end
    from 
    (
        select 
            [mp allocation group key] = case when camp.[mp allocation group key] > 0 then camp.[mp allocation group key] else sp.[mp allocation group key] end
            --,[month shift] = case when camp.[mp allocation group key] > 0 then camp_mpag.[month shift] else sp_mpag.[month shift] end
            ,[scaling cap] = case when camp.[mp allocation group key] > 0 then camp_mpag.[scaling cap] else sp_mpag.[scaling cap] end
            ,[book date key]
            ,[pcw date key] = cast(convert(char(8),dateadd(d,-1,dateadd(m,case when camp.[mp allocation group key] > 0 then camp_mpag.[month shift] else sp_mpag.[month shift] end+1,dateadd(d,1-day(convert(datetime,cast([book date key] as char(8)))),convert(datetime,cast([book date key] as char(8)))))),112) as int)
            ,TOTAL_LOCL = sum(fact.[direct premium] * TOUSD.[to usd] * TOLOCAL.[from usd])
        from stage.premium fact
            left outer join [dim campaign split] split on fact.[campaign split key] = split.[campaign split key]
            left outer join [dim campaign] camp on split.[campaign key] = camp.[campaign key]
            left outer join [dim sponsor] sp on camp.[sponsor key] = sp.[sponsor key]
            left outer join [master policy allocation group] sp_mpag on sp.[mp allocation group key] = sp_mpag.[mp allocation group key]
            left outer join [master policy allocation group] camp_mpag on camp.[mp allocation group key] = camp_mpag.[mp allocation group key]
            left outer join [dim country] country on camp.[country key] = country.[country key]
            left outer join [v_fx] TOUSD on camp.[currency key] = TOUSD.[currency key]
            left outer join [v_fx] TOLOCAL on country.[currency key] = TOLOCAL.[currency key]
        where camp.[line of business key] = 100001
            and sp.[sponsor group key] = 100002 and camp.[sponsor key] > 0 
            and fact.[book date key] >= TOUSD.[effective date key]
            and fact.[book date key] < TOUSD.[expiration date key]
            and fact.[book date key] >= TOLOCAL.[effective date key]
            and fact.[book date key] < TOLOCAL.[expiration date key]
            and fact.[book date key] >= case when camp.[mp allocation group key] > 0 then camp_mpag.[scaling begin date key] else sp_mpag.[scaling begin date key] end
            and fact.[book date key] <= case when camp.[mp allocation group key] > 0 then camp_mpag.[scaling end date key]  else sp_mpag.[scaling end date key] end
            and camp.[country key] = @countryKey

        group by 
            case when camp.[mp allocation group key] > 0 then camp.[mp allocation group key] else sp.[mp allocation group key] end
            --,case when camp.[mp allocation group key] > 0 then camp_mpag.[month shift] else sp_mpag.[month shift] end
            ,case when camp.[mp allocation group key] > 0 then camp_mpag.[scaling cap] else sp_mpag.[scaling cap] end
            ,[book date key]
            ,cast(convert(char(8),dateadd(d,-1,dateadd(m,case when camp.[mp allocation group key] > 0 then camp_mpag.[month shift] else sp_mpag.[month shift] end+1,dateadd(d,1-day(convert(datetime,cast([book date key] as char(8)))),convert(datetime,cast([book date key] as char(8)))))),112) as int)
    ) s6 
    left outer join 
    (
        select c.[mp allocation group key], a.[date key], sum(isnull(a.[gwp],0)) as gwp 
        from [Fact PCW] a
            left outer join [Master Policy] b on a.[master policy key] = b.[master policy key]
            left outer join [Master Policy Allocation Group] c on b.[mp allocation group key] = c.[MP allocation group key]
        where c.[country key] = @countryKey
        group by c.[mp allocation group key], a.[date key]
    ) pcw 
        on s6.[mp allocation group key] = pcw.[mp allocation group key]
            and s6.[pcw date key] = pcw.[date key]

0 个答案:

没有答案