July 16, 2009 at 12:17 pm
Hi
I have a cursor that will loop data from rows, and I need to add values into a variable.
How can I do it?
For example, in the cursor I have values of a variable @vNOVO_ID_CURSO with:
1
2
3
4
5
and I want to put this values in the variable @acumul_error, so I can returned like this example: @acumul_error = '12345'
This is the following code:
if @vID_CURSO_SEL <> @vNOVO_ID_CURSO
begin
set @acumul_error = @acumul_error + @vNOVO_ID_CURSO
end
The code is not working in the proper way. Can someone give me a feedback?
Best Regads,
André.
July 16, 2009 at 12:23 pm
The code is not working in the proper way.
What is it doing, are you getting an error? It would help if you could post your code, sample data, and DDL. By the way, if you've written a cursor just to do this, you don't need one as you can do this in one SQL statement.
July 16, 2009 at 12:24 pm
it should work with some minor tweaks:
after you've done the DECLARE @acumul_error varchar(100) or whatever it is, you need to initialize it as an empty string:
SET @acumul_error =''
in the cursor, i'm betting one of the values(@vNOVO_ID_CURSO) is an integer, so you might need to convert it:
set @acumul_error = @acumul_error + CONVERT(VARCHAR(30),@vNOVO_ID_CURSO)
then your code in your cursor should work fine...otherwise ti was concatenating NULL + '1'
Lowell
July 16, 2009 at 12:35 pm
Lowell (7/16/2009)
it should work with some minor tweaks:after you've done the DECLARE @acumul_error varchar(100) or whatever it is, you need to initialize it as an empty string:
SET @acumul_error =''
in the cursor, i'm betting one of the values(@vNOVO_ID_CURSO) is an integer, so you might need to convert it:
set @acumul_error = @acumul_error + CONVERT(VARCHAR(30),@vNOVO_ID_CURSO)
then your code in your cursor should work fine...otherwise ti was concatenating NULL + '1'
Thanks. It works!
All I had to do was to initialize the variable outside the cursor.
Best Regards,
André.
July 19, 2009 at 8:44 pm
lopes80andre (7/16/2009)
Lowell (7/16/2009)
it should work with some minor tweaks:after you've done the DECLARE @acumul_error varchar(100) or whatever it is, you need to initialize it as an empty string:
SET @acumul_error =''
in the cursor, i'm betting one of the values(@vNOVO_ID_CURSO) is an integer, so you might need to convert it:
set @acumul_error = @acumul_error + CONVERT(VARCHAR(30),@vNOVO_ID_CURSO)
then your code in your cursor should work fine...otherwise ti was concatenating NULL + '1'
Thanks. It works!
All I had to do was to initialize the variable outside the cursor.
Best Regards,
André.
If I may... no matter what you're doing in this cursor, it is slow. It looks like you may be trying to do a running total of accumulated errors. My recommendation would be to post the table you're trying to do the running total with, some sample data in a readily consumable format, and a description of what the business process is that you're trying to accomplish.
Please see the first link in my signature line below for how to easily do all of that.
And, to wet your whistle, if it is a running total problem, we can show you how to do it on a million rows in somewhere between 3 and 7 seconds... all without a cursor.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply