| Author |
Topic |
|
nguyenl
Posting Yak Master
128 Posts |
Posted - 2008-07-31 : 14:43:27
|
| Hi,What is a "Foreach" and how do I use it?Thanks, |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2008-07-31 : 14:46:41
|
it foreaches the collection _______________________________________________Causing trouble since 1980Blog: http://weblogs.sqlteam.com/mladenpSpeed up SSMS development: www.ssmstoolspack.com <- version 1.0 out! |
 |
|
|
nguyenl
Posting Yak Master
128 Posts |
Posted - 2008-07-31 : 16:01:37
|
| Do you know what is its equivelant? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
nguyenl
Posting Yak Master
128 Posts |
Posted - 2008-07-31 : 16:48:01
|
| Are you able to give me an idea on how to create a cursor for this? FOREACH SELECT 'RES', recid1, year_built, main_fn_area, uppr_fn_area, unfin_area, bsmt_area, bldg_style, quality, condition, num_stories, phy_pct_good INTO aBldg, aRecid, aYrBlt, wrkA1, wrkA2, wrkA3, wrkA4, tmpCode, aQuality, aCondition,aNumSt, aPctGood FROM residence, parcel WHERE parcel.property_id = propid and parcel.parcel_year = taxyear-- AND parcel.parcel_year > 0 and parcel.parcel_year < 3000 AND parcel.link_id = residence.link_id IF wrkA1 IS NULL THEN LET wrkA1 = 0; END IF; IF wrkA2 IS NULL THEN LET wrkA2 = 0; END IF; IF wrkA3 IS NULL THEN LET wrkA3 = 0; END IF; IF wrkA4 IS NULL THEN LET wrkA4 = 0; END IF; LET aSqFt = wrkA1 + wrkA2 + wrkA3 + wrkA4; IF aSqFt = 0 THEN LET aSqFt = NULL; END IF; SELECT description INTO aCode FROMvalue_list WHERE table_name = 'RESIDENCE' AND column_name = 'BLDG_STYLE' AND code = TRIM(tmpCode); IF aCode is NULL THEN LET aCode = tmpCode; END IF; RETURN aBldg, aRecid, aYrBlt, aSqFt, aCode, aQuality, aCondition, aNumSt, aPctGood with RESUME; END FOREACH;I am lost.Thanks, |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
nguyenl
Posting Yak Master
128 Posts |
Posted - 2008-07-31 : 16:51:54
|
| The whole query looks like this. I was hopng to get a basic idea on how to translate the first "foreach" so I can apply it to the others.CREATE PROCEDURE "informix".getbldg_x(propid INTEGER, taxyear INTEGER) RETURNING CHAR(03), INTEGER, CHAR(04), INTEGER, CHAR(18), CHAR(02), CHAR(02), INTEGER, INTEGER; DEFINE aBldg CHAR(03); DEFINE aRecid INTEGER; DEFINE aYrBlt CHAR(04); DEFINE aSqft INTEGER; DEFINE wrkA1 INTEGER; DEFINE wrkA2 INTEGER; DEFINE wrkA3 INTEGER; DEFINE wrkA4 INTEGER; DEFINE wrkA5 INTEGER; DEFINE aCode CHAR(18); DEFINE aQuality CHAR(02); DEFINE aCondition CHAR(02); DEFINE aNumSt INTEGER; DEFINE aPctGood INTEGER; DEFINE tmpCode CHAR(04);-- DEFINE acurrent_tax_year INTEGER;-- added for specific year-- select current_tax_year into acurrent_tax_year from system_options; FOREACH SELECT 'RES', recid1, year_built, main_fn_area, uppr_fn_area, unfin_area, bsmt_area, bldg_style, quality, condition, num_stories, phy_pct_good INTO aBldg, aRecid, aYrBlt, wrkA1, wrkA2, wrkA3, wrkA4, tmpCode, aQuality, aCondition,aNumSt, aPctGood FROM residence, parcel WHERE parcel.property_id = propid and parcel.parcel_year = taxyear-- AND parcel.parcel_year > 0 and parcel.parcel_year < 3000 AND parcel.link_id = residence.link_id IF wrkA1 IS NULL THEN LET wrkA1 = 0; END IF; IF wrkA2 IS NULL THEN LET wrkA2 = 0; END IF; IF wrkA3 IS NULL THEN LET wrkA3 = 0; END IF; IF wrkA4 IS NULL THEN LET wrkA4 = 0; END IF; LET aSqFt = wrkA1 + wrkA2 + wrkA3 + wrkA4; IF aSqFt = 0 THEN LET aSqFt = NULL; END IF; SELECT description INTO aCode FROMvalue_list WHERE table_name = 'RESIDENCE' AND column_name = 'BLDG_STYLE' AND code = TRIM(tmpCode); IF aCode is NULL THEN LET aCode = tmpCode; END IF; RETURN aBldg, aRecid, aYrBlt, aSqFt, aCode, aQuality, aCondition, aNumSt, aPctGood with RESUME; END FOREACH; FOREACH SELECT 'COM', recid1, year_built, grnd_fl_area, bldg_type, quality, condition, num_stories, phy_pct_good INTO aBldg, aRecid, aYrBlt, wrkA1, tmpCode, aQuality, aCondition, aNumSt, aPctGood FROM comm_section, parcel WHERE parcel.property_id = propid-- AND parcel.parcel_year > 0 and parcel.parcel_year < 3000 AND parcel.parcel_year = taxyear AND parcel.link_id = comm_section.link_id IF wrkA1 IS NULL THEN LET wrkA1 = 0; END IF; LET aSqFt = wrkA1; IF aSqFt = 0 THEN LET aSqFt = NULL; END IF; SELECT description INTO aCode FROM value_list WHERE table_name = 'COMM_SECTION' AND column_name = 'BLDG_TYPE' AND code = TRIM(tmpCode); IF aCode is NULL THEN LET aCode = tmpCode; END IF; RETURN aBldg, aRecid, aYrBlt, aSqFt, aCode, aQuality, aCondition,aNumSt, aPctGood with RESUME; END FOREACH; FOREACH SELECT 'DET', recid1, year_built, main_fn_area, uppr_fn_area, structure, quality, condition, num_stories, phy_pct_good INTO aBldg, aRecid, aYrBlt, wrkA1, wrkA2, tmpCode, aQuality, aCondition, aNumSt, aPctGood FROM det_structure, parcel WHERE parcel.property_id = propid AND parcel.parcel_year = taxyear--- AND parcel.parcel_year > 0 and parcel.parcel_year < 3000 AND parcel.link_id = det_structure.link_idIF wrkA1 IS NULL THEN LET wrkA1 = 0; END IF; IF wrkA2 IS NULL THEN LET wrkA2 = 0; END IF; LET aSqFt = wrkA1 + wrkA2; IF aSqFt = 0 THEN LET aSqFt = NULL; END IF; SELECT description INTO aCode FROM value_list WHERE table_name = 'DET_STRUCTURE' AND column_name = 'STRUCTURE' AND code = TRIM(tmpCode); IF aCode IS NULL THEN LET aCode = tmpCode; END IF; RETURN aBldg, aRecid, aYrBlt, aSqFt, aCode, aQuality, aCondition, aNumSt, aPctGood with RESUME; END FOREACH; FOREACH SELECT 'MOB', recid1, year_built, tot_liv_area, make, quality, condition, 0,phy_pct_good INTO aBldg, aRecid, aYrBlt, wrkA1, aCode, aQuality, aCondition, aNumSt, aPctGood FROM mobile_home, parcel WHERE parcel.property_id = propid AND parcel.parcel_year = taxyear--- AND parcel.parcel_year > 0 and parcel.parcel_year < 3000 AND parcel.link_id = mobile_home.link_id IF wrkA1 IS NULL THEN LET wrkA1 = 0; END IF; LET aSqFt = wrkA1; IF aSqFt = 0 THEN LET aSqFt = NULL; END IF; LET aNumSt = NULL; RETURN aBldg, aRecid, aYrBlt, aSqFt, aCode, aQuality, aCondition, aNumSt, aPctGood with RESUME; END FOREACH;END PROCEDURE; |
 |
|
|
nguyenl
Posting Yak Master
128 Posts |
Posted - 2008-07-31 : 18:39:56
|
| I basically rewrote the "first" foreach statement as:CREATE PROCEDURE [dbo].[getbldg_x](@propid INTEGER, @taxyear INTEGER)ASDECLARE @aBldg VARCHAR(3)DECLARE @aRecid INTDECLARE @aYrBlt VARCHAR(4)DECLARE @aSqft INTDECLARE @wrkA1 INTDECLARE @wrkA2 INTDECLARE @wrkA3 INTDECLARE @wrkA4 INTDECLARE @wrkA5 INTDECLARE @aCode VARCHAR(18)DECLARE @tmpCode VARCHAR(4)DECLARE @aQuality VARCHAR(2)DECLARE @aCondition VARCHAR(2)DECLARE @aNumSt INTDECLARE @aPctGood INT DECLARE residence_CURSOR CURSOR FORSELECT'RES',recid1,year_built,main_fn_area,uppr_fn_area,unfin_area,bsmt_area,bldg_style,quality,condition,num_stories,phy_pct_goodFROMresidence,parcelWHERE parcel.property_id = @propidand parcel.parcel_year = @taxyear-- AND parcel.parcel_year > 0 and parcel.parcel_year < 3000AND parcel.link_id = residence.link_idOPEN residence_CURSORFETCH next FROM residence_CURSOR INTO@aBldg,@aRecid,@aYrBlt,@wrkA1,@wrkA2,@wrkA3,@wrkA4,@tmpCode,@aQuality,@aCondition,@aNumSt,@aPctGoodWHILE @@fetch_status = 0--Processing Statements BeginsBEGINIF @wrkA1 IS NULL SET @wrkA1 = '0'ENDBEGIN IF @wrkA2 IS NULL SET @wrkA2 = 0ENDBEGINIF @wrkA3 IS NULLSET @wrkA3 = 0;ENDBEGINIF @wrkA4 IS NULLSET @wrkA4 = 0ENDSET @aSqFt = @wrkA1 + @wrkA2 + @wrkA3 + @wrkA4 BEGINIF @aSqFt = 0SET @aSqFt = NULLEND--Processing statements EndBEGINFETCH next FROM residence_CURSOR INTO@aBldg,@aRecid,@aYrBlt,@wrkA1,@wrkA2,@wrkA3,@wrkA4,@tmpCode,@aQuality,@aCondition,@aNumSt,@aPctGoodENDCLOSE residence_CURSORDEALLOCATE residence_CURSORI hope this is correct. When I create this procedure in SQL I get no errors. I notice that there is also a "RETURN" statement at the end on my original query. Do you know if I have to do anything with that?Thanks |
 |
|
|
|
|
|