Host Variables
HP NonStop SQL/MP Programming Manual for C—429847-008
2-16
Date-Time and INTERVAL Data Types
Example—Creating DATETIME and INTERVAL Data Types
Example 2-1. Creating Valid DATETIME and INTERVAL Data Types
#include <stdio.h>
#include <string.h>
#include <sql.h>
#define STMT_LEN 256
EXEC SQL BEGIN DECLARE SECTION;
short sqlcode;
char hv_projdesc[30];
char hv_start_date[11];
char in_start_date[11];
char curspec[STMT_LEN];
EXEC SQL END DECLARE SECTION;
int main()
{
int len;
strcpy(curspec,
"SELECT projdesc, CAST(start_date AS CHAR(10)) FROM test1 "
"WHERE start_date <= CAST(CAST( ? AS CHAR(10)) "
"AS DATE) BROWSE ACCESS");
len = strlen(curspec);
memset(&curspec[len], ' ', STMT_LEN - len);
EXEC SQL PREPARE cursor_spec from :curspec;
/* Declare the dynamic cursor from the prepared statement. */
EXEC SQL DECLARE get_proj CURSOR FOR cursor_spec;
/* Initialize the parameter in the WHERE clause. */
printf("Enter the most recent start date in the form yyyy-mm-dd: ");
scanf("%s", in_start_date);
/* Open the cursor using the value of the dynamic parameter. */
EXEC SQL OPEN get_proj USING :in_start_date;
/* Fetch the first row of the result table. */
EXEC SQL FETCH get_proj INTO :hv_projdesc,:hv_start_date;
while (sqlcode == 0)
{
hv_start_date[10]='\0';
printf("\n Start Date: %s", hv_start_date);
/* Fetch the next row of the result table. */
EXEC SQL FETCH get_proj INTO :hv_projdesc,:hv_start_date;
}
/* Close the cursor. */
EXEC SQL CLOSE get_proj;
return 0;
}