繁体   English   中英

SSRS - 将数据类型 nvarchar 转换为日期时间时出错

[英]SSRS - Error converting data type nvarchar to datetime

我有一个已知的工作报告,我向其中添加了一个参数和“if”语句。 添加后,我现在在 SSRS 中预览报告时收到错误:

将数据类型 nvarchar 转换为日期时间时出错

我最近添加的参数是char ,而不是datetimenvarchar所以我不清楚这个错误是否与新添加的参数有关。 如果我在 SSMS 中设置参数,我可以毫无问题地解析或运行查询。 自从查询的最后一个已知工作状态以来,我编辑了两个位置,它们在查询中进行了标记。 想法?

 CREATE PROCEDURE [dbo].[LRP_ContributionDetail_BASE_trn]
    (@fyear int= null,
     @fyear_end int = null,
     @start_dt_post datetime= null,
     @end_dt_post datetime=null,
     @list_no int = 0,
     @start_dt datetime = null,
     @end_dt datetime = null,
     @include_restricted char (1) = null --recently added parameter
    )
AS
    SET ANSI_WARNINGS OFF
    SET CONCAT_NULL_YIELDS_NULL OFF
    SET NOCOUNT ON

    if @fyear_end is null or @fyear_end = 0 
       set @fyear_end = @fyear

    if @start_dt_post is null 
       set @start_dt_post = '1/1/1900'

    if @start_dt is null 
       set @start_dt = '1/1/1900'

if @end_dt_post is null set @end_dt_post = GETDATE()
if @end_dt is null set @end_dt = GETDATE()


SELECT  CONT.ref_no As Contribution_ID,
        CONT.customer_no,
        CUST.lname,
        CUST.fname,
        CAT.id As Cat_ID,
        CAT.description AS Cat_Desc,
        CAMP.fyear As FiscalYear,
        CAMP.campaign_no As Camp_ID,
        CAMP.description AS Camp_Desc,
        FUND.fund_no As Fund_ID,
        FUND.description AS Fund_Desc,
        CDES.id As Desig_ID,
        CDES.description As Desig_Desc,
        CONT.cont_type,
        CONT.cont_dt,
        cont.cont_amt,
        CONT.recd_amt,
        (cont.cont_amt-CONT.recd_amt) AS Balance,
        TRN_AMT = SUM(x.trn_amt), --show actual transactions
        post_no = 0,--BAT.post_no,
        MORG.memb_org_no as MembType_ID,
        MORG.description as MembType_Desc,
        fund.nonrestricted_income_gl_no
into #work
FROM    T_TRANSACTION x
join    T_CONTRIBUTION cont on x.ref_no = cont.ref_no
        INNER JOIN T_CAMPAIGN CAMP ON x.campaign_no=CAMP.campaign_no
        INNER JOIN TR_CAMPAIGN_CATEGORY CAT ON CAMP.category=CAT.id
        INNER JOIN T_CUSTOMER CUST ON CONT.customer_no=CUST.customer_no
        join T_BATCH bat on x.batch_no = bat.batch_no
        join T_FUND fund on x.fund_no = fund.fund_no
        LEFT OUTER JOIN TR_CONT_DESIGNATION CDES ON CONT.cont_designation=CDES.id
        LEFT OUTER JOIN T_MEMB_ORG MORG ON CAMP.memb_org_no=MORG.memb_org_no
WHERE   
x.trn_type in (1,2,5,4) --gift/pledge
and (@fyear is null or camp.fyear between @fyear and @fyear_end)
and cont.cont_dt between @start_dt and @end_dt --contribution date
 and bat.posted_dt between isnull(@start_dt_post,'1/1/1900') and isnull(@end_dt_post,getdate()) and --posting date
 (Coalesce(@list_no, 0) = 0
        or Exists (Select * From [dbo].T_LIST_CONTENTS 
                    Where customer_no = cont.customer_no
                    and list_no = @list_no))
and cont.cont_amt > 0
--and cont.ref_no = 549924
GROUP BY 
cONT.ref_no,CONT.customer_no,CUST.lname,CUST.fname,CAT.id,CAT.description,
CAMP.fyear,CAMP.campaign_no,CAMP.description,FUND.fund_no,FUND.description,
CDES.id,CDES.description,CONT.cont_type,CONT.cont_dt,CONT.recd_amt,
MORG.memb_org_no,MORG.description, cont.cont_amt, fund.nonrestricted_income_gl_no

if @include_restricted = 'N' --recently added if statement
    begin
        --find where resticted activity takes place
        select b.ref_no, b.trn_type, trn_amt = case when b.trn_type = 11 then SUM(b.trn_amt)* (-1)
            else SUM(b.trn_amt) end
        into #rest
        from #work a
        join t_transaction b on a.Contribution_ID = b.ref_no
        where b.trn_type in (10,11)
        group by b.ref_no, b.trn_type

        select ref_no, rest_amt = SUM(trn_amt)
        into #rest_sum
        from #rest
        group by ref_no

        --delete where money is still restricted
        delete #work
        from #rest_sum a
        where a.ref_no = #work.Contribution_ID
        and a.rest_amt = #work.cont_amt

        --only show unresricted amount
        update #work
        set cont_amt = cont_amt - a.rest_amt,
        recd_amt = recd_amt - a.rest_amt
        from #rest_sum a
        where a.ref_no = #work.Contribution_ID
        and a.rest_amt > 0

        update #work
        set Balance = cont_amt - recd_amt
    end

select * from #work 

EXEC LUP_Log_ProcedureCall @ObjectID = @@PROCID;

您应该输入所有这样的日期:

YYYYMMDD

注意,那里没有连字符或破折号

查看此链接了解更多

在Visual Studio的“数据集属性”框中,我正在执行sproc并列出参数。 我从这里删除了新的@include_restricted参数,并在存储过程中设置了默认值,如下所示:

    if @include_restricted is null set @include_restricted = 'N'

还更改了存储过程的尾部以包含登录名,因为@include_restricted是“是”或“否”而不是“否”。 这是修改后的工作程序:

        USE [impresario]
GO
/****** Object:  StoredProcedure [dbo].[LRP_ContributionDetail_BASE_trn]    Script Date: 03/10/2015 10:31:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO






ALTER                   PROCEDURE [dbo].[LRP_ContributionDetail_BASE_trn](
                                    @fyear int= null,
                                    @fyear_end int = null,
                                    @start_dt_post datetime= null,
                                    @end_dt_post datetime=null,
     @list_no int = 0,
     @start_dt datetime = null,
     @end_dt datetime = null
     ,@include_restricted char (1) = null
     )

AS

SET ANSI_WARNINGS OFF
SET CONCAT_NULL_YIELDS_NULL OFF
SET NOCOUNT ON


if @fyear_end is null or @fyear_end = 0 set @fyear_end = @fyear

if @start_dt_post is null set @start_dt_post = '19000101'
if @start_dt is null set @start_dt = '19000101'

if @end_dt_post is null set @end_dt_post = GETDATE()
if @end_dt is null set @end_dt = GETDATE()

if @include_restricted is null set @include_restricted = 'N'

SELECT  CONT.ref_no As Contribution_ID,
        CONT.customer_no,
        CUST.lname,
        CUST.fname,
        CAT.id As Cat_ID,
        CAT.description AS Cat_Desc,
        CAMP.fyear As FiscalYear,
        CAMP.campaign_no As Camp_ID,
        CAMP.description AS Camp_Desc,
        FUND.fund_no As Fund_ID,
        FUND.description AS Fund_Desc,
        CDES.id As Desig_ID,
        CDES.description As Desig_Desc,
        CONT.cont_type,
        CONT.cont_dt,
        cont.cont_amt,
        CONT.recd_amt,
        (cont.cont_amt-CONT.recd_amt) AS Balance,
        TRN_AMT = SUM(x.trn_amt), --show actual transactions
        post_no = 0,--BAT.post_no,
        MORG.memb_org_no as MembType_ID,
        MORG.description as MembType_Desc,
        fund.nonrestricted_income_gl_no
into #work
FROM    T_TRANSACTION x
join    T_CONTRIBUTION cont on x.ref_no = cont.ref_no
        INNER JOIN T_CAMPAIGN CAMP ON x.campaign_no=CAMP.campaign_no
        INNER JOIN TR_CAMPAIGN_CATEGORY CAT ON CAMP.category=CAT.id
        INNER JOIN T_CUSTOMER CUST ON CONT.customer_no=CUST.customer_no
        join T_BATCH bat on x.batch_no = bat.batch_no
        join T_FUND fund on x.fund_no = fund.fund_no
        LEFT OUTER JOIN TR_CONT_DESIGNATION CDES ON CONT.cont_designation=CDES.id
        LEFT OUTER JOIN T_MEMB_ORG MORG ON CAMP.memb_org_no=MORG.memb_org_no
WHERE   
x.trn_type in (1,2,5,4) --gift/pledge
and (@fyear is null or camp.fyear between @fyear and @fyear_end)
and cont.cont_dt between @start_dt and @end_dt --contribution date
 and bat.posted_dt between isnull(@start_dt_post,'19000101') and isnull(@end_dt_post,getdate()) and --posting date
 (Coalesce(@list_no, 0) = 0
        or Exists (Select * From [dbo].T_LIST_CONTENTS 
                    Where customer_no = cont.customer_no
                    and list_no = @list_no))
and cont.cont_amt > 0
--and cont.ref_no = 549924
GROUP BY 
cONT.ref_no,CONT.customer_no,CUST.lname,CUST.fname,CAT.id,CAT.description,
CAMP.fyear,CAMP.campaign_no,CAMP.description,FUND.fund_no,FUND.description,
CDES.id,CDES.description,CONT.cont_type,CONT.cont_dt,CONT.recd_amt,
MORG.memb_org_no,MORG.description, cont.cont_amt, fund.nonrestricted_income_gl_no

if @include_restricted = 'Y'
    select * from #work

if @include_restricted = 'N' 
    begin
        --find where resticted activity takes place
        select b.ref_no, b.trn_type, trn_amt = case when b.trn_type = 11 then SUM(b.trn_amt)* (-1)
            else SUM(b.trn_amt) end
        into #rest
        from #work a
        join t_transaction b on a.Contribution_ID = b.ref_no
        where b.trn_type in (10,11)
        group by b.ref_no, b.trn_type

        select ref_no, rest_amt = SUM(trn_amt)
        into #rest_sum
        from #rest
        group by ref_no

        --delete where money is still restricted
        delete #work
        from #rest_sum a
        where a.ref_no = #work.Contribution_ID
        and a.rest_amt = #work.cont_amt

        --only show unresricted amount
        update #work
        set cont_amt = cont_amt - a.rest_amt,
        recd_amt = recd_amt - a.rest_amt
        from #rest_sum a
        where a.ref_no = #work.Contribution_ID
        and a.rest_amt > 0

        update #work
        set Balance = cont_amt - recd_amt

        select * from #work
    end 


EXEC LUP_Log_ProcedureCall @ObjectID = @@PROCID;

要将 nvarchar 转换为日期类型,并且在提取到 excel 时,过滤器类型也需要是数据类型过滤器,然后执行以下步骤

在报表设计中单击字段文本框的表达式

使用 CDate(Fields!postdate.vale) 函数转换为日期类型。 要获得所需的日期格式 hi 文本框属性和数字选项卡上选择自定义并提供格式样式,如 MM-dd-yyyy。 **Postdate 是字段名称结果:02-29-2021

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM