0

我有一个存储过程

ALTER PROCEDURE [dbo].[CallHistoryBasedOnPersonInfoPerEvent]
(
 @LastName varchar(50)
,@FirstName varchar(50)
,@Gender varchar(20)
,@StartDate varchar(20)
,@EndDate varchar(20) = null
)
AS
BEGIN 
SELECT     
    *
FROM         
        CallerInformation 
    INNER JOIN
        ActivityLog ON CallerInformation.CallerID = ActivityLog.CallerID 
    LEFT OUTER JOIN
        CaseStatus ON CallerInformation.CaseStatusID = CaseStatus.CaseStatusID
WHERE     
        (ActivityLog.ActivityID = 1) 
    AND
        ActivityLog.SearchParameter.value('(/Root/PersonSearch/LastName)[1]','varchar(50)') = @LastName
    AND
        ActivityLog.SearchParameter.value('(/Root/PersonSearch/FirstName)[1]','varchar(50)') = @FirstName
    AND
        ActivityLog.SearchParameter.value('(/Root/PersonSearch/Gender)[1]','varchar(20)') = @Gender
    AND
        ActivityLog.SearchParameter.value('(/Root/PersonSearch/StartDate)[1]','varchar(20)') = @StartDate
    AND
        ActivityLog.SearchParameter.value('(/Root/PersonSearch/EndDate)[1]','varchar(50)') = @EndDate
END 

在表中ActivityLogSearchParameter是一xml列。xml以这种格式存储在那里

<Root>
    <PersonSearch>
        <CallerID>1249</CallerID>
        <StartDate>2013-01-16T00:00:00</StartDate>
        <LastName>b</LastName>
        <FirstName>t</FirstName>
        <Gender>Male</Gender>
        <DOBDay>0</DOBDay>
        <DOBMonth>0</DOBMonth>
        <AgeRangeStart>33</AgeRangeStart>
        <AgeRangeEnd>44</AgeRangeEnd>
        <Country>United States</Country>
        <UserName>nojha</UserName>
    </PersonSearch>
</Root>

根据我存储过程中的连接,我必须选择ActivityLog. 、FirstNameLastName和位于 XML中GenderStartdateEnddate

现在我只面临日期问题。当我提供日期时,我在执行存储过程时没有得到任何记录。

startDate我通过我的代码发送的就是这种格式1/16/2013 12:00:00 AM

我知道有些不匹配。我尝试了几件事。我删除了 XML 中的时间,并将参数转换@startdate为日期。那没起效。然后我把@startdate日期改成了varchar(20)。那没起效。

我哪里错了

4

1 回答 1

1

如果你做对了,它工作很好——见这里:

-- define a table variable for demo purposes
DECLARE @table TABLE (ID INT, XMLDATA XML)

-- insert three rows with different <StartDate> values
INSERT INTO @table 
VALUES
   (1, '<Root><PersonSearch><StartDate>2013-01-16T00:00:00</StartDate></PersonSearch></Root>'),
   (2, '<Root><PersonSearch><StartDate>2013-01-18T00:00:00</StartDate></PersonSearch></Root>'),
   (3, '<Root><PersonSearch><StartDate>2013-01-16T00:00:00</StartDate></PersonSearch></Root>')

-- declare a search value, e.g. like the parameter of the stored procedure    
DECLARE @SearchDate DATE = '20130116'

-- select the rows from the table 
SELECT 
    ID
FROM
    @table
WHERE   
    XmlData.value('(/Root/PersonSearch/StartDate)[1]','date') = @SearchDate

ID选择了 #1 和 #3 - 正如预期的那样。

因此,如果您真的DATE按照应有的方式使用日期,那么它就可以工作

于 2013-10-21T19:01:06.270 回答