Can someone give me a couple of ideas about how to know if a cursor is necessary? I have a loop and I need to pull a variable line by line and immediately I'm thinking cursor, but after seeing all the warnings about not using cursors unless you really have to, I thought I'd ask for some input. Here's what I have:
the select @franchiseName returns a list of franchises. I would like to use one franchise name at a time to plug into my command line. The code above, pulls the same first franchise name equivalent to the number of franchises.
I'm wondering if this is a good instance to use a cursor, or if there's a better way? Thanks!
SR
Code:
SET @loop=0
WHILE @loop < @numberFranchises
BEGIN
USE master
SELECT @franchiseName = [Name] FROM master..sysdatabases
WHERE OBJECT_ID([Name]+'..invoice') IS NOT NULL
PRINT 'EXECUTE xp_cmdshell ''C:\batchfiles\' + @franchiseName + '-refresh.bat'
PRINT @franchiseName + ' Complete'
set @loop = @loop+1
END
the select @franchiseName returns a list of franchises. I would like to use one franchise name at a time to plug into my command line. The code above, pulls the same first franchise name equivalent to the number of franchises.
I'm wondering if this is a good instance to use a cursor, or if there's a better way? Thanks!
SR