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 GOSET ANSI_NULLS OFF GO-- Drop store procedure if it existsIF EXISTS (SELECT name FROM sysobjects WHERE name = N'dbo.procAcctStatusSort' AND type = 'P') DROP PROCEDURE dbo.procAcctStatusSortGOCREATE 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*******************************************************************************/ASSET NOCOUNT ONif 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 tableINSERT 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 tableDELETE FROM tmpAcctStatusIF @SortOrder = 1BEGIN 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_idENDELSE 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 szTmpName1ENDELSE IF @SortOrder = 3 BEGIN INSERT INTO tmpAcctStatus SELECT lTmpAccountNumber_id, szTmpTaxAccountNumber_id, lTaxUnit_id, lTmpStatusCode_id, szTmpSAlphaCode_nm, lTmpRollCode_id, szTmpRA