Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > Sybase

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 06:03 PM
dufffman@gmail.com
 
Posts: n/a
Default loop through arrays in a stored procedure

Hi,

I want to create a stored procedure that will populate an empty table.

I am a java programmer and a novice in databases. Would someone mind
helping me translate my psuedocode into a stored procedure?

create procedure MappingTable{

@array1 = {1,2,3,4}
@array2 = {10,20,30,40}

@i = 0

while ( array1.hasMoreElements(){
if ( array2[i] < array1[i]
-- perform a particualr insert statement
else if ( array2[i] > array1[i]
-- perform another insert statement
else
for (int j = 0; j < array2.length; j++){
if ( array2[i] == array1[i]
-- insert
else
-- an insert statement
}
}


}


Hope this makes some sense. Help is much appreciated.

Thank You.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 06:03 PM
D_Peglow
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

dufffman@gmail.com schrieb:
> Hi,
>
> I want to create a stored procedure that will populate an empty table.
>
> I am a java programmer and a novice in databases. Would someone mind
> helping me translate my psuedocode into a stored procedure?
>
> create procedure MappingTable{
>
> @array1 = {1,2,3,4}
> @array2 = {10,20,30,40}
>
> @i = 0
>
> while ( array1.hasMoreElements(){
> if ( array2[i] < array1[i]
> -- perform a particualr insert statement
> else if ( array2[i] > array1[i]
> -- perform another insert statement
> else
> for (int j = 0; j < array2.length; j++){
> if ( array2[i] == array1[i]
> -- insert
> else
> -- an insert statement
> }
> }
>
>
> }




create table #tmpArrays (Array1 int, Array2 int)

insert #tmpArrays values( 1, 10)
insert #tmpArrays values( 2, 20)
insert #tmpArrays values( 3, 30)
insert #tmpArrays values( 4, 40)

declare @array1, @array2 int

declare row cursor for
select Array1, Array2
from #tmpArrays

open row

fetch row into @array1, @array2

while (@@sqlstatus = 0)
begin

if (@array2 < @array1)
begin
--- your insert statement
end
else
--- bla bla bla
--- and so on
--- next if statement and so on


--- end while, get next values from array

fetch row into @array1, @array2
end

close row
deallocate cursor row


Obviously, I did not test it. But it could give you an idea how to do it.

Cheers,

Dietmar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 06:03 PM
Pablo Sanchez
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

dufffman@gmail.com wrote in
news:1126813555.086515.215820@g44g2000cwa.googlegr oups.com:

> I am a java programmer and a novice in databases. Would someone
> mind helping me translate my psuedocode into a stored procedure?
>


Hi,

If you'd like your application to scale, you should know processing
data row at a time is at least 10x slower than in a set.

Think of arrays as tables. Load all the data into a table, then use
set based logic to process the data.

Cheers,
--
Pablo Sanchez - Blueoak Database Engineering, Inc
http://www.blueoakdb.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 06:03 PM
--CELKO--
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

>> I am a java programmer and a novice in databases. <<

SQL is a declarative language. We do not like to write procedural code
at all! We also do not have arrays; the ONLY data structure is a table.
In short, your question pseudo-code makes no sense.

What we would look more like this:

CREATE TABLE Array1
(subscript INTEGER NOT NULL PRIMARY KEY,
value INTEGER NOT NULL);

CREATE TABLE Array2
(subscript INTEGER NOT NULL PRIMARY KEY,
value INTEGER NOT NULL);

BEGIN
INSERT INTO Foobar2 (..)
SELECT ..
FROM FakeArray1 AS A1, FakeArray2 AS A2
WHERE A1.subscript = A2.subscript
AND A1.value > A1.value;

INSERT INTO Foobar3 (..)
SELECT ..
FROM FakeArray1 AS A1, FakeArray2 AS A2
WHERE A1.subscript = A2.subscript
AND A1.value = A1.value;
END;

See? No loops, no if', just pure set-oriented operations. It will take
you about a year of full-time SQL programming to un-learn procedural
coding.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 06:03 PM
D_Peglow
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

--CELKO-- schrieb:

>>>I am a java programmer and a novice in databases. <<

>
>
> SQL is a declarative language. We do not like to write procedural code
> at all! We also do not have arrays; the ONLY data structure is a table.
> In short, your question pseudo-code makes no sense.


Makes no sense. For what is PL/SQL (Oracle) and Transact SQL in Sybase?
Because procedural code is not necessary? Sure, your code is 100% pure
non procedural.


> What we would look more like this:


> See? No loops, no if', just pure set-oriented operations. It will take
> you about a year of full-time SQL programming to un-learn procedural
> coding.


Sorry, nonsense.

Cheers,

Dietmar.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-08-2008, 06:03 PM
--CELKO--
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

>> For what is PL/SQL (Oracle) and Transact SQL in Sybase? Because procedural code is not necessary? <<

Those are all proprietary tools from early days of SQL, when systems
were built on top of existing file systems. And the Standard SQL/PSM
also exists. You use them for kludges when you do not know how to
write SQL or have to write code that gets to the engine internals.

>> Sorry, nonsense. <<


No, I based that estimate on 20 years experience teaching SQL to
procedural programmers in college and industry. Oh, I also have written
a few books on the language Will you tell me why you think that
estimate is wrong?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-08-2008, 06:03 PM
D_Peglow
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

--CELKO-- schrieb:
>>>For what is PL/SQL (Oracle) and Transact SQL in Sybase? Because procedural code is not necessary? <<

>
>
> Those are all proprietary tools from early days of SQL, when systems
> were built on top of existing file systems. And the Standard SQL/PSM
> also exists. You use them for kludges when you do not know how to
> write SQL or have to write code that gets to the engine internals.


>>>Sorry, nonsense. <<

>
> No, I based that estimate on 20 years experience teaching SQL to
> procedural programmers in college and industry. Oh, I also have written
> a few books on the language Will you tell me why you think that
> estimate is wrong?


We are often given internal training to staff or at the client site on
our database model which is from the financial sector and the database
has more than 1000 tables. If we would teach it from the ivory tower
point of view in a academic way, it would take us 1 year to teach
design of some pure SQL procedures that allow us to calculate some Loans
including linear interpolation in a yield curve or applying some more
complex math calc (including built-in functions like exp() and so on.
Not to use some cursors or while statements simply would make the code
too complex or we would need java, C, C++ or VBA which would make it
again more complex. I think mixing procedural code (avoiding it where
possible) with good SQL data retrieval statements is the best approach
to get good productivity and performance.

Dietmar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-08-2008, 06:03 PM
Mike Epprecht \(SQL MVP\)
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

Hi

Which raises the point of why are you doing those calculations in the
database?

A middle tier would do it a lot more efficiently. T-SQL or PL-SQL are not
the fastest in the world to do number crunching.

A middle tier can scale out, a database hits a finite hardware limit.

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

"D_Peglow" <news_peg02@snafu.de> wrote in message
news:3p4rbrF8jhh9U1@uni-berlin.de...
> --CELKO-- schrieb:
>>>>For what is PL/SQL (Oracle) and Transact SQL in Sybase? Because
>>>>procedural code is not necessary? <<

>>
>>
>> Those are all proprietary tools from early days of SQL, when systems
>> were built on top of existing file systems. And the Standard SQL/PSM
>> also exists. You use them for kludges when you do not know how to
>> write SQL or have to write code that gets to the engine internals.

>
>>>>Sorry, nonsense. <<

>>
>> No, I based that estimate on 20 years experience teaching SQL to
>> procedural programmers in college and industry. Oh, I also have written
>> a few books on the language Will you tell me why you think that
>> estimate is wrong?

>
> We are often given internal training to staff or at the client site on our
> database model which is from the financial sector and the database has
> more than 1000 tables. If we would teach it from the ivory tower point of
> view in a academic way, it would take us 1 year to teach design of some
> pure SQL procedures that allow us to calculate some Loans including linear
> interpolation in a yield curve or applying some more complex math calc
> (including built-in functions like exp() and so on. Not to use some
> cursors or while statements simply would make the code too complex or we
> would need java, C, C++ or VBA which would make it again more complex. I
> think mixing procedural code (avoiding it where possible) with good SQL
> data retrieval statements is the best approach to get good productivity
> and performance.
>
> Dietmar



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-08-2008, 06:03 PM
--CELKO--
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

>> .. it would take us 1 year to teach design of some pure SQL procedures that allow us to calculate some Loans including linear interpolation in a yield curve or applying some more complex math calc (including built-in functions like exp() and so on. <<

Or you could find a solution for NPV and IRR using auxiliary function
tables, a quick look at the details are in Chapter 22 of SQL FOR
SMARTIES. That is how people did it before calculators and cheap
computers. It also has the advantage of operating in parallel instead
of on a row-by-row basis, and you can avoid linear interpolation in
favor of a second delta interpolation so the results are more accurate.


The auxiliary table is loaded once and the programming is a simple
join. I doubt that it would take a year to do this

LISP and APL programmers understand SQL almost immediately. It is
procedural programmers that fall back to cursors and procedures when
they are firt learning. I have written five cursors in my career and I
know that if we had the CASE expression 15-20 years ago, I could have
avoided at least three of them.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-08-2008, 06:03 PM
D_Peglow
 
Posts: n/a
Default Re: loop through arrays in a stored procedure

Mike Epprecht (SQL MVP) schrieb:
> Hi
>
> Which raises the point of why are you doing those calculations in the
> database?


Complex calculations like monte carlo, black scholes and stuff like that
are done outside the database (in C++) of course. Some simpler reports
including NPV calcs can be done inside the database in some cases where
C++/java development would be shooting at a small bird using nuclear
weapons.

D.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:05 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619