SQL Server - Incorect sytax when creating a stored procedure

Asked By Sanjeet Koppikar
14-Dec-05 02:29 PM
When I add a file to a database I have to retreive the max sort order number add one to it and then use that value to populate the sort order field. 
I am getting an error message at my ELSE when I create this stored proc:

CREATE PROCEDURE [dbo].oc_OnlineCaseFileAdd
     @CaseId int,
     @FileId int,
     @FileName nvarchar(100),
     @FileDescription nvarchar(400)
     
AS

declare @ResultCount int
       
       

Select @ResultCount = Count(*) From oc_OnlineCaseFile
Where FileId = @FileId

IF @ResultCount = 0  /* if the file does not exist INSERT */

/* Retreive the highest sortorder */
Declare @HighestSortOrder int


Select @HighestSortOrder = Max(SortOrder) + 1
From oc_onlinecasefile
Where CaseId = @CaseId


INSERT INTO [dbo].oc_OnlineCaseFile   /* Do I Insert the File Id here, it was not before) */
     (
     [CaseId],
     [FileId],
     [FileName],
     [FileDescription],
     [SortOrder]
     ) 
     VALUES
     (
     @CaseId,
     @FileId,
     @FileName,
     @FileDescription,
     @HighestSortOrder
     )

ELSE /* if the file does exist UPDATE the File Info */  <<<<<<<<<<<<THIS IS WHERE IT IS GVING ME PROBLEMS, AT LEAST IN SQL ANAYZER <<<<<<<<<<<<<<<<<



EXEC [dbo].oc_OnlineCaseFileUpdate @FileId=@FileId, @FileDescription=@FileDescription



SELECT SCOPE_IDENTITY() AS NewIdentity, @ResultCount AS ResultCount

/* otherwise you run into a keyword issue with "Identity" */


GO

BEGIN END  BEGIN END

14-Dec-05 02:34 PM
Hi Sanjeet,

Since you have a set of commands after the IF statement, you have to put them inside a BEGIN END block, as well as the block of commands after your ELSE:

IF @ResultCount = 0 /* if the file does not exist INSERT */ 
BEGIN
/* Retreive the highest sortorder */ 
Declare @HighestSortOrder int 


Select @HighestSortOrder = Max(SortOrder) + 1 
From oc_onlinecasefile 
Where CaseId = @CaseId 


INSERT INTO [dbo].oc_OnlineCaseFile /* Do I Insert the File Id here, it was not before) */ 
( 
[CaseId], 
[FileId], 
[FileName], 
[FileDescription], 
[SortOrder] 
) 
VALUES 
( 
@CaseId, 
@FileId, 
@FileName, 
@FileDescription, 
@HighestSortOrder 
) 
END
ELSE
BEGIN
EXEC [dbo].oc_OnlineCaseFileUpdate @FileId=@FileId, @FileDescription=@FileDescription 

SELECT SCOPE_IDENTITY() AS NewIdentity, @ResultCount AS ResultCount 
END

Add BEGIN END  Add BEGIN END

14-Dec-05 02:39 PM
Your SP needs to look like
CREATE PROCEDURE p_test 
AS
BEGIn
//Statements go here
END

The only time you do not need this is when you have a Single Select Statement within your SP,

Not Really  Not Really

14-Dec-05 02:50 PM
In stored procedures, there's no need to put the block of code that comprise the stored procedure inside a BEGIN/END block.  This is only needed for user-defined functions.
THANKS Ronaldo, Still need help on the other stor  THANKS Ronaldo, Still need help on the other stor
14-Dec-05 03:07 PM
I have still having porblem with the other stored pro. I opened the thread about the file list not working correctly. The problem is sometimes when the files is at the second location and I click move up it does not take the 1st positio. This does not happen all the  tme only sometimes. I use query analyzer I noticed that when the file is at position 2 and I clcik  move up the sort order will change to 1 but the file id with the sortorder that already has 1 does not get updated to 2.

Any suggestions on that one?

Here is the stored proc again:

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
        
	
/*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
            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
            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 */


BEGIN

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

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


END




GO
true  true
14-Dec-05 04:10 PM
my mistake. Thanks for pointing it out.
Thanks what about my stored proc  Thanks what about my stored proc
14-Dec-05 04:59 PM
Sometimes when the file is at sortorder #2 and I click moeu it does not move up. The sortorder # in the dataabse changes but the file with the oldsot order does not change. 

Sometimes when I try moving files that belong toanther case up and down they don't work correctly. The File id with the old sort number does not get updated.

Thanks fo your help.


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
        
	
/*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 
BEGIN
            SELECT TOP 1 @AdjFileId= o.FileId
            FROM dbo.oc_onlinecasefile o WITH (nolock) 
            WHERE o.FileId != @FileId
            AND o.CaseId = @CaseId
            AND o.SortOrder >= @NewSortOrder 
            ORDER BY o.SortOrder 

END
      ELSE 

BEGIN
            SELECT TOP 1 @AdjFileId= o.FileId
            FROM dbo.oc_onlinecasefile o WITH (nolock) 
            WHERE o.FileId != @FileId
            AND o.CaseId = CaseId
            AND o.SortOrder <= @NewSortOrder 
            ORDER BY o.SortOrder DESC 
END



/*Set New Sort Order */


BEGIN

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


    

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


END




GO
Erroneous Line  Erroneous Line
14-Dec-05 05:48 PM
Look for the following line:

AND o.CaseId = CaseId 

and change it to:

AND o.CaseId = @CaseId
Whew it worked  Whew it worked
14-Dec-05 06:32 PM
Thanks ROnald, great attention to detail ;)
Create New Account
help
server SQL Server Do I have to set up a sql server database on a sql server? Or can I do this on any server SQL Server Setup Discussions SQL Server (1) CREATE DATABASE (1) Databases (1) Database (1) Create
Attach SQL Server 2008 database to SQL Server 2000 Server SQL Server Hi, I have created a database in SQL Server 2008, with compatibility level of SQL Server 2000. Now when I detach the DB
Linked server SQL Server HI its possible create a local sql server as a linked server In sql server 2000 and Sql server 2005 I thought sql server 2005 its not possible thanks
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit seconds left to the installation and bombed with the same errors as the Crystal Reports Server install. The error is the same whether I try to install MSDE by itself or
Backup on Sql server A and restore to Sql server B SQL Server Hi! I have two SQL Server 2008 Std (Sql Server A and Sql Server B) on Windows Server 2008 R2. SQL