














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document, authored by david toman from the university of waterloo, provides an introduction to dynamic embedded sql. It covers the goals and problems of dynamic sql, the concept of dynamic sql statements, and the execution of prepared statements using execute and cursor. The document also discusses the use of sql descriptor areas (sqlda) for describing prepared statements and passing parameters. Examples of an adhoc application that executes sql statements provided as arguments.
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!
School of Computer Science University of Waterloo
struct sqlname /* AttributeName (^) / { short length; / Name length [1..30] (^) / char data[30]; / Variable or Column name (^) */ };
struct sqlvar /* Attribute Descriptor (^) / { short sqltype; / Variable data type (^) / short sqllen; / Variable data length (^) */ char (^) SQL_POINTER sqldata; / data buffer (^) */ short (^) SQL_POINTER sqlind; / null indiciator (^) / struct sqlname sqlname; / Variable name (^) */ };
struct sqlda /* Main SQLDA (^) / { char sqldaid[8]; / Eye catcher = ’SQLDA ’ (^) / long sqldabc; / SQLDA size in bytes=16+44*SQLN (^) / short sqln; / Number of SQLVAR elements (^) / short sqld; / Number of used SQLVAR elements (^) / struct sqlvar sqlvar[1]; / first SQLVAR element (^) */ };
int main(int argc, char (^) *argv[]) { int i, isnull; short type; printf("Sample C program : ADHOC interactive SQL\n");
EXEC SQL WHENEVER SQLERROR GO TO error;
EXEC SQL CONNECT TO :db; printf("Connected to DB2\n");
strncpy(sqlstmt,argv[1],1000); printf("Processing <%s>\n",sqlstmt);
EXEC SQL PREPARE stmt FROM :sqlstmt;
init_da(&select,1);
EXEC SQL DESCRIBE stmt INTO :*select;
i= select->sqld;
if (i>0) { printf(" ... looks like a query\n");
/* new SQLDA to hold enough descriptors for answer (^) */ init_da(&select,i);
/* get the names, types, etc... (^) / EXEC SQL DESCRIBE stmt INTO :select;
printf("Number of select variables <%d>\n",select->sqld); for (i=0; i<select->sqld; i++ ) { printf(" variable %d <%.*s (%d%s [%d])>\n", i, select->sqlvar[i].sqlname.length, select->sqlvar[i].sqlname.data, select->sqlvar[i].sqltype, ( (select->sqlvar[i].sqltype&1)==1? "": " not null"), select->sqlvar[i].sqllen); } printf("\n");
EXEC SQL DECLARE cstmt CURSOR FOR stmt; EXEC SQL OPEN cstmt; EXEC SQL WHENEVER NOT FOUND GO TO end; for (;;) { EXEC SQL FETCH cstmt USING DESCRIPTOR :*select; for (i=0; i<select->sqld; i++ ) if ( (^) *(select->sqlvar[i].sqlind) < 0 ) print_var("NULL", select->sqlvar[i].sqltype, select->sqlvar[i].sqlname.length, select->sqlvar[i].sqllen); else print_var(select->sqlvar[i].sqldata, select->sqlvar[i].sqltype, select->sqlvar[i].sqlname.length, select->sqlvar[i].sqllen); printf("\n"); }; end: printf("\n");
} else { printf(" ... looks like an update\n");
EXEC SQL EXECUTE stmt; };
/* and get out of here (^) */ EXEC SQL COMMIT; EXEC SQL CONNECT reset; exit(0);
error: check_error("My error",&sqlca); EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL CONNECT reset; exit(1); }