views:

29

answers:

2

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 ?

A: 

you can declare cursor once on the top. Then open it when needed. "open" command causes retrieving fresh data from db.

then your print syntax is like that:

print 'my custI is %1!, custName is %2!', @custId, @custName
Burçin Yazıcı
sql server says that in the print line there is incorrect syntax near ',' this one in fact: , @custId
i just wanted to make my code one click to run, i was searching for some thing like that one the web in creating a view. there was a function called object_id which was used to see if the object of the view has created or not we used to see if it is not null then delete the view and create a new one. i could not do the same thing about the cursor. would you please offer a way?? thank you very much
PRINT doesn't work that way in TSQL: it doesn't support variable interpolation directly.
Pondlife
sorry I missed. Its working on sybase...
Burçin Yazıcı
+2  A: 

It's not really clear what you're trying to do. Why not just run your query to get all the results in one go, and then use your client code to format and present the data?

select CustomerID, [Customer Name], [Product Name], Maximum from favorit

You can use the ROW_NUMBER() function to add row numbers if you want, or number them in the client application when you present the output.

Next, PRINT is a statement, not a function. It can print only one string, so you need to build and then PRINT the string. Your second example might actually work, but you will have to handle any different data types:

print cast(@custID as varchar(10)) + 'this is the customer name' + cast(@custName as varchar(100)) + 'etc'  

Finally, formatting and printing data directly from TSQL is usually a sign that you're doing something wrong. The usual approach is to return a result set to a client application and let the client application format it, print it, convert it to PDF or whatever. Your application could be a small Perl script, a complete reporting solution, or anything in between, but PRINT is not really a good mechanism for presenting data.

Pondlife