Joey writes "Developing on : Windows XP PRO with MSDE2000Production Server: Win2k with SQL2000OK, I have a store procedure ( shown below ). I feed it several parameters, as you can see by the starting code, and the user gets to specify how many records come back per page, which computer to see event log alerts for and which categories of alerts are important. All that part works fine, but its when I start specifying the order for them to come back in, that I get the problem. I found out how to do dynamic order using a CASE ELSE statement. And as long as I send it the value corresponding to a field or column that contains numbers, like EventID or TimeGenerated, it will return exactly everything you want it to and in the exact order you asked it for. But if I send it the value that would tell it to ORDER the results by a column or field that contains text, such as ComputerName, it comes back with the following error. "[Microsoft][ODBC SQL Server Driver][SQL Server]Syntax error converting datetime from character string. " Now I know what this error usually means, but in this instance, I cant figure out where I am going wrong, or where I am trying to convert a string to or from DateTime.Please Help!Also as a side note. I have been trying to manipulate this stored procedure so that it will output the total number of records that match my search criteria. IE. There are 86 records that match my search criteria, but we will only be displaying them 10 at a time per page. I want to be able to display "Total Results: 96" at the top of the asp pages so the user knows how many pages of records they will be paging through.Thanks in advance for any help you may be able to give.-------- BEGIN STORED PROCEDURE CODE --------CREATE PROCEDURE sp_PagedItems_TEST ( @Page int, @iRecordsPerPage int, @startdate datetime, @enddate datetime, @whichcomp varchar(256), @cat1 varchar(25), @cat2 varchar(25), @orderby int )AS-- We don't want to return the # of rows inserted-- into our temporary table, so turn NOCOUNT ON-- SET NOCOUNT ON--Create a temporary tableCREATE TABLE #TempItems( ID int IDENTITY, EventLog varchar(256), RecordNumber int, TimeGenerated datetime, EventID int, EventType int, EventTypeName varchar(256), EventCategory int, SourceName varchar(256), ComputerName varchar(256), Message varchar(512),)-- Insert the rows from tblEvents into the temp. tableINSERT INTO #TempItems (EventLog, RecordNumber, TimeGenerated, EventID, EventType,EventTypeName, EventCategory, SourceName, ComputerName, Message)SELECT EventLog, RecordNumber, TimeGenerated, EventID, EventType,EventTypeName, EventCategory, SourceName, ComputerName, MessageFROM tblEventswhere (TimeGenerated between @startdate and @enddate)AND (ComputerName like '%' + @whichcomp + '%')AND (EventLog like '%' + @cat1 + '%')AND (EventTypeName like '%' + @cat2 + '%')ORDER BY CASE WHEN @orderby = 2 THEN ComputerNameWHEN @orderby = 3 THEN TimeGeneratedWHEN @orderby = 4 THEN EventIDWHEN @orderby = 5 THEN SourceNameELSE RecordNumberEND-- Find out the first and last record we wantDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @iRecordsPerPageSELECT @LastRec = (@Page * @iRecordsPerPage + 1)-- Now, return the set of paged records, plus, an indiciation of we-- have more records or not!SELECT *, MoreRecords = ( SELECT COUNT(*) FROM #TempItems TI WHERE TI.ID >= @LastRec ) FROM #TempItemsWHERE ID > @FirstRec AND ID < @LastRecORDER by TimeGenerated-- Turn NOCOUNT back OFF-- SET NOCOUNT OFFGO
-------- END STORED PROCEDURE CODE --------"