简体   繁体   中英

SQL Conversion failed when converting row number

So I've been trying to add an Dynamic Row Number column with out using Dynamic SQL. However when I try I get an error 'Conversion failed when converting character string to smalldatetime data type.'

I Know the Error is coming from in the functions So if you want to just look at the switch case in the function that is the problem, but here is the stored procedure just in case you need to see it.

I have a store procedure which looks like this:

ALTER PROCEDURE [dbo].[MMS_EdgateMainQueue] 
-- Add the parameters for the stored procedure here
@OrderByColumnID int = 3,
@Skip int = 0,
@Take int = 0,
@Descending bit = 1,
@ResultCount INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;




Declare @UrlTitlePrefix varchar(2080) = '<a href="/Title/PageByExtTitleID?ActionName=Edgate&ExtTitleID=' 
Declare @UrlProducerPrefix varchar(2080) = '<a href="/Producers/ByExtVendorID?ActionName=Details&ExtVendorID=' 
Declare @Urlmidfix varchar(100) = '">' 
Declare @UrlPostFix varchar(100) = '</a>' 



SELECT TOP (@Take)
        [row_numb],
        @UrlTitlePrefix  + ExtTitleID + @Urlmidfix + ExtTitleID + @UrlPostFix as [Item #], 
        f.Title as Name,
        @UrlProducerPrefix + f.ExtVendorID + @Urlmidfix + f.DisplayName + @UrlPostFix as Producer,
        f.Created as Created,
        isnull(f.Academic, '') as Academic,
        isnull(f.Sears,'') as Sears,
        isnull(f.Editor, '') as Editor,
        CONVERT(INT, f.[Copy]) AS Copy,
        f.[Segment],
        CONVERT(INT, f.[Taxonomy]) AS Taxonomy,
        f.[Priority]
    FROM EdgateNewTitlesInnerQuery(@OrderByColumnID, @Descending) as f
Where f.[row_numb] Between  ((@Skip * @Take) + 1)  and ((@Skip + 1) * @Take) order by  f.[row_numb]


END

And The Inner Function Looks Like:

ALTER FUNCTION [dbo].[EdgateNewTitlesInnerQuery]
(   
  @OrderByColumnID int,
  @Descending bit 

)
RETURNS TABLE 
AS
RETURN 
(


SELECT DISTINCT 
                v.ExtVendorID, 
                t.ID, 
                t.ExtTitleID, 
                t.Title, 
                v.DisplayName, 
                t.Created, 
                ecs.Title as [Academic],
                ssub.Title as [Sears],
                etw.EditorName as [Editor],
                etw.CopyDone AS [Copy], 
                etw.SegmentsStatus as [Segment],
                etw.TaxonomyDone AS [Taxonomy],
                CASE WHEN wft.[Priority] is null THEN 0 ELSE wft.[Priority] END as [Priority],
                --row_number() OVER (ORDER BY t.Created DESC) AS [row_number]

 row_number() OVER (ORDER BY 

            CASE @OrderByColumnID WHEN 0 THEN t.ExtTitleID
                WHEN 1 THEN t.Title
                WHEN 2 THEN v.DisplayName
                WHEN 3 THEN t.Created
                WHEN 4 THEN ecs.Title
                WHEN 5 THEN ssub.Title
                WHEN 6 THEN etw.EditorName
                WHEN 7 THEN etw.CopyDone
                WHEN 8 THEN etw.SegmentsStatus
                WHEN 9 THEN etw.TaxonomyDone
                WHEN 10 THEN CASE WHEN wft.[Priority] is null THEN 0 ELSE wft.[Priority] END 
                ELSE t.Created
END DESC ) AS [row_numb]

FROM  [Title] t 
join EdgateTitleWorkflow etw on etw.FK_TitleID = t.ID
join Vendor v on v.ExtVendorID = t.ProducerID
join CollectionItem i on i.TitleID = t.ID and i.CollectionID = 16
left join [EdgateSuggestedAcademicSubject] esas on esas.FK_TitleID = t.ID and esas.isPrimary = 1
left join EC_Subject ecs on ecs.ID = esas.FK_SubjectID
left join [FMGSuggestedSears] fss on fss.FK_TitleID = t.ID and fss.isPrimary = 1
left join [FMGSearsSubjects] ssub on ssub.ID = fss.SearsSubjectID and ssub.ParentID is null
left join [WorkFlow_Tracker] wft on wft.TitleID = t.ID
where (etw.CopyDone = 0 or etw.TaxonomyDone = 0 or etw.SegmentsStatus = 0)

)

I've tried passing this in as a string originally but it just didn't sort at all. So I was looking at similar problems and tried this solution Here but my switch Case is now throwing a Conversion Error. Does anyone have an Idea on how to fix this?

The problem is that case is an expression that returns one type, defined during compilation. You can fix this with a separate case for each key. I think this is the statement you want:

row_number() OVER (ORDER BY (CASE WHEN @OrderByColumnID = 0 THEN t.ExtTitleID END),
                            (CASE WHEN @OrderByColumnID = 1 THEN t.Title END),
                            (CASE WHEN @OrderByColumnID = 2 THEN v.DisplayName END),
                            (CASE WHEN @OrderByColumnID = 3 THEN t.Created END),
                            (CASE WHEN @OrderByColumnID = 4 THEN ecs.Title END),
                            (CASE WHEN @OrderByColumnID = 5 THEN ssub.Title END),
                            (CASE WHEN @OrderByColumnID = 6 THEN etw.EditorName END),
                            (CASE WHEN @OrderByColumnID = 7 THEN etw.CopyDone END),
                            (CASE WHEN @OrderByColumnID = 8 THEN etw.SegmentsStatus END),
                            (CASE WHEN @OrderByColumnID = 9 THEN etw.TaxonomyDone END),
                            (CASE WHEN @OrderByColumnID = 10 THEN COALESCE(wft.[Priority], 0) END) 
                            t.Created DESC
                  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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