Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 order issues in dynamic and paginated stored procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-06-07 : 07:32:16
Joey writes "Developing on : Windows XP PRO with MSDE2000
Production Server: Win2k with SQL2000

OK, 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 table
CREATE 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. table
INSERT INTO #TempItems (EventLog, RecordNumber, TimeGenerated, EventID, EventType,EventTypeName, EventCategory, SourceName, ComputerName, Message)
SELECT EventLog, RecordNumber, TimeGenerated, EventID, EventType,EventTypeName, EventCategory, SourceName, ComputerName, Message
FROM tblEvents
where (TimeGenerated between @startdate and @enddate)
AND (ComputerName like '%' + @whichcomp + '%')
AND (EventLog like '%' + @cat1 + '%')
AND (EventTypeName like '%' + @cat2 + '%')
ORDER BY CASE WHEN @orderby = 2 THEN ComputerName
WHEN @orderby = 3 THEN TimeGenerated
WHEN @orderby = 4 THEN EventID
WHEN @orderby = 5 THEN SourceName
ELSE RecordNumber
END

-- Find out the first and last record we want
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@Page - 1) * @iRecordsPerPage
SELECT @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 #TempItems
WHERE ID > @FirstRec AND ID < @LastRec
ORDER by TimeGenerated

-- Turn NOCOUNT back OFF
-- SET NOCOUNT OFF
GO
-------- END STORED PROCEDURE CODE --------"
   

- Advertisement -