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 8786219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:33:46+00:00 2026-06-13T21:33:46+00:00

Given this SQL statement that uses a cursor to loop through each row of

  • 0

Given this SQL statement that uses a cursor to loop through each row of a table and update a row in another table:

DECLARE @x varchar(100)
DECLARE @y varchar(100)
DECLARE c CURSOR FOR
SELECT col1, col2 FROM table
OPEN c
FETCH NEXT FROM c INTO @x, @y
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE table2 SET cola = @x WHERE colb = @y
FETCH NEXT FROM c INTO @x, @y
END
CLOSE c
DEALLOCATE c;

If I run this statement directly within SQL console or Navicat, everything works as expected. But if I run it in PHP using:

sqlsrv_query($sql)

only SOME of the rows in table2 are actually updated. It seems the cursor in the SQL statement does not properly execute, perhaps sqlsrv_query returns while the statement’s cursor is still running?

Is there a way to instruct sqlsrv_query to ‘wait’ until the statement is fully done executing before returning a result?

  • 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-13T21:33:47+00:00Added an answer on June 13, 2026 at 9:33 pm

    How about something like this instead of a cursor. The only reason I’ve ever had to write a cursor was to convert an older model into a newer model, in order to run some business rules and such for the conversion. I do not have any cursors running in production.

    update table2
        set cola = t1.col1
    from table2 t2
    join table1 t1 on t1.col2 = t2.colb
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no idea how to convert this SQL statement to LINQ that uses
Given this SQL: SELECT * FROM mytable ORDER BY mycolumn, RAND() Assuming that mycolumn
Is there a way to rewrite a Transact SQL statement that uses a CASE
So I have a SQL table that contains time-clock information like this. The number
I have this problem. Given a users table that consists of users' username in
I'm trying to write a sql statement that will insert data given a few
I need a regex that will determine if a given SQL statement has a
In a SQL Server 2008 R2 database, given this schema: AgentsAccounts _______________ AgentID int
when i give this sql query in my msaccess database table called warehouse1 it
Why does SQL server express 2008 give me this error? CREATE TABLE model (

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.