FIX: Stored Procedure Continues to Use Parameter Value Passed During First Execution

This article was previously published under Q288185
This article has been archived. It is offered "as is" and will no longer be updated.
BUG #: 101265 (SQLBUG_70)
SYMPTOMS
A stored procedure that accepts a parameter, which is used to determine how many columns to add to a temporary table that the stored procedure creates, might continue to use the value of the parameter passed the first time the procedure was executed, even though different values are passed for subsequent executions.
WORKAROUND
Create the procedure using a WITH RECOMPILE clause.
STATUS
Microsoft has confirmed this to be a problem in SQL Server 7.0. This problem has been corrected in U.S. Service Pack SP3 for Microsoft SQL Server 7.0. For information about how to download and install the latest SQL Server Service Pack, see the following Microsoft Web site:For more information, contact your primary support provider.
MORE INFORMATION

Steps to Reproduce Behavior

  1. Run the script below.
  2. Modify the script, commenting out the three lines as indicated by the comment in the script.
  3. Run the script again.
USE tempdbgoDROP PROCEDURE pr_TestProcgoCREATE PROCEDURE pr_TestProc @Columns INTASSET NOCOUNT ONDECLARE @SQL VARCHAR(1000)-- Now remove the next three lines (which do nothing valuable) and try again.CREATE TABLE #tmpJunk (JunkID INT)        -- COMMENT THIS LINEINSERT #tmpJunk (JunkID) VALUES (0)       -- COMMENT THIS LINEDROP TABLE #tmpJunk                       -- COMMENT THIS LINECREATE TABLE #tmpTbl (XX int)SET @SQL = 'ALTER TABLE #tmpTbl ADD 'WHILE @Columns > 0BEGIN SET @SQL = @SQL + '[Col' + CONVERT(VARCHAR(10), @Columns) + '] VARCHAR(20)'+ ',' SET @Columns = @Columns - 1ENDSET @SQL = LEFT(@SQL, LEN(@SQL) - 1)EXEC (@SQL)SELECT * FROM #tmpTblgoexec pr_TestProc 7exec pr_TestProc 3exec pr_TestProc 12go				
Properties

Article ID: 288185 - Last Review: 10/23/2013 16:11:02 - Revision: 3.1

  • Microsoft SQL Server 7.0 Standard Edition
  • kbnosurvey kbarchive kbbug kbfix kbsqlserv2000sp1fix KB288185
Feedback