## The SQL to the Orbiting Ball

12 09 2010

September 12, 2010

Several years ago I developed a somewhat simple program in 16 bit Borland C (last compiled in 1994, so it pre-dates graphical web pages) that was optimized for 386 and 486 computers which sported VGA graphics but was also able to work with EGA graphics cards.  The simple program, after creating 18 images on the fly that simulated a rotating basketball, generated X and Y coordinates using a specially crafted formula that was capable of producing X and Y cordinates in a circular pattern.  With slightly different inputs the same formula produced a slightly out of round circular pattern that eventually might have generated straight line patterns.  With a slight adjustment to the inputs the same formula produced W patterns within a bounded box.  Keyboard input allowed the user to specify adjusted inputs as the program executed.  The C program looked like this:

```#include <graphics.h>               // for graphics functions
#include <conio.h>                  // for kbhit()
#include <math.h>
#include <dos.h>
#define LEFT      100               // boundary of rectangle
#define TOP       100
#define pi        3.14159276
#define L_ARROW   75
#define R_ARROW   77
#define U_ARROW   72
#define D_ARROW   80
#define ESC       27

void main(void)
{
int driver, mode;                // for initgraph()
float x, y, dx, dy, i, sped, temp, x1, x2, y1, y2, sta1, sta2, ena1, ena2, rad;    // ball coordinates
int imgnum, tim, del;
char key;
unsigned char ballbuff[10][5000];    // buffer for ball image

driver = DETECT;                 // auto detect
// set graphics mode
initgraph(&driver, &mode, "c:\\bc4\\bgi");

x = LEFT + RADIUS;          // designate center where ball is created
for (i =0; i <180; i = i + 18) // create basketball rotation images
{
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
floodfill(x, y, RED);
setcolor(BLACK);
rad = i / (2 * pi);
x1 = x + 30 * cos(rad);
x2 = x - 30 * cos(rad);
y1 = y + 30 * sin(rad);
y2 = y - 30 * sin(rad);
sta1 = (180 + i -62);
ena1 = (180 + i + 42);
sta2 = (i -62);
ena2 = (i + 42);
if ((i/36) != int(i/36))    // must be included to swap arcs
{
temp = sta1;
sta1 = sta2;
sta2 = temp;
temp = ena1;
ena1 = ena2;
ena2 = temp;
}
// pickup image
// clear screen
setcolor(WHITE);
rectangle(-1,-1, 640, 480);
setfillstyle(SOLID_FILL, BLACK);
floodfill(100,100, WHITE);
}

imgnum = 10;                         // load first position + 1
tim = 0;                             // set delay to zero
dx = .1;                             // set constant in x equation
dy = .1;
sped = 1;
del = 1;
while ( key != ESC )
{
if (kbhit())
{
key = getch();
if (key == 61) del++;            // = key pressed
if (key == 43) del++;            // + key pressed
if (key == 45) del--;            // - key pressed
if (key == 47) sped = sped +.1;  // / key pressed
if (key == 92) sped = sped -.1;  // \ key pressed
if (key == 0)                    // place image on screen
switch(getch())
{
case L_ARROW:
dx = dx -.01;
break;
case R_ARROW:
dx = dx + .01;
break;
case U_ARROW:
dy = dy + .01;
break;
case D_ARROW:
dy = dy -.01;
break;
case ESC:
key = 27;
break;
}
}
tim = tim + sped;
x = (sin(dx * tim)*100) + 300;
y = (cos(dy * tim)* 100) + 300;
imgnum--;                   // cycle through images
if (imgnum == -1) imgnum = 9;

// move ball across screen
// to make ball move rapidly increase +
// set height on screen
delay(del);                 // make delay smaller for slow computers
}
getch();

closegraph();
}```

If you still have a 16 bit Borland C compiler (my copy is in a huge box on the bottom self of my bookcase), feel free to compile the above program to see it in action.  The original compiled C program, last compiled in 1994, may be downloaded here: OrbitBall2.zip (save as OrbitBall.zip and extract the files – download might not work).  The original program is confirmed to work on 32 bit Windows 95 through Windows XP (in DOS full screen mode), but definitely will not work on 64 bit Windows, even in a virtual machine (it also failed to run on a 32 bit Windows 7 netbook).

You are probably thinking, “Neat, but what does that have to do with Oracle?”.  It might be interesting to produce a modernized version of the above program.  We could use a simple SQL statement like the following to generate 3,600 X and Y coordinates, much like the X and Y coordinates that were generated by the above C program (note that the COS function is typically used to derive X values (using mathematical cosine) and the SIN function is typically used to derive Y values – the functions were swapped so that the X and Y cordinates start at the bottom-center of the screen).  The SQL follows:

```SELECT
ROUND((SIN(DX * (SPEED * COUNTER)) * WIDTH/2) + WIDTH/2) X,
ROUND((COS(DY * (SPEED * COUNTER)) * HEIGHT/2) + HEIGHT/2) Y
FROM
(SELECT
0.1 DX,
0.1 DY,
1 SPEED,
1 DELAY,
300 WIDTH,
300 HEIGHT,
LEVEL COUNTER
FROM
DUAL
CONNECT BY
LEVEL<=3600);```

Now we have a slight problem, how do we present the X and Y coordinate points produced by the above SQL statement?  We need some sort of object to track the X and Y coordinate pairs.  Drawn basketballs might work, but instead I will use these pictures (created with Microsoft Power Point 2010):

To display the round oak pictures, we will put together a VBS script to build a web page on the fly, cycling through the above eight pictures (every two X,Y coordinate pairs cause the displayed picture to change).  Much like the original program, we will allow the user to control the input parameters as the program runs.  Each time the parameters are adjusted, 3,600 new X,Y coordinate points are retrieved from the database into an array – this allows the VBS script to continue at the next X,Y coordinate pair, rather than starting at the beginning every time the parameters are adjusted.

```Option Explicit

Dim dbDatabase
Dim snpData
Dim comData
Dim varData
Dim objIE

Dim strDatabase             'SID name from tnsnames.ora

startup

Sub startup()
Dim strSQL
Dim strHTML
Dim objOrbitBall
Dim objOrbitBallPic
Dim objCommand
Dim objSettings
Dim i
Dim intQuit

'Fire up Internet Explorer
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Left = 0
objIE.Top = 0
objIE.Width = 930
objIE.Height = 820
objIE.StatusBar = True
objIE.Toolbar = False
objIE.Document.Title = "The SQL to the Orbiting Ball"
objIE.Visible = True

'The data entry area
strHTML = "<div style=""position: absolute;width: 180px; height: 200px;top: 10px;left: 710px;"">" & vbCrLf
strHTML = strHTML & "<input type=text id=txtCommand value="""" size=""1""><br>" & vbCrLf
strHTML = strHTML & "<font size=1><b>+&nbsp;&nbsp;&nbsp;Increase Delay (Not Used)<br>" & vbCrLf
strHTML = strHTML & "-&nbsp;&nbsp;&nbsp;Decrease Delay (Not Used)<br>" & vbCrLf
strHTML = strHTML & "/&nbsp;&nbsp;&nbsp;Increase Rotation Speed<br>" & vbCrLf
strHTML = strHTML & "\&nbsp;&nbsp;&nbsp;Decrease Rotation Speed<br>" & vbCrLf
strHTML = strHTML & "D&nbsp;&nbsp;&nbsp;Increase Rotation Speed X Axis<br>" & vbCrLf
strHTML = strHTML & "A&nbsp;&nbsp;&nbsp;Decrease Rotation Speed X Axis<br>" & vbCrLf
strHTML = strHTML & "W&nbsp;&nbsp;&nbsp;Increase Rotation Speed Y Axis<br>" & vbCrLf
strHTML = strHTML & "S&nbsp;&nbsp;&nbsp;Decrease Rotation Speed Y Axis<br>" & vbCrLf
strHTML = strHTML & "L&nbsp;&nbsp;&nbsp;Increase Width X Axis<br>" & vbCrLf
strHTML = strHTML & "J&nbsp;&nbsp;&nbsp;Decrease Width X Axis<br>" & vbCrLf
strHTML = strHTML & "I&nbsp;&nbsp;&nbsp;Increase Height Y Axis<br>" & vbCrLf
strHTML = strHTML & "K&nbsp;&nbsp;&nbsp;Decrease Height Y Axis<br>" & vbCrLf
strHTML = strHTML & "(space)&nbsp;&nbsp;&nbsp;Restart at 0<br>" & vbCrLf
strHTML = strHTML & "X&nbsp;&nbsp;&nbsp;Exit</b></font>" & vbCrLf
strHTML = strHTML & "</div>" & vbCrLf

'The current orbit information
strHTML = strHTML & "<div id=""Settings"" style=""position: absolute;width: 180px; height: 100px;top: 600px;left: 710px;""> </div>"
strHTML = strHTML & "<IMG ID=""picOrbitBall"" style=""position: absolute;"" src=""http://hoopercharles.files.wordpress.com/2010/09/sqlorbitingball0.png"">" & vbCrLf
objIE.Document.Body.InnerHTML = strHTML

'The sleep here is only necessary if the database connections happen very quickly
'Wscript.Sleep 500

'Database configuration
strDatabase = "MyDB"

On Error Resume Next

dbDatabase.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=" & strDatabase & ";User ID=" & strUsername & ";Password=" & strPassword & ";"
dbDatabase.open

'Should verify that the connection attempt was successful, but I will leave that for someone else to code

strSQL = "SELECT" & vbCrLf
strSQL = strSQL & "  ROUND((SIN(DX * (SPEED * COUNTER)) * WIDTH/2) + WIDTH/2) X," & vbCrLf
strSQL = strSQL & "  ROUND((COS(DY * (SPEED * COUNTER)) * HEIGHT/2) + HEIGHT/2) Y" & vbCrLf
strSQL = strSQL & "FROM" & vbCrLf
strSQL = strSQL & "  (SELECT" & vbCrLf
strSQL = strSQL & "    ? DX," & vbCrLf
strSQL = strSQL & "    ? DY," & vbCrLf
strSQL = strSQL & "    ? SPEED," & vbCrLf
strSQL = strSQL & "    ? DELAY," & vbCrLf
strSQL = strSQL & "    ? WIDTH," & vbCrLf
strSQL = strSQL & "    ? HEIGHT," & vbCrLf
strSQL = strSQL & "    LEVEL COUNTER" & vbCrLf
strSQL = strSQL & "  FROM" & vbCrLf
strSQL = strSQL & "    DUAL" & vbCrLf
strSQL = strSQL & "  CONNECT BY" & vbCrLf
strSQL = strSQL & "    LEVEL<=3600)"

With comData
'Set up the command properties
.CommandText = strSQL
.CommandTimeout = 30

.ActiveConnection = dbDatabase

End With

comData("dx") = 0.1
comData("dy") = 0.1
comData("speed") = 1
comData("delay") = 1
comData("width") = 600
comData("height") = 600

Set snpData = comData.Execute

'Retrieve up to 10,000 data points from Oracle
varData = snpData.GetRows(10000)
snpData.Close
Set snpData = Nothing

Set objOrbitBall = objIE.Document.getElementById("picOrbitBall").Style
Set objOrbitBallPic = objIE.Document.getElementById("picOrbitBall")
Set objCommand = objIE.Document.All.txtCommand
Set objSettings = objIE.Document.getElementById("Settings")

'Write out the current settings for the orbit
objSettings.InnerText = "DX: " & comData("dx") & Chr(10) & "DY: " & comData("dy") & Chr(10) & _
"Speed: " & comData("speed") &Chr(10) & _
"Width: " & comData("width") & Chr(10) & "Height: " & comData("height")

intQuit = False

Do While intQuit = False
For i = 0 To UBound(varData, 2)
objOrbitBall.Left = CInt(varData(0, i))
objOrbitBall.Top = CInt(varData(1, i))
objOrbitBallPic.Src = "http://hoopercharles.files.wordpress.com/2010/09/sqlorbitingball" & cStr(i/2 Mod 8 ) & ".png"

Wscript.Sleep 50
Select Case Left(objCommand.Value, 1)
Case "=", "+"
comData("delay") = comData("delay") + 1
Case "-"
comData("delay") = comData("delay") - 1
Case "/"
comData("speed") = comData("speed") + 0.1
Case "\"
comData("speed") = comData("speed") - 0.1
Case "W", "w"
comData("dy") = comData("dy") + 0.0005
Case "S", "s"
comData("dy") = comData("dy") - 0.0005
Case "D", "d"
comData("dx") = comData("dx") + 0.0005
Case "A", "a"
comData("dx") = comData("dx") - 0.0005
Case "I", "i"
comData("height") = comData("height") + 5
Case "K", "k"
comData("height") = comData("height") - 5
Case "L", "l"
comData("width") = comData("width") + 5
Case "J", "j"
comData("width") = comData("width") - 5
Case "X", "x"
intQuit = True
Exit For
Case " "
'Reset the loop from the beginning
objCommand.Value = ""
Exit For
End Select

If objCommand.Value <> "" Then
objCommand.Value = ""

Set snpData = comData.Execute

'Retrieve up to 10,000 data points from Oracle
varData = snpData.GetRows(10000)

snpData.Close
Set snpData = Nothing

'Write out the current settings for the orbit
objSettings.InnerText = "DX: " & comData("dx") & Chr(10) & "DY: " & comData("dy") & Chr(10) & _
"Speed: " & comData("speed") &Chr(10) & _
"Width: " & comData("width") & Chr(10) & "Height: " & comData("height")
End If
Next
Loop

objIE.quit
dbDatabase.Close
Set dbDatabase = Nothing
Set objIE = Nothing
End Sub```

A Circular Orbit:

————-

An Orbit that Changes from a Circular Orbit to a Straight Line:

————-

An Orbit where the Ball Bounces Between the Top and Bottom of the Window:

—————————

As written, the script assumes a minimum of 930 x 820 resolution (1080p resolution or greater should work without any problems).  Adjust the script as necessary for lower resolution screens.  The program written in C certainly is shorter than the moderized version of the program, and had a bit more wow factor prior to the widespread use of Windows and other graphical user interfaces.

## Graphical Work Center Utilization – Creating the Demo Data and Active Server Page

1 09 2010

September 1, 2010

Today’s blog article provides a graphical view of production work areas on a factory floor, providing feedback to indicate when the production area is in use.  The blog article includes a classic ASP web page which uses VBScript to write the web page on the fly to the browser on the client computer.  The web page written to the client browser automatically refreshes itself every 30 seconds, automatically advancing the view time by 15 minutes, displaying the production work areas that were in use during the new view time (the time displayed at the top of the page).

The ASP web page is the simple part of today’s blog article (although enabling classic ASP support may be a little challenging), while creating the demo data is actually the challenging portion of the article.  First, we need a table that will define the production work areas and the locations of those areas on the factory floor:

```CREATE TABLE RESOURCE_LOCATION_DEMO (
RESOURCE_ID VARCHAR2(15),
DESCRIPTION VARCHAR2(30),
LOCATION_LEFT NUMBER(12,4),
LOCATION_TOP NUMBER(12,4),
LOCATION_WIDTH NUMBER(12,4),
LOCATION_HEIGHT NUMBER(12,4),
PRIMARY KEY (RESOURCE_ID));```

To keep things interesting, I do not want to just place the first production work area next to the second, the second next to the third, etc.  Instead, I want to randomly locate the production work areas on the factory floor, making certain that no two work areas overlap.  We can accomplish this by creating a list of numbers and ordering the numbers in a random sequence, like this:

```SELECT
ROWNUM RN
FROM
DUAL
CONNECT BY
LEVEL<=200
ORDER BY
DBMS_RANDOM.VALUE;

RN
----
191
165
122
12
48
27
104
...
198
168
150

200 rows selected.```

Now, to locate the production work areas, imagine that we permitted 10 work areas horizontally (along the X axis) across the factory floor.  We can use the above number sequence along with the MOD function to determine the horizontal location of the work areas, and the FLOOR function to determine the vertical location of the work areas (note that each time we run this SQL statement will we receive different results):

```SELECT
'MA'||TO_CHAR(ROWNUM) RESOURCE_ID,
'Shop Floor Machine '||TO_CHAR(ROWNUM) DESCRIPTION,
MOD(RN,10) BOX_LEFT,
FLOOR(RN/10) BOX_TOP
FROM
(SELECT
ROWNUM RN
FROM
DUAL
CONNECT BY
LEVEL<=200
ORDER BY
DBMS_RANDOM.VALUE)
WHERE
ROWNUM<=50;

RESOURCE_ID DESCRIPTION                 BOX_LEFT    BOX_TOP
----------- ------------------------- ---------- ----------
MA1         Shop Floor Machine 1               4         14
MA2         Shop Floor Machine 2               9          6
MA3         Shop Floor Machine 3               5          2
MA4         Shop Floor Machine 4               5          9
MA5         Shop Floor Machine 5               7         18
MA6         Shop Floor Machine 6               9          4
MA7         Shop Floor Machine 7               0          8
MA8         Shop Floor Machine 8               6          6
MA9         Shop Floor Machine 9               5          5
MA10        Shop Floor Machine 10              7         15
...
MA49        Shop Floor Machine 49              2         11
MA50        Shop Floor Machine 50              8          7```

It would be too boring to assume that each of the production work areas is exactly the same width and height, so we will add a little more randomization.  Additionally, I want each production area to be up to 1.5 units wide and up to 1.0 units tall, both offset 0.25 units from the top-left (we are dealing with screen coordinates here, where positive Y is the same as mathematical -Y).  While there are up to 200 locations on the factory floor for work areas, we will only define 50 work areas (controlled by the ROWNUM<=50 predicate at the end of the SQL statement):

```SELECT
'MA'||TO_CHAR(ROWNUM) RESOURCE_ID,
'Shop Floor Machine '||TO_CHAR(ROWNUM) DESCRIPTION,
(MOD(RN,10))*1.5 + 0.25 LOCATION_LEFT,
(FLOOR(RN/10))*1.0 + 0.25 LOCATION_TOP,
ROUND(1.5*DBMS_RANDOM.VALUE,4) LOCATION_WIDTH,
ROUND(1.0*DBMS_RANDOM.VALUE,4) LOCATION_HEIGHT
FROM
(SELECT
ROWNUM RN
FROM
DUAL
CONNECT BY
LEVEL<=200
ORDER BY
DBMS_RANDOM.VALUE)
WHERE
ROWNUM<=50;

RESOURCE_ID DESCRIPTION               LOCATION_LEFT LOCATION_TOP LOCATION_WIDTH LOCATION_HEIGHT
----------- ------------------------- ------------- ------------ -------------- ---------------
MA1         Shop Floor Machine 1               3.25        18.25         1.2386           .7948
MA2         Shop Floor Machine 2               4.75        11.25          .6078           .9578
MA3         Shop Floor Machine 3               1.75        12.25          .5318            .457
MA4         Shop Floor Machine 4               3.25        13.25         1.2908           .9813
MA5         Shop Floor Machine 5                .25        16.25          .3245           .4644
MA6         Shop Floor Machine 6              12.25        15.25           .239           .3822
MA7         Shop Floor Machine 7               1.75        18.25          .0159           .8868
MA8         Shop Floor Machine 8               1.75        16.25          .3948           .8511
MA9         Shop Floor Machine 9              12.25         6.25          .4856           .3356
MA10        Shop Floor Machine 10             13.75        11.25         1.2619           .6124
...
MA49        Shop Floor Machine 49              7.75        16.25          .6664           .6938
MA50        Shop Floor Machine 50              9.25        15.25         1.3449           .6606```

Now that we have tested the results, let’s insert a new set of similar random values into the RESOURCE_LOCATION_DEMO table, and display some of the inserted rows:

```INSERT INTO
RESOURCE_LOCATION_DEMO
SELECT
'MA'||TO_CHAR(ROWNUM) RESOURCE_ID,
'Shop Floor Machine '||TO_CHAR(ROWNUM) DESCRIPTION,
(MOD(RN,10))*1.5 + 0.25 LOCATION_LEFT,
(FLOOR(RN/10))*1.0 + 0.25 LOCATION_TOP,
ROUND(1.5*DBMS_RANDOM.VALUE,4) LOCATION_WIDTH,
ROUND(1.0*DBMS_RANDOM.VALUE,4) LOCATION_HEIGHT
FROM
(SELECT
ROWNUM RN
FROM
DUAL
CONNECT BY
LEVEL<=200
ORDER BY
DBMS_RANDOM.VALUE)
WHERE
ROWNUM<=50;

COMMIT;

SELECT
*
FROM
RESOURCE_LOCATION_DEMO;

RESOURCE_ID DESCRIPTION               LOCATION_LEFT LOCATION_TOP LOCATION_WIDTH LOCATION_HEIGHT
----------- ------------------------- ------------- ------------ -------------- ---------------
MA1         Shop Floor Machine 1              10.75        13.25           .104           .2165
MA2         Shop Floor Machine 2               7.75        18.25         1.2291            .478
MA3         Shop Floor Machine 3               9.25        16.25          .3431           .4364
MA4         Shop Floor Machine 4               1.75        15.25          .3665           .7278
MA5         Shop Floor Machine 5               4.75        15.25          .6842           .4507
MA6         Shop Floor Machine 6               4.75        18.25          .1384           .6434
MA7         Shop Floor Machine 7               4.75         7.25          .7448           .2178
MA8         Shop Floor Machine 8               7.75          .25          .3756            .499
MA9         Shop Floor Machine 9               1.75        18.25         1.0155           .0769
MA10        Shop Floor Machine 10              7.75        13.25         1.1892           .7518
...
MA49        Shop Floor Machine 49              6.25         3.25           .278           .6513
MA50        Shop Floor Machine 50               .25        15.25          .5216           .9607```

To translate the above storage units (maybe in inch scale) into screen units we will multiply the storage units by 96 (96 dots per inch) and then multiply by the zoom percent (75% = 0.75).

```SELECT
RESOURCE_ID,
ROUND(LOCATION_LEFT*96 *0.75) LOCATION_LEFT,
ROUND(LOCATION_TOP*96 *0.75) LOCATION_TOP,
ROUND(LOCATION_WIDTH*96 *0.75) LOCATION_WIDTH,
ROUND(LOCATION_HEIGHT*96 *0.75) LOCATION_HEIGHT
FROM
RESOURCE_LOCATION_DEMO;

RESOURCE_ID LOCATION_LEFT LOCATION_TOP LOCATION_WIDTH LOCATION_HEIGHT
----------- ------------- ------------ -------------- ---------------
MA1                   774          954              7              16
MA2                   558         1314             88              34
MA3                   666         1170             25              31
MA4                   126         1098             26              52
MA5                   342         1098             49              32
MA6                   342         1314             10              46
MA7                   342          522             54              16
MA8                   558           18             27              36
MA9                   126         1314             73               6
MA10                  558          954             86              54
...```

Next, we need a table to maintain the time periods in which each of the production work areas was in use, and by whom the work areas were used:

```CREATE TABLE LABOR_TICKET_DEMO (
TRANSACTION_ID NUMBER,
RESOURCE_ID VARCHAR2(15),
EMPLOYEE_ID VARCHAR2(15),
CLOCK_IN DATE,
CLOCK_OUT DATE,
PRIMARY KEY (TRANSACTION_ID));```

Let’s see if we are able to generate some random data for the table:

```ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI';

SELECT
ROWNUM TRANSACTION_ID,
'MA'||TO_CHAR(ROUND(DBMS_RANDOM.VALUE(1,50))) RESOURCE_ID,
'EMP'||TO_CHAR(ROUND(DBMS_RANDOM.VALUE(1,300))) EMPLOYEE_ID,
TRUNC(SYSDATE)+DBMS_RANDOM.VALUE CLOCK_IN
FROM
DUAL
CONNECT BY
LEVEL<=10;

TRANSACTION_ID RESOURCE_ID EMPLOYEE_ID CLOCK_IN
-------------- ----------- ----------- -----------------
1 MA29        EMP50       01-SEP-2010 01:56
2 MA35        EMP181      01-SEP-2010 10:06
3 MA13        EMP172      01-SEP-2010 17:40
4 MA21        EMP182      01-SEP-2010 09:00
5 MA14        EMP80       01-SEP-2010 09:53
6 MA4         EMP80       01-SEP-2010 19:04
7 MA7         EMP110      01-SEP-2010 14:34
8 MA45        EMP19       01-SEP-2010 22:05
9 MA10        EMP207      01-SEP-2010 21:51
10 MA46        EMP127      01-SEP-2010 16:49```

That worked, but note that we did not generate a CLOCK_OUT time – we want to make certain that the CLOCK_OUT time is after the CLOCK_IN time, but we simply cannot do that with the above SQL statement as written.  We slide the above into an inline view and then set the CLOCK_OUT time to be up to 12 hours after the CLOCK_IN time (DBMS_RANDOM.VALUE by default returns a value between 0 and 1, so if we divide that value by 2, we can add up to 1/2 of a day to the CLOCK_IN date and time):

```INSERT INTO
LABOR_TICKET_DEMO
SELECT
TRANSACTION_ID,
RESOURCE_ID,
EMPLOYEE_ID,
CLOCK_IN,
CLOCK_IN + (DBMS_RANDOM.VALUE/2) CLOCK_OUT
FROM
(SELECT
ROWNUM TRANSACTION_ID,
'MA'||TO_CHAR(ROUND(DBMS_RANDOM.VALUE(1,50))) RESOURCE_ID,
'EMP'||TO_CHAR(ROUND(DBMS_RANDOM.VALUE(1,300))) EMPLOYEE_ID,
TRUNC(SYSDATE)+DBMS_RANDOM.VALUE CLOCK_IN
FROM
DUAL
CONNECT BY
LEVEL<=1000);

COMMIT;```

Let’s take a look at what made it into the table (your results will be different):

```SELECT
*
FROM
LABOR_TICKET_DEMO
ORDER BY
TRANSACTION_ID;

TRANSACTION_ID RESOURCE_ID     EMPLOYEE_ID     CLOCK_IN          CLOCK_OUT
-------------- --------------- --------------- ----------------- -----------------
1 MA34            EMP49           01-SEP-2010 20:32 02-SEP-2010 08:18
2 MA47            EMP230          01-SEP-2010 20:08 02-SEP-2010 03:06
3 MA20            EMP257          01-SEP-2010 02:17 01-SEP-2010 05:44
4 MA21            EMP129          01-SEP-2010 09:37 01-SEP-2010 15:41
5 MA18            EMP214          01-SEP-2010 18:57 01-SEP-2010 20:57
6 MA46            EMP173          01-SEP-2010 05:51 01-SEP-2010 15:32
7 MA34            EMP224          01-SEP-2010 20:23 02-SEP-2010 08:17
8 MA31            EMP8            01-SEP-2010 02:56 01-SEP-2010 14:02
9 MA37            EMP178          01-SEP-2010 09:28 01-SEP-2010 16:03
10 MA8             EMP31           01-SEP-2010 20:17 02-SEP-2010 05:51
...
999 MA43            EMP138          01-SEP-2010 05:07 01-SEP-2010 05:23
1000 MA2             EMP235          01-SEP-2010 05:29 01-SEP-2010 13:28```

We now have 1000 transactions scattered across the 50 work areas (RESOURCE_ID column).  What we next want to determine is which of the work areas was in use at a specific time of the day.  Because we eventually will want only one row per unique RESOURCE_ID value, we will use the ROW_NUMBER analytic function to number each of the rows within each unique RESOURCE_ID value:

```SELECT
RESOURCE_ID,
EMPLOYEE_ID,
TRUNC(SYSDATE) + (1.25)/24 CHECK_TIME,
CLOCK_IN,
CLOCK_OUT,
ROW_NUMBER() OVER (PARTITION BY RESOURCE_ID ORDER BY CLOCK_IN) RN
FROM
LABOR_TICKET_DEMO
WHERE
CLOCK_IN<=TRUNC(SYSDATE) + (1.25)/24
AND CLOCK_OUT>TRUNC(SYSDATE) + (1.25)/24
ORDER BY
RESOURCE_ID,
CLOCK_IN;

RESOURCE_ID  EMPLOYEE_ID  CHECK_TIME        CLOCK_IN          CLOCK_OUT         RN
------------ ------------ ----------------- ----------------- ----------------- --
MA10         EMP192       01-SEP-2010 01:15 01-SEP-2010 00:21 01-SEP-2010 05:44  1
MA10         EMP233       01-SEP-2010 01:15 01-SEP-2010 00:23 01-SEP-2010 02:42  2
MA16         EMP114       01-SEP-2010 01:15 01-SEP-2010 00:21 01-SEP-2010 04:48  1
MA18         EMP261       01-SEP-2010 01:15 01-SEP-2010 00:18 01-SEP-2010 07:02  1
MA18         EMP133       01-SEP-2010 01:15 01-SEP-2010 00:32 01-SEP-2010 04:35  2
MA2          EMP62        01-SEP-2010 01:15 01-SEP-2010 01:14 01-SEP-2010 12:03  1
MA21         EMP235       01-SEP-2010 01:15 01-SEP-2010 00:05 01-SEP-2010 10:42  1
MA22         EMP4         01-SEP-2010 01:15 01-SEP-2010 00:01 01-SEP-2010 06:27  1
MA22         EMP300       01-SEP-2010 01:15 01-SEP-2010 01:12 01-SEP-2010 11:50  2
MA23         EMP135       01-SEP-2010 01:15 01-SEP-2010 00:35 01-SEP-2010 05:19  1
MA25         EMP35        01-SEP-2010 01:15 01-SEP-2010 00:20 01-SEP-2010 06:58  1
MA28         EMP298       01-SEP-2010 01:15 01-SEP-2010 00:52 01-SEP-2010 06:27  1
MA30         EMP72        01-SEP-2010 01:15 01-SEP-2010 00:56 01-SEP-2010 07:28  1
MA32         EMP84        01-SEP-2010 01:15 01-SEP-2010 01:00 01-SEP-2010 05:25  1
MA34         EMP299       01-SEP-2010 01:15 01-SEP-2010 00:31 01-SEP-2010 12:04  1
MA38         EMP268       01-SEP-2010 01:15 01-SEP-2010 00:31 01-SEP-2010 04:15  1
MA38         EMP278       01-SEP-2010 01:15 01-SEP-2010 00:32 01-SEP-2010 04:50  2
MA38         EMP176       01-SEP-2010 01:15 01-SEP-2010 01:01 01-SEP-2010 04:01  3
MA4          EMP257       01-SEP-2010 01:15 01-SEP-2010 00:10 01-SEP-2010 10:45  1
MA40         EMP231       01-SEP-2010 01:15 01-SEP-2010 00:58 01-SEP-2010 11:01  1
MA43         EMP65        01-SEP-2010 01:15 01-SEP-2010 00:54 01-SEP-2010 09:29  1
MA44         EMP18        01-SEP-2010 01:15 01-SEP-2010 00:07 01-SEP-2010 03:30  1
MA46         EMP36        01-SEP-2010 01:15 01-SEP-2010 00:40 01-SEP-2010 04:57  1
MA48         EMP61        01-SEP-2010 01:15 01-SEP-2010 00:27 01-SEP-2010 10:20  1
MA48         EMP169       01-SEP-2010 01:15 01-SEP-2010 00:44 01-SEP-2010 01:27  2
MA5          EMP147       01-SEP-2010 01:15 01-SEP-2010 00:02 01-SEP-2010 04:48  1
MA6          EMP132       01-SEP-2010 01:15 01-SEP-2010 00:34 01-SEP-2010 09:42  1

27 rows selected.```

In some cases we have up to three employees working in a work area at 1:15AM (the time of day is indicated by the 1.25 value in the SQL statement).  Now, lets eliminate the duplicate rows, leaving just the rows where the calculated RN column is equal to 1.  We will join the above SQL statement in an inline view to the RESOURCE_LOCATION_DEMO table and convert the production work area coordinates to screen coordinates, in this case 96 pixels per unit (inches) at a 75% zoom percent (we made this same screen coordinate conversion in a previous SQL statement above):

```SELECT
RL.RESOURCE_ID,
RL.DESCRIPTION,
ROUND(RL.LOCATION_LEFT*96 *0.75) LOCATION_LEFT,
ROUND(RL.LOCATION_TOP*96 *0.75) LOCATION_TOP,
ROUND(RL.LOCATION_WIDTH*96 *0.75) LOCATION_WIDTH,
ROUND(RL.LOCATION_HEIGHT*96 *0.75) LOCATION_HEIGHT,
LT.EMPLOYEE_ID,
LT.CLOCK_IN,
LT.CLOCK_OUT
FROM
RESOURCE_LOCATION_DEMO RL,
(SELECT
RESOURCE_ID,
EMPLOYEE_ID,
CLOCK_IN,
CLOCK_OUT,
ROW_NUMBER() OVER (PARTITION BY RESOURCE_ID ORDER BY CLOCK_IN) RN
FROM
LABOR_TICKET_DEMO
WHERE
CLOCK_IN<=TRUNC(SYSDATE) + (1.25)/24
AND CLOCK_OUT>TRUNC(SYSDATE) + (1.25)/24) LT
WHERE
RL.RESOURCE_ID=LT.RESOURCE_ID(+)
AND NVL(LT.RN,1)=1
ORDER BY
RL.RESOURCE_ID;

RESOURCE_ID  DESCRIPTION                    LOCATION_LEFT LOCATION_TOP LOCATION_WIDTH LOCATION_HEIGHT EMPLOYEE_ID  CLOCK_IN          CLOCK_OUT
------------ ------------------------------ ------------- ------------ -------------- --------------- ------------ ----------------- -----------------
MA1          Shop Floor Machine 1                     774          954              7              16
MA10         Shop Floor Machine 10                    558          954             86              54 EMP192       01-SEP-2010 00:21 01-SEP-2010 05:44
MA11         Shop Floor Machine 11                    882         1098             29               1
MA12         Shop Floor Machine 12                    234          378             51              51
MA13         Shop Floor Machine 13                    882         1314             83              62
MA14         Shop Floor Machine 14                    558          378             38              61
MA15         Shop Floor Machine 15                    774          522             63              64
MA16         Shop Floor Machine 16                    126          666            103              55 EMP114       01-SEP-2010 00:21 01-SEP-2010 04:48
MA17         Shop Floor Machine 17                    558          234             94              30
MA18         Shop Floor Machine 18                    342          450             85              21 EMP261       01-SEP-2010 00:18 01-SEP-2010 07:02
MA19         Shop Floor Machine 19                    342          666             94              20
MA2          Shop Floor Machine 2                     558         1314             88              34 EMP62        01-SEP-2010 01:14 01-SEP-2010 12:03
MA20         Shop Floor Machine 20                    666          162             33              33
MA21         Shop Floor Machine 21                    774          306             66              22 EMP235       01-SEP-2010 00:05 01-SEP-2010 10:42
MA22         Shop Floor Machine 22                    990          378             78              71 EMP4         01-SEP-2010 00:01 01-SEP-2010 06:27
MA23         Shop Floor Machine 23                    666          666             50              37 EMP135       01-SEP-2010 00:35 01-SEP-2010 05:19
MA24         Shop Floor Machine 24                    990          810            107              45
...```

From the above output, we know the screen coordinates of each production work area (RESOURCE_ID) and the first employee to use the production work area (and the employee was still using it) at 1:15AM.

For the next portion of this blog article, the portion that requires an ASP enabled web server, we need a couple of pictures (these were created using Microsoft Power Point):

Representing a production work center that is in use:

Representing a production work center that is idle:

The factory floor – the background area:

——

Now we need the classic ASP page code – note that the code syntax is very similar to that of the previous VBScript examples.  The script uses Response.Write to write information to the client computer’s web browser, and an embedded Java script to call the post event of the embedded HTML form to update the display as of time every 30 seconds (yes, I should have used bind variables in the SQL statement, but that would have required an extra 120 seconds to code and would have left you with nothing to improve):

```<html>

<meta http-equiv="refresh" content="600">
<title>Graphical Work Center Utilization Animated</title>

<body>
<%
Dim strSQL

Dim sglOriginLeft
Dim sglOriginTop

Dim sglZoom
Dim strZoom

Dim i
Dim intWidth
Dim intHeight
Dim strOffset
Dim sglOffset
Dim varDateTime

Dim snpData
Dim dbDatabase

'Database configuration
strDatabase = "MyDB"

On Error Resume Next

dbDatabase.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=" & strDatabase & ";User ID=" & strUsername & ";Password=" & strPassword & ";"
dbDatabase.Open

'Should verify that the connection attempt was successful, but I will leave that for someone else to code

'Retrieve the last value for the time offset and the selected zoom percent
strOffset = Request.Form("txtOffset")
strZoom = Request.Form("cboZoom")

'Convert back to a number
sglOffset = CSng(strOffset)
sglZoom = CSng(strZoom) / 100

'Advance to the next 0.25 hour
if (sglOffset = 0) or (sglOffset = 24) then
sglOffset = 0.25
else
sglOffset = sglOffset + 0.25
end if

'Set the zoom percent, if not already set
If sglZoom = 0 Then
sglZoom = 0.5 '50% zoom
End If

varDateTime = DateAdd("n", sglOffset*60, Date) 'Create a printable version of the view date and time

Response.Write "<form name=" & chr(34) & "reportform" & chr(34) & " method=" & chr(34) & "POST" & chr(34) & " action=" & chr(34) & "GraphicalWorkCenterUtilization.asp" & chr(34) & ">" & vbcrlf
Response.Write varDateTime & "  " & vbCrLf
Response.Write "&nbsp;&nbsp;&nbsp;Zoom Percent <select size=""1"" id=""cboZoom"" name=""cboZoom"" style=""width:50"">" & vbCrLf
For i = 10 to 200 Step 10
If sglZoom = i / 100 Then
Response.Write "<option selected value=""" & cStr(i) & """>" & cStr(i) & "</option>"
Else
Response.Write "<option value=""" & cStr(i) & """>" & cStr(i) & "</option>"
End If
Next
Response.Write "</select><br>"
Response.Write "  <input type=" & chr(34) & "submit" & chr(34) & " value=" & chr(34) & "Update View" & chr(34) & " name=" & chr(34) & "cmdViewReport" & chr(34) & ">" & vbcrlf
Response.Write "  <input type=" & chr(34) & "hidden" & chr(34) & " name=" & chr(34) & "txtOffset" & chr(34) & " size=" & chr(34) & "5" & chr(34) & " value=" & chr(34) & cStr(sglOffset) & chr(34) & ">" & vbcrlf
Response.Write "</form>" & vbcrlf

'The background image
intWidth = Round(16 * 96 * sglZoom)
intHeight = Round(20 * 96 * sglZoom)
Response.Write "<img src=" & chr(34) & "http://hoopercharles.files.wordpress.com/2010/09/graphicalworkcenterutilizationbackground.jpg" & chr(34) & " width=" & chr(34) & cstr(intWidth) & chr(34) & " height=" & chr(34) & cstr(intheight) & chr(34) & " style=" & chr(34) & "position:absolute;top:50px;left:0px;z-index:-1" & chr(34) & "/>" & vbcrlf

'The SQL statement developed earlier
strSQL = "SELECT" & VBCrLf
strSQL = strSQL & "  RL.RESOURCE_ID," & VBCrLf
strSQL = strSQL & "  RL.DESCRIPTION," & VBCrLf
strSQL = strSQL & "  ROUND(RL.LOCATION_LEFT*96 *" & cStr(sglZoom) & ") LOCATION_LEFT," & VBCrLf
strSQL = strSQL & "  ROUND(RL.LOCATION_TOP*96 *" & cStr(sglZoom) & ") LOCATION_TOP," & VBCrLf
strSQL = strSQL & "  ROUND(RL.LOCATION_WIDTH*96 *" & cStr(sglZoom) & ") LOCATION_WIDTH," & VBCrLf
strSQL = strSQL & "  ROUND(RL.LOCATION_HEIGHT*96 *" & cStr(sglZoom) & ") LOCATION_HEIGHT," & VBCrLf
strSQL = strSQL & "  LT.EMPLOYEE_ID," & VBCrLf
strSQL = strSQL & "  LT.CLOCK_IN," & VBCrLf
strSQL = strSQL & "  LT.CLOCK_OUT" & VBCrLf
strSQL = strSQL & "FROM" & VBCrLf
strSQL = strSQL & "  RESOURCE_LOCATION_DEMO RL," & VBCrLf
strSQL = strSQL & "  (SELECT" & VBCrLf
strSQL = strSQL & "    RESOURCE_ID," & VBCrLf
strSQL = strSQL & "    EMPLOYEE_ID," & VBCrLf
strSQL = strSQL & "    CLOCK_IN," & VBCrLf
strSQL = strSQL & "    CLOCK_OUT," & VBCrLf
strSQL = strSQL & "    ROW_NUMBER() OVER (PARTITION BY RESOURCE_ID ORDER BY CLOCK_IN) RN" & VBCrLf
strSQL = strSQL & "  FROM" & VBCrLf
strSQL = strSQL & "    LABOR_TICKET_DEMO" & VBCrLf
strSQL = strSQL & "  WHERE" & VBCrLf
strSQL = strSQL & "    CLOCK_IN<=TRUNC(SYSDATE) + (" & cStr(sglOffset) & ")/24" & VBCrLf
strSQL = strSQL & "    AND CLOCK_OUT>TRUNC(SYSDATE) + (" & cStr(sglOffset) & ")/24) LT" & VBCrLf
strSQL = strSQL & "WHERE" & VBCrLf
strSQL = strSQL & "  RL.RESOURCE_ID=LT.RESOURCE_ID(+)" & VBCrLf
strSQL = strSQL & "  AND NVL(LT.RN,1)=1" & VBCrLf
strSQL = strSQL & "ORDER BY" & VBCrLf
strSQL = strSQL & "  RL.RESOURCE_ID"

snpData.open strSQL, dbDatabase

If snpData.State = 1 then
Response.Write "<B><font color=" & chr(34) & "#0000FF" & chr(34) & "><p style=" & chr(34) & "position:absolute;top:15px;left:500px" & chr(34) & ">" & FormatDateTime(cdate(snpData("START_TIME")),2) & " " & FormatDateTime(cdate(snpData("START_TIME")),4) & " - " & FormatDateTime(cdate(snpData("END_TIME")),4) & "</p></font></b>" & vbcrlf

Do While Not snpData.EOF
If Not(IsNull(snpData("employee_id"))) Then
'A labor ticket was in process during this time period
Response.Write "<img alt=" & Chr(34) & snpData("resource_id") & "  " & snpData("description") & vbCrlf & snpData("employee_id") & "(" & snpData("clock_in") & " - " & snpData("clock_out") & ")" & Chr(34) & " src=" & chr(34) & "http://hoopercharles.files.wordpress.com/2010/09/graphicalworkcenterutilizationrunning.jpg" & chr(34) & " width=" & chr(34) & cStr(snpData("location_width")) & chr(34) & " height=" & chr(34) & cStr(snpData("location_height")) & chr(34) & " style=" & chr(34) & "position:absolute;top:" & cStr(cLng(snpData("location_top")) + 40) & "px;left:" & cStr(snpData("location_left")) & "px" & chr(34) & "/>" & vbcrlf
'Write the title down 1 pixel
Response.Write "<B><font color=" & chr(34) & "#00FFFF" & chr(34) & "><p style=" & chr(34) & "position:absolute;top:" & cStr(Round(41 + CSng(snpData("location_top")))) & "px;left:" & cStr(Round(CSng(snpData("location_left")))) & "px" & chr(34) & ">" & snpData("resource_id") & "</p></font></b>" & vbcrlf
Else
'No labor ticket was in process during this time period
Response.Write "<img alt=" & Chr(34) & snpData("resource_id") & "  " & snpData("description") & Chr(34) & " src=" & chr(34) & "http://hoopercharles.files.wordpress.com/2010/09/graphicalworkcenterutilizationstopped.jpg" & chr(34) & " width=" & chr(34) & cStr(snpData("location_width")) & chr(34) & " height=" & chr(34) & cStr(snpData("location_height")) & chr(34) & " style=" & chr(34) & "position:absolute;top:" & cStr(cLng(snpData("location_top")) + 40) & "px;left:" & cStr(snpData("location_left")) & "px" & chr(34) & "/>" & vbcrlf
'Write the title down 1 pixel
Response.Write "<B><font color=" & chr(34) & "#FF0000" & chr(34) & "><p style=" & chr(34) & "position:absolute;top:" & cStr(Round(41 + CSng(snpData("location_top")))) & "px;left:" & cStr(Round(CSng(snpData("location_left")))) & "px" & chr(34) & ">" & snpData("resource_id") & "</p></font></b>" & vbcrlf
End If
snpData.movenext
Loop
End if

snpData.Close
dbDatabase.Close

Set snpData = Nothing
Set dbDatabase = Nothing
%>

<script language="JavaScript1.2">
function NextInterval(){
reportform.submit();
}

setTimeout("NextInterval()",30000)
</script>
</body>

</html>```

GraphicalWorkCenterUtilization.asp (save as GraphicalWorkCenterUtilization.asp on a web server that supports classic ASP pages)

Below are samples of the output as the display as of time advanced – the zoom percent was set to 50.  Notice that the work centers go online and offline as the time progresses (click a picture to see a larger version of that picture):

The final example demonstrates how the display changed when the zoom percent was changed from 50% to 130%:

-

As the mouse pointer is moved over the boxes representing the work centers, a pop-up tooltip appears that describes the work center, as well as employee ID, clock in time, and clock out time for the first labor ticket at that work center in the time period.

——-

Hopefully, you have found this example to be interesting.  Your assignment is to now connect proximity switches to the devices in your office and surrounding areas, recording their location in the RESOURCE_LOCATION_DEMO table.  Then log the proximity switch status to the LABOR_TICKET_DEMO table so that you are able to determine when the water cooler, coffee machine, chairs, keyboards, and computer mice are in use.  Use the data to determine which employees are the hardest working, and which employees have determined how to think smarter rather than working harder.  :-)

## Oracle Logging Trigger Creator

25 08 2010

August 25, 2010

In a multi-user database environment it seems that sometimes changes just magically appear in ERP (or other) data.  The questions swirl, “Who changed that record?”; “I know that employee ABC arrived on time yesterday, why does the computer show he arrived late?”; “That new record is missing vital information, who created it?”, etc.  There are auditing features built into Oracle Database, and it is possible to review the redo log contents with LogMiner, but what if you just need a simple way to quietly record changes made to a table’s data?  Sure, triggers could be manually crafted to log changes, but it is a time consuming task, and consistency from one logging table/trigger to the next is difficult to maintain.  Well, this seems to be enough justification to develop a solution in search of a suitable problem, for which the solution will solve (we always invent the solution first, and then search for suitable problems that can be fixed, right?).

Today’s article will offer a VBS script that connects to an Oracle Database, and displays table definitions for all tables in the database using Internet Explorer as the script’s user interface.  For example, the following screen capture shows the tables in the TESTUSER schema with the definition of the LOCATIONS table displayed.  As shown, this table contains two VARCHAR2(15) columns in the primary key, a typical VARCHAR2(80) column, and a CHAR(1) column with a NOT NULL constraint.

—-

Switching to the PARTS table, we see that there is much more to the VBS script that automatically creates logging triggers and the table to maintain the captured data.  For each column in the table we are able to specify whether or not a change in a column’s value will cause a row to be inserted into the logging table – the “Trigger On Change” column in the VBS script’s user interface allows the user to specify the columns that cause the trigger to fire.  The “Log Values” column determines what data will be captured when the trigger fires:

• Both New and Old (capture the prior values before the change and the values after the change)
• New Values (capture only the values after the change)
• Old Values (capture only the values before the change)
• New Values and Delete (capture only the values after the change, unless the row is being deleted – then capture the values from before the change)
• Old Values and Insert (capture only the values before the change, unless the row is being inserted – then capture the values from the insert statement)
• Do Not Log (no data is captured for this column)

Clicking on the Create Data Log Script button builds the script which may be used to create the logging table and the logging trigger.  By default the logging table will be owned by the DATALOG user, although that can be changed within the VBS script.  The generated logging script displayed on the screen should look like this when the above selections are made:

(Added January 6, 2013: Sample trigger code shown above in a text file, save as TRIGGER_DATALOG_or112p_HPM_LOG_PARTS.SQL - without the .DOC extension.)

Notice that the generated script provides details about the purpose of the script, how to execute the script, what passwords need to be specified, sample commands to create a DATALOG tablespace and DATALOG user, the SQL to create the logging table, and the SQL to create the logging trigger.  By design, the default behavior of the script is easy to modify.

The script logic starts in the StartUp sub:

```Sub StartUp()
Dim strSQL
Dim intResult
Dim intFlag

'Fire up Internet Explorer
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Left = 0
objIE.Top = 0
objIE.Width = 1100
objIE.Height = 800
objIE.StatusBar = True
objIE.Toolbar = False

objIE.Document.Title = "Charles Hoopers Oracle Logging Trigger Creator"
objIE.Visible = True

'Database configuration
strDatabase = "MyDB"

On Error Resume Next

dbDatabase.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=" & strDatabase & ";User ID=" & strUsername & ";Password=" & strPassword & ";"
dbDatabase.Open

'Should verify that the connection attempt was successful, but I will leave that for someone else to code

strTableOwner = "TESTUSER"  'Set to the default owner name to be selected, possibly same as strUsername
strDataLogSchema = "DATALOG"```

As was the case for VBS scripts appearing in earlier articles that interact with the database, you must modify the database connection details, specifying username, password, and database.  Additionally, this script permits setting the default table schema (strTableOwner) and the default logging schema (strDataLogSchema) that display in the script’s user interface.  The script runs in a tight loop (sleeping for 0.5 seconds) in the StartUp sub until the script ends.  Within that loop the script reacts to changes in the Internet Explorer window, such as a selection of a different table name, or clicking the Create Data Log Script button.

When the user selects a different Table Owner the script executes the TableOwnerChange procedure which retrieves a list of the tables that reside in the selected user’s schema (note that this means the username used to run the script must have access to the various DBA_ views).  When a Table Name is selected, the TableNameChange procedure is executed which retrieves the definition for that table, and displays that definition in the Internet Explorer supplied user interface.  When the Create Data Log Script button is clicked the CreateLoggingScript procedure is executed, which writes the SQL*Plus compatible script to the root of the C:\ drive, and then displays the script on the screen.

If we run the script that was generated (after setting the correct passwords in the script), we should see something like this:

```SQL> @C:\TRIGGER_DATALOG_or112p_HPM_LOG_PARTS.SQL
Connected.
DROP TABLE DATALOG.HPM_LOG_PARTS
*
ERROR at line 1:
ORA-00942: table or view does not exist```

Note that the script attempted to drop an old logging table named DATALOG.HPM_LOG_PARTS at the start of the script, and that is what triggered the ORA-00942 error – just ignore this error message.

The PARTS table in this database has a definition that looks like this (it was a table created in one of my scripts mentioned in this article Faulty Quotes 5 – Block Sizes):

```CREATE TABLE PARTS (
PART_ID VARCHAR2(30) NOT NULL ENABLE,
DESCRIPTION VARCHAR2(40),
STOCK_UM VARCHAR2(15) NOT NULL ENABLE,
PLANNING_LEADTIME NUMBER DEFAULT 0 NOT NULL ENABLE,
ORDER_POLICY CHAR(1) DEFAULT 'M' NOT NULL ENABLE,
ORDER_POINT NUMBER(14,4),
SAFETY_STOCK_QTY NUMBER(14,4),
FIXED_ORDER_QTY NUMBER(14,4),
DAYS_OF_SUPPLY NUMBER,
MINIMUM_ORDER_QTY NUMBER(14,4),
MAXIMUM_ORDER_QTY NUMBER(14,4),
ENGINEERING_MSTR VARCHAR2(3),
PRODUCT_CODE VARCHAR2(15),
COMMODITY_CODE VARCHAR2(15),
MFG_NAME VARCHAR2(30),
MFG_PART_ID VARCHAR2(30),
FABRICATED CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
PURCHASED CHAR(1) DEFAULT 'Y' NOT NULL ENABLE,
STOCKED CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
DETAIL_ONLY CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
DEMAND_HISTORY CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
TOOL_OR_FIXTURE CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
INSPECTION_REQD CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
WEIGHT NUMBER(14,4),
WEIGHT_UM VARCHAR2(15),
DRAWING_ID VARCHAR2(15),
DRAWING_REV_NO VARCHAR2(8),
PREF_VENDOR_ID VARCHAR2(15),
PRIMARY_WHS_ID VARCHAR2(15),
PRIMARY_LOC_ID VARCHAR2(15),
BACKFLUSH_WHS_ID VARCHAR2(15),
BACKFLUSH_LOC_ID VARCHAR2(15),
INSPECT_WHS_ID VARCHAR2(15),
INSPECT_LOC_ID VARCHAR2(15),
MRP_REQUIRED CHAR(1) DEFAULT 'N',
MRP_EXCEPTIONS CHAR(1) DEFAULT 'N',
PRIVATE_UM_CONV CHAR(1) DEFAULT 'N',
AUTO_BACKFLUSH CHAR(1) DEFAULT 'Y',
PLANNER_USER_ID VARCHAR2(20),
ABC_CODE CHAR(1),
ANNUAL_USAGE_QTY NUMBER(15,4),
INVENTORY_LOCKED CHAR(1) DEFAULT 'N' NOT NULL ENABLE,
UNIT_MATERIAL_COST NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
UNIT_LABOR_COST NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
UNIT_BURDEN_COST NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
UNIT_SERVICE_COST NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
BURDEN_PERCENT NUMBER(5,2) DEFAULT 0 NOT NULL ENABLE,
BURDEN_PER_UNIT NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
PURC_BUR_PERCENT NUMBER(6,3) DEFAULT 0 NOT NULL ENABLE,
PURC_BUR_PER_UNIT NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
FIXED_COST NUMBER(15,2) DEFAULT 0 NOT NULL ENABLE,
UNIT_PRICE NUMBER(20,6),
NEW_MATERIAL_COST NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
NEW_LABOR_COST NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
NEW_BURDEN_COST NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
NEW_SERVICE_COST NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
NEW_BURDEN_PERCENT NUMBER(5,2) DEFAULT 0 NOT NULL ENABLE,
NEW_BURDEN_PERUNIT NUMBER(20,6) DEFAULT 0 NOT NULL ENABLE,
NEW_FIXED_COST NUMBER(15,2) DEFAULT 0 NOT NULL ENABLE,
MAT_GL_ACCT_ID VARCHAR2(30),
LAB_GL_ACCT_ID VARCHAR2(30),
BUR_GL_ACCT_ID VARCHAR2(30),
SER_GL_ACCT_ID VARCHAR2(30),
QTY_ON_HAND NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
QTY_AVAILABLE_ISS NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
QTY_AVAILABLE_MRP NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
QTY_ON_ORDER NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
QTY_IN_DEMAND NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
USER_1 VARCHAR2(80),
USER_2 VARCHAR2(80),
USER_3 VARCHAR2(80),
USER_4 VARCHAR2(80),
USER_5 VARCHAR2(80),
USER_6 VARCHAR2(80),
USER_7 VARCHAR2(80),
USER_8 VARCHAR2(80),
USER_9 VARCHAR2(80),
USER_10 VARCHAR2(80),
LT_PLUS_DAYS NUMBER,
LT_MINUS_DAYS NUMBER,
STATUS CHAR(1),
USE_SUPPLY_BEF_LT CHAR(1),
QTY_COMMITTED NUMBER(14,4) DEFAULT 0 NOT NULL ENABLE,
PRT_CREATE_USER_ID VARCHAR2(30) DEFAULT USER,
PRT_CREATE_DATE DATE DEFAULT SYSDATE,
CONSTRAINT "CHK_PART1" CHECK (
AND (ORDER_POLICY = 'N'
Or ORDER_POLICY = 'M'
Or ORDER_POLICY = 'F'
Or ORDER_POLICY = 'E'
Or ORDER_POLICY = 'D'
Or ORDER_POLICY = 'P')
AND (ORDER_POINT >= 0)
AND (SAFETY_STOCK_QTY >= 0)
AND (FIXED_ORDER_QTY >= 0)
AND (DAYS_OF_SUPPLY >= 0)
AND (MINIMUM_ORDER_QTY >= 0)
AND (MAXIMUM_ORDER_QTY >= 0)
AND (FABRICATED = 'Y' Or FABRICATED = 'N')
AND (PURCHASED = 'Y' Or PURCHASED = 'N')
AND (STOCKED = 'Y' Or STOCKED = 'N')
AND (DETAIL_ONLY = 'Y' Or DETAIL_ONLY = 'N')
AND (DEMAND_HISTORY = 'Y' Or DEMAND_HISTORY = 'N')
AND (TOOL_OR_FIXTURE = 'Y' Or TOOL_OR_FIXTURE = 'N')
AND (MRP_REQUIRED = 'Y' Or MRP_REQUIRED = 'N')
AND (MRP_EXCEPTIONS = 'Y' Or MRP_EXCEPTIONS = 'N')
AND (PRIVATE_UM_CONV = 'Y' Or PRIVATE_UM_CONV = 'N')
AND (INVENTORY_LOCKED = 'Y' Or INVENTORY_LOCKED = 'N')
AND (INSPECTION_REQD = 'Y' Or INSPECTION_REQD = 'N')) ENABLE,
PRIMARY KEY (PART_ID),
CONSTRAINT "FKEY_INSP" FOREIGN KEY (INSPECT_WHS_ID, INSPECT_LOC_ID)
REFERENCES LOCATIONS (WAREHOUSE_ID, LOCATION_ID) ENABLE,
CONSTRAINT "FKEY_PREF_VENDOR" FOREIGN KEY (PREF_VENDOR_ID)
REFERENCES VENDORS (VENDOR_ID) ENABLE,
CONSTRAINT "FKEY_UM" FOREIGN KEY (WEIGHT_UM)
REFERENCES UMS (UNIT_OF_MEASURE) ENABLE,
CONSTRAINT "FKEY_STOCK_UM" FOREIGN KEY (STOCK_UM)
REFERENCES UMS (UNIT_OF_MEASURE) ENABLE);```

Yes, it is an ugly, long table definition that references a couple of additional tables.  Now, let’s make some changes to the above table:

```INSERT INTO PARTS (
PART_ID,
DESCRIPTION,
STOCK_UM,
ORDER_POLICY,
FABRICATED,
PURCHASED,
STOCKED,
DETAIL_ONLY,
DEMAND_HISTORY,
TOOL_OR_FIXTURE,
INSPECTION_REQD,
INVENTORY_LOCKED,
UNIT_MATERIAL_COST,
UNIT_LABOR_COST,
UNIT_BURDEN_COST,
UNIT_SERVICE_COST,
BURDEN_PERCENT,
BURDEN_PER_UNIT,
PURC_BUR_PERCENT,
PURC_BUR_PER_UNIT,
FIXED_COST,
UNIT_PRICE,
NEW_MATERIAL_COST,
NEW_LABOR_COST,
NEW_BURDEN_COST,
NEW_SERVICE_COST,
NEW_BURDEN_PERCENT,
NEW_BURDEN_PERUNIT,
NEW_FIXED_COST,
QTY_ON_HAND,
QTY_AVAILABLE_ISS,
QTY_AVAILABLE_MRP,
QTY_ON_ORDER,
QTY_IN_DEMAND,
QTY_COMMITTED)
SELECT
'AA'||ROWNUM,
'WATCH ME',
'EA',
0,
'N',
'Y',
'N',
'N',
'N',
'N',
'N',
'N',
'N',
100,
0,
0,
0,
0,
0,
0,
0,
0,
109,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
FROM
DUAL
CONNECT BY
LEVEL<=5;

5 rows created.```

The above added 5 rows to the PARTS table.  Now let’s modify those rows:

```UPDATE
PARTS
SET
DESCRIPTION='CHANGED'
WHERE
PART_ID IN ('AA1','AA2');

2 rows updated.

UPDATE
PARTS
SET
SAFETY_STOCK_QTY=10,
STOCK_UM='PC',
FIXED_ORDER_QTY=10
WHERE
PART_ID IN ('AA1','AA4','A5');

2 rows updated.

UPDATE
PARTS
SET
SAFETY_STOCK_QTY=10,
STOCK_UM='PC',
FIXED_ORDER_QTY=10
WHERE
PART_ID IN ('AA1','AA4','AA5');

3 rows updated.

DELETE FROM
PARTS
WHERE
PART_ID IN ('AA2','AA3','AA4','AA5');

4 rows deleted.

COMMIT;

SELECT
COUNT(*)
FROM
DATALOG.HPM_LOG_PARTS;

COUNT(*)
----------
14```

As indicated by the above output, our logging table now has 14 rows of data.  Let’s view that data using a spreadsheet program (in this case Excel 2010):

Obviously, it is important to be smart when deciding which columns to capture, whether the old and/or new values should be captured, and changes to which columns should cause the trigger to fire.  So, what was the inspiration for this article?  One of the features in my Toy Project for Performance Tuning:

The VBS script should work on client computers running Windows 2000 or above.  Have fun with the script, and let me know about any improvements that you make to the script (there were a couple of neat improvements to the Automated DBMS XPLAN Viewer script).

## Oracle Statistics Chart Viewer

11 07 2010

July 11, 2010

Last week I provided a blog article that created a horizonal orientation auto-scaling HTML-based bar chart using nothing more than a SQL statement.  At first glace the final SQL statement would likely appear to be very complicated, however if you follow the blog article from the start, the solution really is quite straightforward.  Today’s blog article will build on the final result of last week’s blog article, however the path to the final solution will be very different, and multiple vertical orientation bar charts will appear on a single web page.

The inspiration for today’s blog article comes from the Real Time Monitor in my Toy Project for Performance Tuning:

Since this project is a bit more advanced than last week’s project, we will use a programming language to perform the calculations and write out the HTML directly to a web browser.  We will use VBScript, which is available on most computers running Windows, so that will make it easy for you to modify the script for your needs:

• Changing the statistics to be displayed
• Changing the sampling frequency
• Changing the number of charts displayed per row and the number of rows
• Changing the spacing between the charts
• Changing the bar colors
• Changing the background color

The actual script may be a little difficult to understand.  Basically, the script queries several views, displays the historical delta values of the specified statistics in chart form, and then waits for the sampling frequency counter to tick down before collecting the next set of statistics.  The statistics collection process ends when the user clicks the Close button.  The following views are queried every time the statistics are gathered:

• V\$OSSTAT
• V\$SYS_TIME_MODEL
• V\$WAITSTAT
• V\$SYSSTAT
• V\$SYSTEM_EVENT

The statistic names from some of the views are prefixed to reduce the chances of name collisions between statistics from the different views.  For example, the statistics from V\$SYS_TIME_MODEL are prefixed with “SYS TIME MODEL ” and the statistics from V\$WAITSTAT are prefixed with “WAITSTAT TIME “.  The current value is displayed in the left-most bar, with up to the previous 29 values displayed in the remaining bars.

### Sample Charts:

Flat appearance with three horizontal and two vertical bar charts with the refresh interval set at 10 seconds:

-

Flat appearance with six horizontal and four vertical bar charts with the refresh interval set at 10 seconds:

-

3D appearance with three horizontal and two vertical bar charts with the refresh interval set at 10 seconds:

—–

So, what changes do you need to make to use the script?  First, if you are running a release of Oracle Database prior to 10.1 you will need to remove the queries of V\$SYS_TIME_MODEL and V\$OSSTAT.  Second, you need to have an Oracle user other than a SYSDBA user (SYS) that is able to query the various performance views.  Third, you need to specify the username and password of that user in the script:

```'Database configuration
strDatabase = "MyDB"```

Next, decide which statistics you would like to display in the charts and in what order (sequenced across the page and then down the page).  The statistic names are all recorded in uppercase, with the following as defaults in the script:

```'The statistics to be displayed in the charts
strStatToChart(1) = "BUSY_TIME"
strStatToChart(2) = "CPU USED BY THIS SESSION"
strStatToChart(3) = "USER CALLS"
strStatToChart(4) = "SQL*NET ROUNDTRIPS TO/FROM CLIENT"
strStatToChart(5) = "USER COMMITS"
strStatToChart(6) = "USER ROLLBACKS"
strStatToChart(7) = "TRANSACTION ROLLBACKS"
strStatToChart(8) = "DB FILE SEQUENTIAL READ"
strStatToChart(9) = "DB FILE SCATTERED READ"
strStatToChart(10) = "READ BY OTHER SESSION"
strStatToChart(11) = "SYS TIME MODEL DB TIME"
strStatToChart(12) = "SYS TIME MODEL DB CPU"
strStatToChart(13) = "SYS TIME MODEL BACKGROUND CPU TIME"
strStatToChart(14) = "PARSE TIME CPU"
strStatToChart(15) = "RECURSIVE CPU USAGE"
strStatToChart(16) = "CONSISTENT GETS"
strStatToChart(17) = "DB BLOCK GETS"
strStatToChart(19) = "DATA BLOCKS CONSISTENT READS - UNDO RECORDS APPLIED"
strStatToChart(20) = "REDO SIZE"
strStatToChart(21) = "REDO WRITE TIME"
strStatToChart(30) = "WAITSTAT TIME DATA BLOCK"```

Next, decide how the charts should appear on the page.  For example, for six charts, you might decide to use the following settings (the defaults)

```'Large Charts
intChartsHorizontal = 3
intChartsVertical = 2
lngLeftMargin = 50
lngTopMargin = 50
lngChartSpacingX = 50
lngChartSpacingY = 50
lngChartWidth = 300
lngChartHeight = 300
strBar3DEffect = "border-style:outset;border-width:thin;"```

For 24 charts, you might use the following settings (the defaults):

```'Small Charts
intChartsHorizontal = 6
intChartsVertical = 4
lngLeftMargin = 50
lngTopMargin = 50
lngChartSpacingX = 10
lngChartSpacingY = 20
lngChartWidth = 150
lngChartHeight = 150
strBar3DEffect = "border-style:none;border-width:inherit;"```

Finally, decide the color scheme of the charts, the update frequency, and whether delta values or absolute values should be displayed.  The following are the defaults:

```strBarColorStandard = "#AAAAAA"
strBarColorCurrent = "#1010FF"
strBarTextColor = "#000000"
strChartBackgroundColor = "#EEEEEE"
strChart3DEffect = "border-style:inset;"
intShowDelta = True
intRefreshSeconds = 60 'Refresh interval of the chart data```

I have found the book “CSS: The Definitive Guide” to be very helpful when adjusting the various appearance options.

Please post any improvements that you make to the script.  It might be interesting to note that this code can be incorporated into the Database Inspector Gadget that I created a while ago if the computer is running Windows Vista or Windows 7.

—————————————————————–

Partially related side note: There are still a couple of seats remaining for the Michigan OakTable Symposium, which will certainly have the most entertaining and in-depth training sessions for DBAs and developers in 2010.  There are 20+ OakTable Network members from around the world converging on Ann Arbor, MI. in mid-September 2010.  I will be in attendance, but not presenting (Randolf Geist, who co-wrote two chapters in the Expert Oracle Practices book with me is flying in from Germany to present two sessions).  More information may be found here: Michigan OakTable Symposium.

## Create an Auto-Scaling HTML Chart using Only SQL

8 07 2010

July 8, 2010 (Modified July 9, 2010)

I thought that I would try something a little different today – build an auto-scaling HTML bar chart using nothing more than a SQL statement.  I mentioned in this book review that I was impressed with the HTML chart that was included in the book, but I felt that it might be more interesting if the example used absolute positioning, rather than an HTML table.  So, I built an example using dynamic positioning that is not based on what appears in that book.

We will use the sample table from this blog article (side note: this is an interesting article that shows how a VBS script can generate a 10046 trace file, and then transform that trace file back into a VBS script), just with the table renamed to T1.

```CREATE TABLE T1 AS
SELECT
DECODE(TRUNC(DBMS_RANDOM.VALUE(0,5)),
0,'MIKE',
1,'ROB',
2,'SAM',
3,'JOE',
4,'ERIC') EMPLOYEE_ID,
TRUNC(SYSDATE)-ROUND(DBMS_RANDOM.VALUE(0,1000)) SHIFT_DATE,
DECODE(TRUNC(DBMS_RANDOM.VALUE(0,10)),
0,'VAC',
1,'HOL',
2,'BEREAVE',
3,'JURY',
4,'ABS',
5,'EXCUSE',
6,'MIL',
'OTHER') INDIRECT_ID
FROM
DUAL
CONNECT BY
LEVEL<=1000;

COMMIT;```

Now that we have 1000 rows in the sample table, let’s see how many entries fall into each week in the table (the week starts on a Monday) for those indirect entries that are either VAC, ABS, or EXCUSE:

```SELECT
NEXT_DAY(SHIFT_DATE,'MONDAY')-7 WEEK_OF,
COUNT(*) IND
FROM
T1
WHERE
INDIRECT_ID IN ('VAC','ABS','EXCUSE')
GROUP BY
NEXT_DAY(SHIFT_DATE,'MONDAY')-7
ORDER BY
1;```

Your results of course will be different from what follows due to the randomization of the data, but this is what was returned from my database:

```WEEK_OF   IND
--------- ---
08-OCT-07   1
15-OCT-07   2
22-OCT-07   4
29-OCT-07   3
05-NOV-07   3
03-DEC-07   2
10-DEC-07   3
24-DEC-07   2
...
05-JAN-09   1
12-JAN-09   3
19-JAN-09   7
02-FEB-09   1
...
21-JUN-10   3
28-JUN-10   2
05-JUL-10   2```

The above SQL statement should work for the base query, now we need to start manipulating the data so that we are able to calculate the size and location of the bars in the chart.  We will slide the above SQL statement into an inline view:

```SELECT
WEEK_OF,
IND,
MAX(IND) OVER () MAX_IND,
COUNT(WEEK_OF) OVER () COUNT_WEEK_OF,
ROWNUM RN
FROM
(SELECT
NEXT_DAY(SHIFT_DATE,'MONDAY')-7 WEEK_OF,
COUNT(*) IND
FROM
T1
WHERE
INDIRECT_ID IN ('VAC','ABS','EXCUSE')
GROUP BY
NEXT_DAY(SHIFT_DATE,'MONDAY')-7
ORDER BY
1);```

In addition to returning the original data from the SQL statement, we are now also returning the maximum data value, the total number of weeks with at least one entry, and a row counter:

```WEEK_OF   IND MAX_IND COUNT_WEEK_OF    RN
--------- --- ------- ------------- -----
08-OCT-07   1       7           126     1
15-OCT-07   2       7           126     2
22-OCT-07   4       7           126     3
29-OCT-07   3       7           126     4
05-NOV-07   3       7           126     5
03-DEC-07   2       7           126     6
10-DEC-07   3       7           126     7
...
05-JAN-09   1       7           126    57
12-JAN-09   3       7           126    58
19-JAN-09   7       7           126    59
02-FEB-09   1       7           126    60
...
14-JUN-10   2       7           126   123
21-JUN-10   3       7           126   124
28-JUN-10   2       7           126   125
05-JUL-10   2       7           126   126```

Next, we need to calculate the position and size of each of the bars in the chart, so we will again slide the above into an inline view:

```SELECT
WEEK_OF,
IND,
MAX_IND,
COUNT_WEEK_OF,
RN,
TRUNC(300 * IND/MAX_IND) BAR_WIDTH,
TRUNC(800 * 1/COUNT_WEEK_OF) BAR_HEIGHT,
TRUNC(800 * 1/COUNT_WEEK_OF * (RN-1)) BAR_TOP,
100 BAR_LEFT
FROM
(SELECT
WEEK_OF,
IND,
MAX(IND) OVER () MAX_IND,
COUNT(WEEK_OF) OVER () COUNT_WEEK_OF,
ROWNUM RN
FROM
(SELECT
NEXT_DAY(SHIFT_DATE,'MONDAY')-7 WEEK_OF,
COUNT(*) IND
FROM
T1
WHERE
INDIRECT_ID IN ('VAC','ABS','EXCUSE')
GROUP BY
NEXT_DAY(SHIFT_DATE,'MONDAY')-7
ORDER BY
1));```

You might notice in the above that I specified that the maximum width of the chart will be 300 (pixels) and the maximum height will be 800 (pixels).  Here is the output:

```WEEK_OF   IND MAX_IND COUNT_WEEK_OF    RN  BAR_WIDTH BAR_HEIGHT BAR_TOP BAR_LEFT
--------- --- ------- ------------- ----- ---------- ---------- ------- --------
08-OCT-07   1       7           126     1         42          6       0      100
15-OCT-07   2       7           126     2         85          6       6      100
22-OCT-07   4       7           126     3        171          6      12      100
29-OCT-07   3       7           126     4        128          6      19      100
05-NOV-07   3       7           126     5        128          6      25      100
03-DEC-07   2       7           126     6         85          6      31      100
10-DEC-07   3       7           126     7        128          6      38      100
24-DEC-07   2       7           126     8         85          6      44      100
...
05-JAN-09   1       7           126    57         42          6     355      100
12-JAN-09   3       7           126    58        128          6     361      100
19-JAN-09   7       7           126    59        300          6     368      100
02-FEB-09   1       7           126    60         42          6     374      100
...
14-JUN-10   2       7           126   123         85          6     774      100
21-JUN-10   3       7           126   124        128          6     780      100
28-JUN-10   2       7           126   125         85          6     787      100
05-JUL-10   2       7           126   126         85          6     793      100```

Now what?  We need to convert the above into HTML using DIV tags to position the bars as calculated.  Prior to the first row we need to write a couple of HTML tags to set the page title, and after the last row we need to write a couple more HTML tags to close the BODY and HTML section of the document.  The transformed SQL statement looks like this:

```SET TRIMSPOOL ON
SET LINESIZE 400
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET PAGESIZE 0
SET SQLPROMPT ''

SPOOL C:\CUSTOM_CHART.HTM

SELECT
'<div style="position:absolute;' ||
'top:' || TO_CHAR(TRUNC(800 * 1/COUNT_WEEK_OF * (RN-1))) || 'px;' ||
'left:' || TO_CHAR(5) || 'px;' ||
'width:' || TO_CHAR(100) || 'px;' ||
'height:' || TO_CHAR(TRUNC(800 * 1/COUNT_WEEK_OF)) || 'px;' ||
'"><font size="1px" color="#0000FF">' || TO_CHAR(WEEK_OF,'MM/DD/YY') ||
REPLACE('     ',' ',CHR(38) || 'nbsp;') || TO_CHAR(IND) || '</font></div>' ||
'<div style="background:#444466;position:absolute;' ||
'top:' || TO_CHAR(TRUNC(800 * 1/COUNT_WEEK_OF * (RN-1))) || 'px;' ||
'left:' || TO_CHAR(100) || 'px;' ||
'width:' || TO_CHAR(TRUNC(300 * IND/MAX_IND)) || 'px;' ||
'height:' || TO_CHAR(TRUNC(800 * 1/COUNT_WEEK_OF)) || 'px;' ||
'"><font size="1px" color="#FFFFFF"></font></div>' ||
DECODE(RN,COUNT_WEEK_OF, CHR(13) || CHR(10) || '</body></html>',' ') HTML_LINE
FROM
(SELECT
WEEK_OF,
IND,
MAX(IND) OVER () MAX_IND,
COUNT(WEEK_OF) OVER () COUNT_WEEK_OF,
ROWNUM RN
FROM
(SELECT
NEXT_DAY(SHIFT_DATE,'MONDAY')-7 WEEK_OF,
COUNT(*) IND
FROM
T1
WHERE
INDIRECT_ID IN ('VAC','ABS','EXCUSE')
GROUP BY
NEXT_DAY(SHIFT_DATE,'MONDAY')-7
ORDER BY
1));

SPOOL OFF```

There is a slight problem with the above, the SQL statement and SPOOL OFF are printed in the resulting HTML file – if someone knows how to avoid that behavior (without placing the above into another script file), I would like to see how it is done (Oracle’s documentation did not help).

This is what the resulting HTML file looks like:

The number of result rows from the query was a bit high (126) so the bars are significantly compressed in height.  Just to see what happens, let’s add the following to the WHERE clause in the inner-most inline view:

`AND SHIFT_DATE >= TO_DATE('01-JAN-2010','DD-MON-YYYY')`

The resulting chart now looks like this:

Of course it is possible to adjust the colors of the font (#0000FF) and the bars (#444466), which are specified in hex in the format of RRGGBB (red green blue).  It is also possible to adjust the color of the bars to reflect the value represented by the bar, but that is an exercise for the reader.  For those who need to feel creative, it is also possible to display pictures in the bars, but that is also an exercise left for the reader.

—-

Edit: The sample output from the SQL statement displays correctly on Red Hat Enterprise Linux 3 using Firefox 0.8:

## Working with Oracle’s Time Model Data 3

9 02 2010

February 9, 2010

In the previous article in this series, we created a structured view of the V\$SYS_TIME_MODEL view:

Then on demand brought in session level detail for the same statistics so that it was possible to investigate why a statistic value continued to grow.  The previous article also displayed delta values from V\$OSSTAT and the wait events from V\$SYSTEM_EVENT.  It appears that a couple people liked the previous effort, and I gave serious thought to having the computer provide audio feedback when a session started consuming a lot of resources – but then I started to wonder if maybe we should instead have an audio warning when a session is consuming less than the average resource consumption so that we could then determine who is reading this article rather than working. :-)  In the end, I decided to leave out the audio feedback.  So, what else can we do with the framework that was provided in the second article of this series?

It might be nice to be able to drill into session-level wait events to determine why a wait event’s time delta keeps growing, maybe by adding a couple of extra buttons to the web page that is created on the fly by the VBS script:

It might also be nice to be able to view the DBMS_XPLAN for a query that was identified as a SQL statement that was recently executed by a session.  One of the problems with the original script is that it only retrieved the SQL_ID and SQL_CHILD_NUMBER from V\$SESSION, and it seemed that those columns contained NULL values just as frequently as they contained data.  The updated script will attempt to return the PREV_SQL_ID and PREV_CHILD_NUMBER columns if the other two columns contain NULL values.  In addition to retrieving the DBMS_XPLAN for the CHILD_NUMBER that is executed by the session, it might also be helpful to retrieve the DBMS_XPLAN for all child cursors for a given SQL_ID so that we are able to determine if the execution plan changed, and why it changed.  While we are at it, let’s make it possible to enable a 10046 extended SQL trace for a session just by clicking a button on the web page.  With the changes to the script, the generated web page might look something like this (note that this is a touched up image – the computer name and username were erased):

Scrolling down to the bottom of the page, note that the Session Waits button for the db file scattered read wait event was clicked:

So, what happens if one of the XPlan buttons is clicked?  We could just write the execution plan to a text file, but where is the fun in doing that?  How about popping up another web browser page that lists all execution plans for a query that are currently in the library cache, the reasons for those generated execution plans from V\$SQL_SHARED_CURSOR, and the bind variable definitions from V\$SQL_BIND_METADATA.  The generated web page might look something like this (with duplicate sections from the different child cursors removed to save space):

```**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 1
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
WORK_ORDER WO,    REQUIREMENT R,    PART P  WHERE
WO.TYPE='W'    AND WO.SUB_ID='0'    AND WO.STATUS IN ('F','R','U')
AND WO.DESIRED_QTY>WO.RECEIVED_QTY    AND WO.PART_ID= :1    AND
R.WORKORDER_TYPE='W'    AND WO.TYPE=R.WORKORDER_TYPE    AND
WO.BASE_ID=R.WORKORDER_BASE_ID    AND WO.LOT_ID=R.WORKORDER_LOT_ID
AND WO.SPLIT_ID=R.WORKORDER_SPLIT_ID    AND R.SUBORD_WO_SUB_ID IS NULL
AND R.CALC_QTY>R.ISSUED_QTY    AND R.PART_ID=P.ID  ORDER BY
R.PART_ID

Plan hash value: 3990487722

------------------------------------------------------------------------------------------------------------
| Id  | Operation                       | Name            | Rows  | Bytes | Cost (%CPU)| Time     | Inst   |
------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |                 |       |       |    27 (100)|          |        |
|   1 |  SORT UNIQUE                    |                 |     9 |   747 |    26   (4)| 00:00:01 |        |
|   2 |   NESTED LOOPS                  |                 |       |       |            |          |        |
|   3 |    NESTED LOOPS                 |                 |     9 |   747 |    25   (0)| 00:00:01 |        |
|   4 |     NESTED LOOPS                |                 |     9 |   621 |    16   (0)| 00:00:01 |        |
|*  5 |      TABLE ACCESS BY INDEX ROWID| WORK_ORDER      |     1 |    37 |    13   (0)| 00:00:01 |   OR11 |
|*  6 |       INDEX RANGE SCAN          | X_WORK_ORDER_1  |    21 |       |     3   (0)| 00:00:01 |   OR11 |
|*  7 |      TABLE ACCESS BY INDEX ROWID| REQUIREMENT     |    25 |   800 |     3   (0)| 00:00:01 |   OR11 |
|*  8 |       INDEX RANGE SCAN          | X_REQUIREMENT_5 |     1 |       |     2   (0)| 00:00:01 |   OR11 |
|*  9 |     INDEX UNIQUE SCAN           | SYS_C0011459    |     1 |       |     0   (0)|          |   OR11 |
|  10 |    TABLE ACCESS BY INDEX ROWID  | PART            |     1 |    14 |     1   (0)| 00:00:01 |   OR11 |
------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
5 - filter(("WO"."DESIRED_QTY">"WO"."RECEIVED_QTY" AND "WO"."SUB_ID"='0' AND
INTERNAL_FUNCTION("WO"."STATUS") AND "WO"."TYPE"='W'))
6 - access("WO"."PART_ID"=:1)
7 - filter("R"."CALC_QTY">"R"."ISSUED_QTY")
8 - access("WO"."TYPE"="R"."WORKORDER_TYPE" AND "WO"."BASE_ID"="R"."WORKORDER_BASE_ID" AND
"WO"."LOT_ID"="R"."WORKORDER_LOT_ID" AND "WO"."SPLIT_ID"="R"."WORKORDER_SPLIT_ID" AND
"R"."SUBORD_WO_SUB_ID" IS NULL)
filter("R"."WORKORDER_TYPE"='W')
9 - access("R"."PART_ID"="P"."ID")

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
ROW_LEVEL_SEC_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 2
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
...
Plan hash value: 3990487722

------------------------------------------------------------------------------------------------------------
| Id  | Operation                       | Name            | Rows  | Bytes | Cost (%CPU)| Time     | Inst   |
------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |                 |       |       |    27 (100)|          |        |
|   1 |  SORT UNIQUE                    |                 |     9 |   747 |    26   (4)| 00:00:01 |        |
|   2 |   NESTED LOOPS                  |                 |       |       |            |          |        |
|   3 |    NESTED LOOPS                 |                 |     9 |   747 |    25   (0)| 00:00:01 |        |
|   4 |     NESTED LOOPS                |                 |     9 |   621 |    16   (0)| 00:00:01 |        |
|*  5 |      TABLE ACCESS BY INDEX ROWID| WORK_ORDER      |     1 |    37 |    13   (0)| 00:00:01 |   OR11 |
|*  6 |       INDEX RANGE SCAN          | X_WORK_ORDER_1  |    21 |       |     3   (0)| 00:00:01 |   OR11 |
|*  7 |      TABLE ACCESS BY INDEX ROWID| REQUIREMENT     |    25 |   800 |     3   (0)| 00:00:01 |   OR11 |
|*  8 |       INDEX RANGE SCAN          | X_REQUIREMENT_5 |     1 |       |     2   (0)| 00:00:01 |   OR11 |
|*  9 |     INDEX UNIQUE SCAN           | SYS_C0011459    |     1 |       |     0   (0)|          |   OR11 |
|  10 |    TABLE ACCESS BY INDEX ROWID  | PART            |     1 |    14 |     1   (0)| 00:00:01 |   OR11 |
------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
5 - filter(("WO"."DESIRED_QTY">"WO"."RECEIVED_QTY" AND "WO"."SUB_ID"='0' AND
INTERNAL_FUNCTION("WO"."STATUS") AND "WO"."TYPE"='W'))
6 - access("WO"."PART_ID"=:1)
7 - filter("R"."CALC_QTY">"R"."ISSUED_QTY")
8 - access("WO"."TYPE"="R"."WORKORDER_TYPE" AND "WO"."BASE_ID"="R"."WORKORDER_BASE_ID" AND
"WO"."LOT_ID"="R"."WORKORDER_LOT_ID" AND "WO"."SPLIT_ID"="R"."WORKORDER_SPLIT_ID" AND
"R"."SUBORD_WO_SUB_ID" IS NULL)
filter("R"."WORKORDER_TYPE"='W')
9 - access("R"."PART_ID"="P"."ID")

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
ROW_LEVEL_SEC_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 3
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
...

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 4
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
...

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
ROW_LEVEL_SEC_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 5
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
...

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
ROW_LEVEL_SEC_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 7
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
...
Plan hash value: 1336181825

------------------------------------------------------------------------------------------------------------------
| Id  | Operation                             | Name            | Rows  | Bytes | Cost (%CPU)| Time     | Inst   |
------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                      |                 |       |       |   577 (100)|          |        |
|   1 |  SORT UNIQUE                          |                 |   838 | 69554 |   576   (1)| 00:00:07 |        |
|*  2 |   HASH JOIN                           |                 |   838 | 69554 |   575   (1)| 00:00:07 |        |
|   3 |    NESTED LOOPS                       |                 |       |       |            |          |        |
|   4 |     NESTED LOOPS                      |                 |   840 | 57960 |   164   (0)| 00:00:02 |        |
|*  5 |      TABLE ACCESS BY INDEX ROWID      | WORK_ORDER      |    35 |  1295 |    60   (0)| 00:00:01 |   OR11 |
|   6 |       BITMAP CONVERSION TO ROWIDS     |                 |       |       |            |          |        |
|   7 |        BITMAP AND                     |                 |       |       |            |          |        |
|   8 |         BITMAP CONVERSION FROM ROWIDS |                 |       |       |            |          |        |
|*  9 |          INDEX RANGE SCAN             | X_WORK_ORDER_1  | 13721 |       |     8   (0)| 00:00:01 |   OR11 |
|  10 |         BITMAP OR                     |                 |       |       |            |          |        |
|  11 |          BITMAP CONVERSION FROM ROWIDS|                 |       |       |            |          |        |
|* 12 |           INDEX RANGE SCAN            | X_WORK_ORDER_2  | 13721 |       |     6   (0)| 00:00:01 |   OR11 |
|  13 |          BITMAP CONVERSION FROM ROWIDS|                 |       |       |            |          |        |
|* 14 |           INDEX RANGE SCAN            | X_WORK_ORDER_2  | 13721 |       |     1   (0)| 00:00:01 |   OR11 |
|  15 |          BITMAP CONVERSION FROM ROWIDS|                 |       |       |            |          |        |
|* 16 |           INDEX RANGE SCAN            | X_WORK_ORDER_2  | 13721 |       |    19   (0)| 00:00:01 |   OR11 |
|* 17 |      INDEX RANGE SCAN                 | X_REQUIREMENT_5 |     1 |       |     2   (0)| 00:00:01 |   OR11 |
|* 18 |     TABLE ACCESS BY INDEX ROWID       | REQUIREMENT     |    24 |   768 |     3   (0)| 00:00:01 |   OR11 |
|  19 |    TABLE ACCESS FULL                  | PART            | 37032 |   506K|   410   (1)| 00:00:05 |   OR11 |
------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("R"."PART_ID"="P"."ID")
5 - filter(("WO"."DESIRED_QTY">"WO"."RECEIVED_QTY" AND "WO"."SUB_ID"='0' AND "WO"."TYPE"='W'))
9 - access("WO"."PART_ID"=:1)
12 - access("WO"."STATUS"='F')
14 - access("WO"."STATUS"='R')
16 - access("WO"."STATUS"='U')
17 - access("WO"."TYPE"="R"."WORKORDER_TYPE" AND "WO"."BASE_ID"="R"."WORKORDER_BASE_ID" AND
"WO"."LOT_ID"="R"."WORKORDER_LOT_ID" AND "WO"."SPLIT_ID"="R"."WORKORDER_SPLIT_ID" AND
"R"."SUBORD_WO_SUB_ID" IS NULL)
filter("R"."WORKORDER_TYPE"='W')
18 - filter("R"."CALC_QTY">"R"."ISSUED_QTY")

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 8
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
WO.TYPE='W'    AND WO.SUB_ID='0'    AND WO.STATUS IN ('F','R','U')
AND WO.DESIRED_QTY>WO.RECEIVED_QTY    AND WO.PART_ID= :1    AND
R.WORKORDER_TYPE='W'    AND WO.TYPE=R.WORKORDER_TYPE    AND
WO.BASE_ID=R.WORKORDER_BASE_ID    AND WO.LOT_ID=R.WORKORDER_LOT_ID
AND WO.SPLIT_ID=R.WORKORDER_SPLIT_ID    AND R.SUBORD_WO_SUB_ID IS NULL
AND R.CALC_QTY>R.ISSUED_QTY    AND R.PART_ID=P.ID  ORDER BY
R.PART_ID

Plan hash value: 3990487722

------------------------------------------------------------------------------------------------------------
| Id  | Operation                       | Name            | Rows  | Bytes | Cost (%CPU)| Time     | Inst   |
------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                |                 |       |       |    27 (100)|          |        |
|   1 |  SORT UNIQUE                    |                 |     9 |   747 |    26   (4)| 00:00:01 |        |
|   2 |   NESTED LOOPS                  |                 |       |       |            |          |        |
|   3 |    NESTED LOOPS                 |                 |     9 |   747 |    25   (0)| 00:00:01 |        |
|   4 |     NESTED LOOPS                |                 |     9 |   621 |    16   (0)| 00:00:01 |        |
|*  5 |      TABLE ACCESS BY INDEX ROWID| WORK_ORDER      |     1 |    37 |    13   (0)| 00:00:01 |   OR11 |
|*  6 |       INDEX RANGE SCAN          | X_WORK_ORDER_1  |    21 |       |     3   (0)| 00:00:01 |   OR11 |
|*  7 |      TABLE ACCESS BY INDEX ROWID| REQUIREMENT     |    24 |   768 |     3   (0)| 00:00:01 |   OR11 |
|*  8 |       INDEX RANGE SCAN          | X_REQUIREMENT_5 |     1 |       |     2   (0)| 00:00:01 |   OR11 |
|*  9 |     INDEX UNIQUE SCAN           | SYS_C0011459    |     1 |       |     0   (0)|          |   OR11 |
|  10 |    TABLE ACCESS BY INDEX ROWID  | PART            |     1 |    14 |     1   (0)| 00:00:01 |   OR11 |
------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
5 - filter(("WO"."DESIRED_QTY">"WO"."RECEIVED_QTY" AND "WO"."SUB_ID"='0' AND
INTERNAL_FUNCTION("WO"."STATUS") AND "WO"."TYPE"='W'))
6 - access("WO"."PART_ID"=:1)
7 - filter("R"."CALC_QTY">"R"."ISSUED_QTY")
8 - access("WO"."TYPE"="R"."WORKORDER_TYPE" AND "WO"."BASE_ID"="R"."WORKORDER_BASE_ID" AND
"WO"."LOT_ID"="R"."WORKORDER_LOT_ID" AND "WO"."SPLIT_ID"="R"."WORKORDER_SPLIT_ID" AND
"R"."SUBORD_WO_SUB_ID" IS NULL)
filter("R"."WORKORDER_TYPE"='W')
9 - access("R"."PART_ID"="P"."ID")

**********************************************************************************************
Reason for Child Cursor Creation
BIND_MISMATCH
**********
Bind Variable Definitions
Position:1  Max Length:32  VARCHAR2  Name:1
**********
SQL_ID  8p3pacrbngv80, child number 9
-------------------------------------
SELECT DISTINCT    R.PART_ID,    P.FABRICATED,    P.PURCHASED  FROM
...
**********************************************************************************************```

In the above, the selected session was actually using the execution plan for child number 3.  Notice that one of the execution plans converted normal B*Tree indexes on the fly to permit comparisons much like what are possible with bitmap indexes.  A large portion of the execution plans were created, at least in part, because of a bind mismatch – see the documentation V\$SQL_SHARED_CURSOR for more information.

This example requires at a minimum Oracle Database 10.1.0.1 running on Windows, Unix, or Linux.  The supplied script must be run from a Windows client computer.  You will need to specify a suitable username, password, and database name (from tnsnames.ora) for the strUsername, strPassword, and strDatabase variables in the VBS script.  This script ties into several of the chapters in the Expert Oracle Practices book.

Download the script from here: MonitorDatabaseTimeModel.vbs (save as MonitorDatabaseTimeModel.vbs, without the .doc extension – note that this is version 2.0.1)

## Submit Input to an ASP Web Page and Retrieve the Result using VBS

17 01 2010

January 17, 2010

While it is helpful that SQL statements may be submitted directly from VBS scripts, in most cases the username and password for the database user will be exposed in the VBS script.  So, we need another way.  How about having the VBS script pass a value of interest to an ASP web page (note that this is not ASP.Net, this is old style programming without a Net).  First, we need to create the ASP web page (after, of course, enabling ASP in Microsoft’s IIS web server configuration):

```<html>
<title>I am a Hidden Web Page - You will Not See Me</title>
<body>
<%
Dim strSQL
Dim snpData
Dim comData
Dim strPartID

Dim dbDatabase

On Error Resume Next

strPartID = cStr(Request("strPartID"))

With ComData
strSQL = "SELECT /*+ LEADING(IT) INDEX(IT X_INV_TRANS_1) */" & vbCrLf
strSQL = strSQL & "  IT.PART_ID," & vbCrLf
strSQL = strSQL & "  TRUNC(SUM(DECODE(IT.CLASS,'I',DECODE(IT.TYPE,'O',IT.QTY,0),0))-SUM(DECODE(IT.CLASS,'I',DECODE(IT.TYPE,'I',IT.QTY,0),0))+.9999) AS NEW_ANNUAL_USAGE" & vbCrLf
strSQL = strSQL & "FROM" & vbCrLf
strSQL = strSQL & "  INVENTORY_TRANS IT," & vbCrLf
strSQL = strSQL & "  PART P" & vbCrLf
strSQL = strSQL & "WHERE" & vbCrLf
strSQL = strSQL & "  IT.TRANSACTION_DATE>TRUNC(SYSDATE-365)" & vbCrLf
strSQL = strSQL & "  AND P.ID=IT.PART_ID" & vbCrLf
strSQL = strSQL & "  AND P.ID= ?" & vbCrLf
strSQL = strSQL & "GROUP BY" & vbCrLf
strSQL = strSQL & "  IT.PART_ID" & vbCrLf
'
'Set up the command properties
.CommandText = strSQL
.CommandTimeout = 30

.ActiveConnection = dbDatabase
End With
Set snpData = ComData.Execute

Response.Write "<input type=""text"" name=""txtPartID"" size=30 value=""" & strPartID & """ disabled=true>"
If Not (snpData Is Nothing) Then
If Not(snpData.EOF) Then
Response.Write "<input type=""text"" name=""txtAnnualUsage"" size=30 value=""" & cstr(snpData("new_annual_usage")) & """ disabled=true>"
Response.Write "<input type=""text"" name=""txtOK"" size=255 value=""RETRIEVED"" disabled=true>"
Else
Response.Write "<input type=""text"" name=""txtAnnualUsage"" size=30 value=""0"" disabled=true>"
Response.Write "<input type=""text"" name=""txtOK"" size=255 value=""NO TRANSACTIONS"" disabled=true>"
End If
Else
Response.Write "<input type=""text"" name=""txtAnnualUsage"" size=30 value=""0"" disabled=true>"
Response.Write "<input type=""text"" name=""txtOK"" size=255 value=""ERROR"" disabled=true>"
End If

snpData.Close
dbDatabase.Close

Set snpData = Nothing
Set comData = Nothing
Set dbDatabase = Nothing
%>
</body>
</html>```

OK, reading the ASP web page code, we create an ADO database connection object, an ADO recordset object, and an ADO command object.  Next, we set the strPartID variable to the value of the passed in strPartID variable from the web session, build a SQL statement with a bind variable set to the value of the strPartID variable, and then execute the SQL statement.  If the SQL statement successfully executed, we build two HTML text box, the first with the value of NEW_ANNUAL_USAGE, and the second with a status of either RETRIEVED or NO TRANSACTIONS.  If the SQL statement failed to execute, the two HTML text boxes will contain 0 and ERROR.

Now for the VBS script that will call the ASP web page:

```Dim intResult
Dim objIE
Dim strHTML
Dim strID
Dim ANNUAL_USAGE_QTY

On Error Resume Next

Set objIE = CreateObject("InternetExplorer.Application")

strID = "ABCDEF123456"
ANNUAL_USAGE_QTY = 100

objIE.Navigate "http://localhost/Update_Annual_Usage_Qty.asp?strPartID=" & strID

objIE.Width=100
objIE.Height=100
objIE.Statusbar=False
objIE.Toolbar=False
objIE.Visible = False

Do While objIE.Busy <> False
WScript.Sleep 200
Loop

'loop until the button is clicked
Do While intFlag = 0
If Err <> 0 Then
IntFlag = -1
End If
If objIE is Nothing Then
'User closed ID
intFlag = -1
Else
If objIE.Document.All.txtOK.Value <> " " Then
intFlag = 1
End If
End If
WScript.Sleep 200
Loop

If intFlag = 1 Then
If objIE.Document.Body.All.txtOK.Value = "ERROR" Then
MsgBox "Error sending the query to the database"
Else
If objIE.Document.Body.All.txtOK.Value = "NO TRANSACTIONS" Then
intResult = MsgBox ("No transactions for this part in the last year, OK to set the annual usage qty to 0?  The old value is " & cStr(ANNUAL_USAGE_QTY), vbQuestion + vbYesNo, "Annual Usage")
If intResult = vbYes Then
ANNUAL_USAGE_QTY = 0
End If
Else
'Copy in the values from the web page
intResult = MsgBox ("The old annual usage quantity value is " & cStr(ANNUAL_USAGE_QTY) & " - the database indicates that the updated quantity should be " & cstr(objIE.Document.Body.All.txtAnnualUsage.Value) & ".  Would you like to update the annual usage quantity?", vbQuestion + vbYesNo,"Annual Usage")
If intResult = vbYes Then
ANNUAL_USAGE_QTY = objIE.Document.Body.All.txtAnnualUsage.Value
End If
End If
End If
objIE.Quit
End If

Set objIE = Nothing
Set objShell = Nothing```

The VBS script launches the ASP page in a hidden Internet Explorer window, passing in the value of strID on the address line (this is picked up in the ASP script as the strPartID session variable).  The VBS script then waits until the ASP page finishes loading.  Once the ASP page finishes, the VBS script reads the values of the two HTML text boxes and acts appropriately based on the values of those text boxes.

The neat thing about straight ASP programming code is that it looks a lot like the VBS programming code, and that looks a lot like the Excel macro programming code, and that looks a lot like the classic Visual Basic programming code, and that kind of looks like the classic BASIC programming code that I started working with in 1981/1982.  I have been sitting in on the technology training advisory committee for one of the local colleges.  The committee helps determine what computer classes will be taught to earn a degree at the college.  The question was asked what languages to teach – I heard C++ and Java being suggested… I wonder if I should have suggested Visual Basic?  VBS like languages are also used as macro languages in some ERP products and other packages (I believe that AutoCAD uses a similar macro syntax, as does PC-DMIS).