Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8510267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:39:19+00:00 2026-06-11T03:39:19+00:00

If I run the below in MS SQL 2008 R2 I get an unexpected

  • 0

If I run the below in MS SQL 2008 R2 I get an unexpected result.

create table #DataTable (someID varchar(5))
insert into #DataTable 
values ('ID1'),('ID2'),('ID3'),('ID4'),('ID5')

declare @data varchar(8);

declare myCursor cursor for
select someID from #DataTable

open myCursor
FETCH NEXT FROM myCursor INTO
@data

WHILE(@@Fetch_Status >=0)
BEGIN 

    declare @tempTable table (someValue varchar(10))

    insert into @tempTable select @data + '_ASDF'
    select * from @tempTable    

FETCH NEXT FROM myCursor INTO
@data

END

close myCursor
deallocate myCursor

drop table #DataTable

Result of the last iteration:

someValue
ID1_ASDF
ID2_ASDF
ID3_ASDF
ID4_ASDF
ID5_ASDF

I haved expected only to see

someValue
ID5_ASDF

It seems that the table variable @tempTable is kept in scope between cursor iterations – but how is it then possible to re-declare the variable in each iteration ? Makes no sense to me.

I solved it by

delete @tempTable

in each iteration – which also backs up my assumption about it still being in scope.

Can anyone explain this behavior ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:39:20+00:00Added an answer on June 11, 2026 at 3:39 am

    Yes, it does – the scope isn’t defined by the begin / end statements, but by the end of a stored procedure, or a go

    The scope of a variable is the range of Transact-SQL statements that
    can reference the variable. The scope of a variable lasts from the
    point it is declared until the end of the batch or stored procedure in
    which it is declared.

    http://msdn.microsoft.com/en-us/library/ms187953(v=sql.105).aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run the script below, I get $VAR1 = [ 'ok0.ok]][[file:ok1.ok', undef, undef,
I got the following trigger on my sql server 2008 database CREATE TRIGGER tr_check_stoelen
I run an in-house application, programmed with visual studio 2008, that uses SQL Server
I'm trying to run following command on SQL Server 2008 DB sp_OACreate 'SQLXMLBulkLoad.SQLXMLBulkload', @object
I have a SQL Server 2008 stored procedure (pseudocode below) that goes as follows:
I have a stored procedure mentioned as below. I'm using SQL Server 2008 R2.
I'm using the below SQL command to add an index to a table. When
I have the below SQL query select to_date(start_date, 'dd-Mon-yy') AS start_date FROM table I
I'm trying to run the sql query below but I'm getting a 505 error
Below is my RUN_SQL function: def RUN_SQL_SAFE(sql, input_tuple=(), get_update_id=False, debug = False): conn =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.