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 |
|
wollk
Starting Member
1 Post |
Posted - 2003-03-07 : 11:49:19
|
| Hi,this procedure have to update a table in SQL Server 2000!When i run this procedure in the Query Analyzer it works well! But when i go to sql server to view the result the table remains empty!! What is wrong or have i forgotten anything?Thank you!CREATE PROCEDURE proc_VersandPauschTabelle_2 ASDECLARE@test_cursor CURSOR,@var1 float,@var2 int,@var3 int,@Zähler int,@row_name CHAR(7),@sql CHAR(100);BEGINSET @test_cursor = CURSOR SCROLL DYNAMIC FOR SELECT [Nebenkosten pro Klasse], Endeklasse, BeginnlotterieFROM LotterieData.dbo.DebitorOPEN @test_cursor ;/*der erste Datenabruf vom Cursor*/FETCH NEXT FROM @test_cursor INTO @var1,@var2,@var3;WHILE (@@FETCH_STATUS = 0)BEGINSET @Zähler=1;WHILE ( @zähler<=@var2)BEGINSET @row_name='Klasse'+cast(@Zähler as varchar(3));SET @sql='Insert into LotterieData.dbo.tblVersandPausch ( '+@row_name+', Lotterie) Values ('+cast(@var1 as varchar)+', '+cast(@var3 as varchar)+')';PRINT @sql;EXEC(@sql);--update LotterieData.dbo.tblVersandPauschSET @zähler=@zähler +1;/*CURSOR weitersetzen*/ENDFETCH NEXT FROM @test_cursor INTO @var1,@var2,@var3;ENDCLOSE @test_cursor;DEALLOCATE @test_cursor;select * from LotterieData.dbo.tblVersandPauschENDGO |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-03-07 : 12:49:12
|
| What do you get when you run the followingSELECT MAX(Endeklasse)FROM LotterieData.dbo.DebitorCan you provide ddl for LotterieData.dbo.tblVersandPausch and LotterieData.dbo.Debitor. Also some sample data and expected results in tblVersandPaush. Someone might be able help you rewrite the cursor. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-03-07 : 12:58:22
|
| I didn't know you could define a cursor like that. I tired:Declar @x CursorSET @X = CURSOR SCROLL DYNAMIC FOR SELECT * From sysobjectand it didn't work.Also, Why Use cursors at all?And why the Dynamic SQL?Brett8-) |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-03-07 : 13:08:36
|
| FROM BOL:Sets the specified local variable, previously created with the DECLARE @local_variable statement, to the given value.SyntaxSET { { @local_variable = expression } | { @cursor_variable = { @cursor_variable | cursor_name | { CURSOR [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] [ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ] [ TYPE_WARNING ] FOR select_statement [ FOR { READ ONLY | UPDATE [ OF column_name [ ,...n ] ] } ] } } } } ArgumentsD. Define a cursor with SETThis example uses the SET statement to define a cursor.DECLARE @CursorVar CURSORSET @CursorVar = CURSOR SCROLL DYNAMICFORSELECT LastName, FirstNameFROM Northwind.dbo.EmployeesWHERE LastName like 'B%'OPEN @CursorVarFETCH NEXT FROM @CursorVarWHILE @@FETCH_STATUS = 0BEGIN FETCH NEXT FROM @CursorVarENDCLOSE @CursorVarDEALLOCATE @CursorVar |
 |
|
|
|
|
|
|
|