Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jeremy96
Starting Member
1 Post |
Posted - 2011-03-29 : 03:59:29
|
Dear All,is it true that cursor cannot process null values?i've this syntaxCREATE TABLE tb_coba( @nama varchar(20), @umur int, @keterangan varchar(50), @kode int)CREATE TABLE tb_master_almt(@toko varchar(20),@alamat varchar(20)@kode int)goinsert into tb_coba values ('jery',22,'biasa',1)insert into tb_coba values ('jhon',23,'extra',2)insert into tb_coba values ('joe',24,'biasa',3)insert into tb_master_almt values ('adira','bandung',1)insert into tb_master_almt values ('supra','jakarta',2)godeclare @kode numeric, @nama varchar(200), @umur numeric, @val1 numeric, @val2 varchar(200), @val3 varchar(200), @message varchar(200)declare cursor_tb_coba1 cursor for select kode, nama, umur from tb_cobaOPEN cursor_tb_coba1FETCH NEXT FROM cursor_tb_coba1 INTO @kode, @nama, @umurWHILE @@FETCH_STATUS = 0 BEGIN PRINT ' ' SELECT @message = '----- Nama : ' + @nama PRINT @message DECLARE cursor_master CURSOR FOR SELECT kode, toko, alamat FROM tb_master_almt WHERE kode = @kode -- Variable value from the outer cursor OPEN cursor_master FETCH NEXT FROM cursor_master INTO @val1, @val2, @val3 IF @@FETCH_STATUS <> 0 PRINT ' <>' WHILE @@FETCH_STATUS = 0 BEGIN SELECT @message = 'Alamat : ' + @val2 + ' ' + @val3 PRINT @message FETCH NEXT FROM cursor_master INTO @val1, @val2, @val3 END CLOSE cursor_master DEALLOCATE cursor_master FETCH NEXT FROM cursor_tb_coba1 INTO @kode, @nama, @umurEND CLOSE cursor_tb_coba1DEALLOCATE cursor_tb_coba1when i try to run it, it works, but all row where @umur = null, it doesn't come out. please correct my syntax if i am wrong.thanks |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-03-29 : 06:52:28
|
FormattingCREATE TABLE tb_coba( @nama varchar(20), @umur int, @keterangan varchar(50), @kode int)CREATE TABLE tb_master_almt(@toko varchar(20),@alamat varchar(20)@kode int)goinsert into tb_coba values ('jery',22,'biasa',1)insert into tb_coba values ('jhon',23,'extra',2)insert into tb_coba values ('joe',24,'biasa',3)insert into tb_master_almt values ('adira','bandung',1)insert into tb_master_almt values ('supra','jakarta',2)godeclare @kode numeric, @nama varchar(200), @umur numeric, @val1 numeric, @val2 varchar(200), @val3 varchar(200), @message varchar(200)declare cursor_tb_coba1 cursor for select kode, nama, umur from tb_cobaOPEN cursor_tb_coba1FETCH NEXT FROM cursor_tb_coba1 INTO @kode, @nama, @umurWHILE @@FETCH_STATUS = 0 BEGIN PRINT ' ' SELECT @message = '----- Nama : ' + @nama PRINT @message DECLARE cursor_master CURSOR FOR SELECT kode, toko, alamat FROM tb_master_almt WHERE kode = @kode -- Variable value from the outer cursor OPEN cursor_master FETCH NEXT FROM cursor_master INTO @val1, @val2, @val3 IF @@FETCH_STATUS <> 0 PRINT ' <>' WHILE @@FETCH_STATUS = 0 BEGIN SELECT @message = 'Alamat : ' + @val2 + ' ' + @val3 PRINT @message FETCH NEXT FROM cursor_master INTO @val1, @val2, @val3 END CLOSE cursor_master DEALLOCATE cursor_master FETCH NEXT FROM cursor_tb_coba1 INTO @kode, @nama, @umurEND CLOSE cursor_tb_coba1DEALLOCATE cursor_tb_coba1 Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-03-29 : 06:55:19
|
I don't know what you are trying to do but the actual problem you are expressing is because you can't compare nulls that way.Consider:DECLARE @foo INTDECLARE @bar INTSET @foo = NULLSET @bar = NULL-- Neither of these will workIF @foo = @bar PRINT 'yo!'IF @foo != @bar PRINT 'no!'-- This willIF @foo IS NULL AND @bar IS NULL PRINT 'NULL' You cannot compare NULLS. All you can do is test for NULL. Read this:http://en.wikipedia.org/wiki/Three-state_logicCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|