i was just working with cursor in SQL 2008 but i face some problems. the first is that with the line i declared the cursor every time i execute the query it gives me an error. which says the cursor with such a name already exists. i want to offer me a way that if the cursor exists do not create it and if it is not defined yet sql creates it. i ad that i have closed and deallocated the cursor at the end of the code but the problem still resides.
declare c1 cursor fast_forward for select CustomerID, [Customer Name], [Product Name],
Maximum from favorit;
open c1
fetch next from c1 into @custID, @custName, @prodName, @max;
while @@FETCH_STATUS = 0
begin
PRINT @CustID, @custName;
fetch next from c1 into @custID, @custName, @prodName, @max;
end
close c1
deallocate c1
i have another question about print function in sql with the line below
PRINT @CustID, @custName;
is invalid?? what is the syntax in SQL and how can we print several variables in SQL, if it is possible please suggest a way to con cat two variables and some strings between them for example like below:
print @custID + 'this is the customer name' + @custName + 'etc'
just one other question for each line i print i want to print the number of line for example some thing like this:
1 customer name customer id ...
2 customer name customer id ...
...
end of cursor.
how would you offer to implement and print those numbers in the while statement of cursor
?