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)
 Strange SQL Server Behavior

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-07-15 : 20:33:43
John S writes "I'm having a problem with a stored procedure that is running on SQL Server 7.0 (SP3). If I run a the commands as a SQL Batch, they work fine but when I run them in a stored procedure I get an unexpected result. The procedure takes a table, copies into a temporary table, deletes the original data, and copy's the data back to the original table sorted by 1 of 3 options. The stored procedure works fine when sorting by account number id (the first sort) but when I try to sort by name, or amount I get the weird behavior. For instance, on name the sort will start with the G's (and sort them in alpha order), go the A's, B's, and then C's in alpha, then skip back to the a's.

I'll paste the procdure for you to look at:

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF
GO

-- Drop store procedure if it exists
IF EXISTS (SELECT name
FROM sysobjects
WHERE name = N'dbo.procAcctStatusSort'
AND type = 'P')
DROP PROCEDURE dbo.procAcctStatusSort
GO



CREATE PROCEDURE dbo.procAcctStatusSort
@SortOrder int -- 1 = Acct No, 2 = Alpha, 3 = Desc Amt.
/******************************************************************************
** Name: procAcctStatusSort
** Auth: John Swaringen
** Date: 6/20/2001
** Desc: This procedure sorts the temporary tables according to the
** parameter that was passed in.
*******************************************************************************
** Change History
** Date: Author: Description:
** -------- -------- ---------------------------------------
**
**
** =============================================
** example to execute the store procedure
** =============================================
** EXECUTE procAcctStatusSort 1
** GO
*******************************************************************************/
AS

SET NOCOUNT ON

if exists (select * from dbo.sysobjects where id = object_id(N'[#tempAcctStatus]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [#tempAcctStatus]

CREATE TABLE #tempAcctStatus
(
[lTmpAccountNumber_id] [int] NULL ,
[szTmpTaxAccountNumber_id] [varchar] (30) NULL ,
[lTaxUnit_id] [int] NULL ,
[lTmpStatusCode_id] [int] NULL ,
[szTmpSAlphaCode_nm] [char] (4) NULL ,
[lTmpRollCode_id] [int] NULL ,
[szTmpRAlphaCode_nm] [char] (4) NULL ,
[szTmpName1] [varchar] (50) NULL ,
[mnyTmpLevyBal] [money] NULL ,
[szDeleteFlag] [char] (1) NULL
)

-- Create and Insert rows into temp table
INSERT INTO #tempAcctStatus
SELECT lTmpAccountNumber_id,
szTmpTaxAccountNumber_id,
lTaxUnit_id,
lTmpStatusCode_id,
szTmpSAlphaCode_nm,
lTmpRollCode_id,
szTmpRAlphaCode_nm,
szTmpName1,
mnyTmpLevyBal,
szDeleteFlag
FROM tmpAcctStatus

-- Delete from original table
DELETE FROM tmpAcctStatus

IF @SortOrder = 1
BEGIN
INSERT INTO tmpAcctStatus
SELECT lTmpAccountNumber_id,
szTmpTaxAccountNumber_id,
lTaxUnit_id,
lTmpStatusCode_id,
szTmpSAlphaCode_nm,
lTmpRollCode_id,
szTmpRAlphaCode_nm,
szTmpName1,
mnyTmpLevyBal,
szDeleteFlag
FROM #tempAcctStatus
ORDER BY lTmpAccountNumber_id
END
ELSE
IF @SortOrder = 2
BEGIN
INSERT INTO tmpAcctStatus
SELECT lTmpAccountNumber_id,
szTmpTaxAccountNumber_id,
lTaxUnit_id,
lTmpStatusCode_id,
szTmpSAlphaCode_nm,
lTmpRollCode_id,
szTmpRAlphaCode_nm,
szTmpName1,
mnyTmpLevyBal,
szDeleteFlag
FROM #tempAcctStatus
ORDER BY szTmpName1
END
ELSE IF @SortOrder = 3
BEGIN
INSERT INTO tmpAcctStatus
SELECT lTmpAccountNumber_id,
szTmpTaxAccountNumber_id,
lTaxUnit_id,
lTmpStatusCode_id,
szTmpSAlphaCode_nm,
lTmpRollCode_id,
szTmpRA
   

- Advertisement -