SQL - Cursor statement
Discussion
AAAAAAAAAAaargh. Right, that's that out of the way.
I have a table (called table_name, for sake of argument!), 3 cols (col_1 nvarchar(135), col_2 int, col_3 nvarchar(135))
col_1 will contain a lot of rubbish for many rows, but some of them will begin "customer".
I think using a cursor will let me go through one row at a time, and where left(col_1, 8) = 'customer', I wish to set col_3 = col_1.
Buggered if I know how to make it work though...any SQL gurus on here care to enlighten me to the dark arts of the cursor?
Thanks
I have a table (called table_name, for sake of argument!), 3 cols (col_1 nvarchar(135), col_2 int, col_3 nvarchar(135))
col_1 will contain a lot of rubbish for many rows, but some of them will begin "customer".
I think using a cursor will let me go through one row at a time, and where left(col_1, 8) = 'customer', I wish to set col_3 = col_1.
Buggered if I know how to make it work though...any SQL gurus on here care to enlighten me to the dark arts of the cursor?
Thanks
Sorry - didn't explain fully.
say there's 100 lines, and every 10th (from line 10) is
"customer XXXXXXXXXXXXXXX"
but the XXXXXXXXXXXXXXXXX is different.
So, from line 1 to line 10 I don't care what's in col_3 (leave it null), from line 10-19 I want col_3 = "customer XXXXXXXXXXXXXXX", for line 20-29 I want it to be customer XXXXXXXXXXXXXX which is different to line 10-19 etc etc.
Problem is that the number of lines between new customers isn't the same.
Does that makes sense?
What I (think I) want to do is : -
check each line,
does col_1 begin "customer"
yes - set @cust_name = col_1
then set col_3 = @cust_name
no - ser col_3 = @cust_name (without changing @cust_name)
say there's 100 lines, and every 10th (from line 10) is
"customer XXXXXXXXXXXXXXX"
but the XXXXXXXXXXXXXXXXX is different.
So, from line 1 to line 10 I don't care what's in col_3 (leave it null), from line 10-19 I want col_3 = "customer XXXXXXXXXXXXXXX", for line 20-29 I want it to be customer XXXXXXXXXXXXXX which is different to line 10-19 etc etc.
Problem is that the number of lines between new customers isn't the same.
Does that makes sense?
What I (think I) want to do is : -
check each line,
does col_1 begin "customer"
yes - set @cust_name = col_1
then set col_3 = @cust_name
no - ser col_3 = @cust_name (without changing @cust_name)
I guess when you say lines you mean rows...?
If it's in Oracle, why not something like :
Personally I think you need to order it, or is that where the col_2 value comes in?
update table_name
set col_3 = col_1
where (left(col_1,8) = 'customer') order by col_2 where rownum > 10;
This should only change rows 11+ based on the available rows being ordered on col_2 in ascending value..
or is that still not what you want to do? or not achievable in the DB you're using?
If it's in Oracle, why not something like :
Personally I think you need to order it, or is that where the col_2 value comes in?
update table_name
set col_3 = col_1
where (left(col_1,8) = 'customer') order by col_2 where rownum > 10;
This should only change rows 11+ based on the available rows being ordered on col_2 in ascending value..
or is that still not what you want to do? or not achievable in the DB you're using?
Assuming col_2 is some kind of unique identifier (or else you have one in some other column)
declare @id int
declare @col1 nvarchar(135)
declare @prevcol1 nvarchar(135)
set @prevcol1=''
declare cur cursor for
select col1, col2 from table_name
open cur
fetch next from cur into @col1, @id
while @@fetch_status = 0
begin
if left(@col1,8) = 'customer' set @prevcol1=@col1
if @prevcol1 <> '' update table_name set col3=@prevcol1 where col2 = @id
fetch next from cur into @col1, @col2
end
close cur
deallocate cur
Thanks guys - I've managed to bodge something together which I *think* works...I need to check by making some identical lines of data as it doesn't currently have an ID field, and part of me thinks this might be an issue later...the code is : -
(Promise I'll never ask about cursors ever again, nor will I ask a SQL question without telling you my create table statements, and giving an example of the data
)
crap SQl code said:
declare cust_cur cursor for
select rawdata, totalvolume, customername
from tmp
open Cust_Cur
fetch next from Cust_Cur
into @c_rawdata, @c_totalvolume, @c_customer
WHILE @@FETCH_STATUS = 0
BEGIN
if left (@c_rawdata, 8) like 'customer'
begin
set @cust_name = @c_rawdata
update tmp
set customername = @cust_name
where current of cust_cur
fetch next from Cust_Cur
into @c_rawdata, @c_totalvolume, @c_customer
end
else
update tmp
set customername = @cust_name
where current of cust_cur
fetch next from Cust_Cur
into @c_rawdata, @c_totalvolume, @c_customer
END
close Cust_Cur
deallocate Cust_Cur
which whilst it isn't pretty, does appear to do the job!select rawdata, totalvolume, customername
from tmp
open Cust_Cur
fetch next from Cust_Cur
into @c_rawdata, @c_totalvolume, @c_customer
WHILE @@FETCH_STATUS = 0
BEGIN
if left (@c_rawdata, 8) like 'customer'
begin
set @cust_name = @c_rawdata
update tmp
set customername = @cust_name
where current of cust_cur
fetch next from Cust_Cur
into @c_rawdata, @c_totalvolume, @c_customer
end
else
update tmp
set customername = @cust_name
where current of cust_cur
fetch next from Cust_Cur
into @c_rawdata, @c_totalvolume, @c_customer
END
close Cust_Cur
deallocate Cust_Cur
(Promise I'll never ask about cursors ever again, nor will I ask a SQL question without telling you my create table statements, and giving an example of the data
)Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


