Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Stored procedure sp_for

By Cade Bryant,

The sp_for procedure provides simple single-statement loop processing for T-SQL statements, similar to the FOR command in the C language or the DOS command-line utility by the same name.

It's syntax is similar to that of the DOS command.  It takes an argument to represent a variable, initialize the variable, set an increment, set an end point, and a T-SQL command to run during the duration of the loop.

EXEC sp_for '@i', 1, 1, 5, 'PRINT OBJECT_NAME(@i)'

The above example would print the object names of all objects in the current database with an id between 1 and 5.  Although this would not be the most efficient way to do this specific operation (a set-oriented approach whould be better here), I have included this merely as an example.

Total article views: 876 | Views in the last 30 days: 1
 
Related Articles
FORUM

Command text was not set for the command object

Command text was not set for the command object

ARTICLE

Introduction to ADO - The Command Object

The third article in a four part series, this week Andy shows how to use the command object to work ...

ARTICLE

An introduction to using the ADO.NET - SqlCommand Object

Are you familiar with using the Command object in ADO? If so, migrating to ADO.NET should be a snap...

FORUM

PowerShell detailed reference

Command and object properties & methods

FORUM

Net commands

Net commands

Tags
miscellaneous    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones