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)
 STored Proc Not working correctly

Author  Topic 

sanjeet
Starting Member

5 Posts

Posted - 2005-12-14 : 11:18:39


I have a file list that display data like this in a data grid:

FILENAME DESCRIPTION
TESTING FOR TESTING FOR ONLINE moveup movedown
SUPPORTING FILE 3.txt dcsdcds 1 moveup movedown
01.00.00.SqlDataProvider.txt x zxghjghjgjhghj moveup movedown
test file add.txt test file add.txt 6 kjhk moveup movedown
dg.txt csdcsdcsd moveup movedown
update.txt update code UPDATE moveup movedown


The moveup and movedown are links that should allow the files to move in the correct order.

When the moveup or move down link is clicked a stored proc is run to handle the new sort order.


The problem is when the files gets updated with the new sort order, the old file file with the old sort order does not get updated, so the file list does not display the correct order.

The file belongs to a case. A Case can have multiple files.


The data in the database looks like this:


FILEID CASEID FILENAME FIELDECRSIPTION SORT ORDER
1 17 TESTING FOR TESTING FOR ONLINE 1
2 17 SUPPORTING FILE 3.txt dcsdcds 1 1
5 17 01.00.00.SqlDataProvider.txt x zxghjghjgjhghj 3
7 17 test file add.txt test file add.txt 6 kjhk 4
11 17 dg.txt csdcsdcsd 5
8 17 update.txt update code UPDATE 6
10 17 DIGARDI.txt YES!!!!!!!!!! 7
12 32 DNA1.txt DNA SUPPORT FILE 1 DON"T UPDATE 2
13 32 DNA2.txt DNA SUPPORT FILE 1 2
14 32 DNA3.txt DNA SUPPORT FILE 3 3

This is my stored proc:


CREATE PROCEDURE [dbo].[oc_OnlineCaseFileSortOrder]
/*****************************************************************************************
oc_OnlineCaseFileSortOrder
**************************************************************************
Description:
Moves the sort order of the file list
Output:
None
**************************************************************************
History:
12/14/05, ssk: Created
*****************************************************************************************/
@FileId int,
@CaseId int,
@Direction int

AS
SET NOCOUNT ON

/* Declarations */

Declare @CurrentSortOrder int,
@NewSortOrder int,
@AdjFileId int,
@AdjCaseId int

/*Get Current Info */

SELECT
@CurrentSortOrder = o.SortOrder,
@NewSortOrder = o.SortOrder + @Direction
FROM oc_onlinecasefile o
WHERE o.FileId = @FileId
And o.CaseId = @CaseId


/*Reorder the file list */


IF @Direction > 0
SELECT TOP 1 @AdjFileId= o.FileId, @AdjCaseId= o.CaseId
FROM dbo.oc_onlinecasefile o WITH (nolock)
WHERE o.FileId != @FileId
AND o.CaseId != @CaseId
AND o.SortOrder >= @NewSortOrder
ORDER BY o.SortOrder
ELSE
SELECT TOP 1 @AdjFileId= o.FileId, @AdjCaseId= o.CaseId
FROM dbo.oc_onlinecasefile o WITH (nolock)
WHERE o.FileId != @FileId
AND o.CaseId != @CaseId
AND o.SortOrder <= @NewSortOrder
ORDER BY o.SortOrder DESC



/*Set New Sort Order */

IF @AdjFileId IS NOT NULL

BEGIN

UPDATE oc_onlinecasefile
SET SortOrder = @NewSortOrder
WHERE FileId = @FileId
AND CaseId = @CaseId


UPDATE oc_onlinecasefile
SET SortOrder = @CurrentSortOrder
WHERE FileId = @AdjFileId
AND CaseId = @AdjCaseId


END


GO


TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-12-14 : 15:37:15
sanjeet, you should have added to this topic:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58995

Try out the code below. If it isn't doing what you need, continue your question on this topic.

use northwind
set nocount on

create table oc_onlinecasefile
([FILEID] int
,[CASEID] int
,[FILENAME] varchar(50)
,[FIELDECRSIPTION] varchar(20)
,[SORT ORDER] int)
go

insert oc_onlinecasefile
select 1, 17, 'TESTING FOR TESTING', 'FOR ONLINE', 1 union all
select 2, 17, 'SUPPORTING FILE 3.txt', 'dcsdcds 1', 2 union all
select 5, 17, '01.00.00.SqlDataProvider.txt x', 'zxghjghjgjhghj', 3 union all
select 7, 17, 'test file add.txt test file add.txt 6', 'kjhk', 4 union all
select 11, 17, 'dg.txt', 'csdcsdcsd', 5 union all
select 8, 17, 'update.txt update code', 'UPDATE', 6 union all
select 10, 17, 'DIGARDI.txt', 'YES!!!!!!!!!!', 7 union all
select 12, 32, 'DNA1.txt DNA SUPPORT', 'FILE 1 DON"T UPDATE', 1 union all
select 13, 32, 'DNA2.txt DNA SUPPORT', 'FILE 1', 2 union all
select 14, 32, 'DNA3.txt DNA SUPPORT', 'FILE 3', 3

go

create proc oc_OnlineCaseFileSortOrder
@fileid int
,@CaseId int
,@direction int
as

declare @sortOrder int
,@msg varchar(200)

--validate @fileid
--validate @direction

begin tran
--get sortOrder of @fileid
select @sortOrder = [sort Order]
from oc_onlinecasefile
where fileid = @fileid
and caseid = @caseid

if @sortOrder is null
begin
set @msg = 'invalid fileid caseid combination'
goto onError
end

if @direction < 0
begin
if not exists (
select 'tg'
from oc_onlinecasefile
where [sort Order] <= @sortOrder + @direction
and caseid = @caseid
)
begin
set @msg = 'file is already first in the list'
goto onError
end
end
else
begin
if not exists (
select 'tg'
from oc_onlinecasefile
where [sort Order] >= @sortOrder + @direction
and caseid = @caseid
)
begin
set @msg = 'file is already last in the list'
goto onError
end
end


--update all affected fileid sortOrders
update oc_onlinecasefile set
[sort Order] = [sort order] + sign(@direction) * -1
from oc_onlinecasefile
where caseid = @caseid
and [sort Order] >= case when @direction < 0 then @sortOrder + @direction else @sortOrder end
and [sort Order] <= case when @direction < 0 then @sortOrder else @sortOrder + @direction end

--set new sortOrder of @fileid
update oc_onlinecasefile set
[sort Order] = @sortOrder + @direction
from oc_onlinecasefile
where fileid = @fileid
and caseid = @caseid
commit tran

return 0

onError:
if @@trancount > 0 rollback tran
set @msg = isNull(@msg, 'an error occured')
raiserror (@msg, 18,1)
return -1

go


print 'original order'
select * from oc_onlinecasefile order by caseid, [sort Order]

print 'oc_OnlineCaseFileSortOrder 5, 17, -1'
exec oc_OnlineCaseFileSortOrder 5, 17, -1
select * from oc_onlinecasefile order by caseid, [sort Order]

print 'oc_OnlineCaseFileSortOrder 5, 17, 1'
exec oc_OnlineCaseFileSortOrder 5, 17, 1
select * from oc_onlinecasefile order by caseid, [sort Order]

go
drop proc oc_OnlineCaseFileSortOrder
drop table oc_onlinecasefile


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -