操作数类型冲突日期与金钱不兼容

时间:2017-01-25 05:53:47

标签: sql-server stored-procedures

我有一个使用saledatestart / saledateend的存储过程来过滤要处理的行的选择:

string buildCS =
    @"CREATE PROCEDURE usp_combinedsearch
                @SaleId nvarchar(25)        = NULL,
                @City nvarchar(50)          = NULL,
                @ZipCode nvarchar(10)       = NULL,
                @County nvarchar(25)        = NULL,
                @JudgementAmountMin money   = NULL,
                @JudgementAmountMax money   = NULL,
                @AssessedValueMin money     = NULL,
                @AssessedValueMax money     = NULL,
                @saledatestart Date         = NULL,
                @saledateend Date           = NULL
                AS
                SELECT s.SaleId, s.CaseNumber, s.County, s.Mapsco, s.Plaintiff, s.Defendant, 
                s.Address, s.City, s.State, s.ZipCode, s.JudgementAmount, s.YearOfConstruction,
                s.LegalDescription, s.AssessedValue
                FROM Sale s
                WHERE (s.SaleId = @SaleId or @SaleId IS NULL)
                AND (s.City = @City OR @City IS NULL)
                AND (s.ZipCode = @ZipCode OR @ZipCode IS NULL)
                AND (s.County = @County OR @County IS NULL)
                AND (s.JudgementAmount >= @JudgementAmountMin OR @JudgementAmountMin IS NULL)
                AND (s.JudgementAmount <= @JudgementAmountMax OR @JudgementAmountMax IS NULL)
                AND (s.AssessedValue >= @AssessedValueMin OR @JudgementAmountMin IS NULL)
                AND (s.AssessedValue <= @AssessedValueMax OR @AssessedValueMax IS NULL) 
                AND ('s.SaleDate' >= '@saledatestart' OR '@saledatestart' IS NULL)
                ORDER BY s.SaleId
                OPTION (RECOMPILE)";

我称之为:

if (!string.IsNullOrEmpty(TbAssessedValueMin.Text))
        {
            build =
                build.Append("DECLARE @AssessedValueMin money " + "= '" +
                             decimal.Parse(TbAssessedValueMin.Text, NumberStyles.Currency) + "' ");
        }
        else
        {
            build = build.Append("DECLARE @AssessedValueMin money ");
        }

        if (!string.IsNullOrEmpty(TbAssessedValueMax.Text))
        {
            build =
                build.Append("DECLARE @AssessedValueMax money " + "= '" +
                             decimal.Parse(TbAssessedValueMax.Text, NumberStyles.Currency) + "' ");
        }
        else
        {
            build = build.Append("DECLARE @AssessedValueMax money ");
        }

        if (_saledateStart != DateTime.MinValue)
        {
            build =
                build.Append("DECLARE @saledatestart date " + "= '" + _saledateStart + "' ");
        }
        else
        {
            build = build.Append("DECLARE @saledatestart date ");
        }

        if (_saledateEnd != DateTime.MinValue)
        {
            build =
                build.Append("DECLARE @saledateend date  " + "= '" + _saledateEnd + "' ");
        }
        else
        {
            build = build.Append("DECLARE @saledateend date ");
        }

        build = build.Append("EXECUTE @RC = [dbo].[usp_combinedsearch] " +
                             "@City " +
                             ",@ZipCode " +
                             ",@County " +
                             ",@JudgementAmountMin " +
                             ",@JudgementAmountMax " +
                             ",@AssessedValueMin " +
                             ",@AssessedValueMax " +
                             ",@saledatestart " +
                             ",@saledateend  " +
                             ";");

SQL Server中的行是SaleDate,定义为date,而不是null。当我运行存储过程时,我得到操作数类型的冲突日期与金钱不兼容。如果我从过程中删除所有对日期的引用,它可以正常工作。所以似乎问题在于我如何处理saledate,但我似乎无法弄清楚我做错了什么。任何帮助将不胜感激。

汤姆

0 个答案:

没有答案