in Education by
Let's say I have the following simple table variable: declare @databases table ( DatabaseID int, Name varchar(15), Server varchar(15) ) -- insert a bunch rows into @databases Is declaring and using a cursor my only option if I wanted to iterate through the rows? Is there another way? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
First of all you should be absolutely sure you need to iterate through each row — set based operations will perform faster in every case I can think of and will normally use simpler code. Depending on your data it may be possible to loop using just SELECT statements as shown below: Declare @Id int While (Select Count(*) From ATable Where Processed = 0) > 0 Begin Select Top 1 @Id = Id From ATable Where Processed = 0 --Do some processing here Update ATable Set Processed = 1 Where Id = @Id End Another alternative is to use a temporary table: Select * Into #Temp From ATable Declare @Id int While (Select Count(*) From #Temp) > 0 Begin Select Top 1 @Id = Id From #Temp --Do some processing here Delete #Temp Where Id = @Id End The option you should choose really depends on the structure and volume of your data. Note: If you are using SQL Server you would be better served using: WHILE EXISTS(SELECT * FROM #Temp) Using COUNT will have to touch every single row in the table, the EXISTS only needs to touch the first one (see Josef's answer below).

Related questions

0 votes
    I have the following simplified query which should filter the decrypted column values SELECT ResourceId, ClientId, ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I am using SQL Server 2014 and I have the following T-SQL query which pulls data from a View Table: SELECT * from MyView ... '2014-07-01' AND CreatedOn = '2014-07-01' AND CreatedOn...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    How can I determine the space used by a table variable without using DATALENGTH on all columns? eg: DECLARE ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    key is used to move the cursor in the adjoining cell of a table Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    As far as I know, in gcc you can write something like: #define DBGPRINT(fmt...) printf(fmt); Is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I want to get a value from a particular location from list variable. Right now what I see is it only ... from a specified location? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I want to get a value from a particular location from list variable. Right now what I see is it only ... from a specified location? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    write a program to input a number and print table of the number using while loop Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    What I'd like to do is iterate through a navigation's list items, and fade each element in, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    write algorithm and flowchart the inputs two numbers swaps these values without using third variable Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    I'm basically trying to build a game board. For simplicity's sake, imagine a Jeopardy! board. I'd ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    How do you load first and last records into target table? How many ways are there to do it? Explain through mapping flows...
asked Mar 28, 2021 in Technology by JackTerrance
0 votes
    Is there an easy way to sift through your guest users?...
asked Mar 10, 2021 in Technology by JackTerrance
...