About 170,000 results
Open links in new tab
  1. Syntax of for-loop in SQL Server - Stack Overflow

    May 20, 2011 · For loop is not officially supported yet by SQL server. Already there is answer on achieving FOR Loop's different ways. I am detailing answer on ways to achieve different types …

  2. How do I loop through a set of records in SQL Server?

    The variable MAXID is used to LOOP through. The variable COUNTER is used to perform an operation on a particular record in the table. If I read the question it talks about " have a few …

  3. SQL Server FOR EACH Loop - Stack Overflow

    Jan 1, 2010 · SQL is primarily a set-orientated language - it's generally a bad idea to use a loop in it. In this case, a similar result could be achieved using a recursive CTE: with cte as (select 1 i …

  4. In SQL Server, how to create while loop in select

    Nov 10, 2013 · Select IDs from your table and use CROSS APPLY the function with data as argument so you'll have as many rows as values contained in the current row's data. No need …

  5. How can INSERT INTO a table 300 times within a loop in SQL?

    In SQL, you should generally try to write set-based code, not procedural, step by step code. So, the SQLish thing to ask for is "how do I insert 300 rows into my table, where each row gets a …

  6. sql - Updating database records in a loop? - Stack Overflow

    Feb 19, 2009 · A possible way that always works (even if there is no unique key available) is to use the rowid pseudocolumn: begin for i in (select rowid, emp.* from emp) loop if i.sal=1300 …

  7. sql - While Loop to Iterate through Databases - Stack Overflow

    Jan 18, 2011 · CREATE TABLE #T (dbname sysname NOT NULL PRIMARY KEY, SumPGCOUNT INT, CREATED DATETIME) DECLARE @Script NVARCHAR(MAX) = '' …

  8. Using a cursor with dynamic SQL in a stored procedure

    The "cursors are evil" opinion is much more prominent in the SQL Server community. So I guess this answer is to switch to Oracle or give MS a clue. Oracle EXECUTE IMMEDIATE into a …

  9. t sql - How to write a foreach in SQL Server? - Stack Overflow

    Of course, if you want to actually do something with each record in your loop (like send an email or write to a file) rather than just shuffle stuff around within SQL, you need to use a cursor or …

  10. Looping through column names with dynamic SQL - Stack Overflow

    Dec 9, 2013 · You can use dynamic SQL and get all the column names for a table. Then build up the script: Declare @sql varchar(max) = '' declare @tablename as varchar(255) = 'test' select …