Comparteix a través de


Instrucciones SQL creadas en tiempo de ejecución

Las aplicaciones que realizan análisis ad hoc suelen compilar instrucciones SQL en tiempo de ejecución. Por ejemplo, una hoja de cálculo podría permitir que un usuario seleccione columnas de las que recuperar datos:

// SQL_Statements_Constructed_at_Run_Time.cpp  
#include <windows.h>  
#include <stdio.h>  
#include <sqltypes.h>  
  
int main() {  
   SQLCHAR *Statement = 0, *TableName = 0;  
   SQLCHAR **TableNamesArray, **ColumnNamesArray = 0;  
   BOOL *ColumnSelectedArray = 0;  
   BOOL  CommaNeeded;  
   SQLSMALLINT i = 0, NumColumns = 0;  
  
   // Use SQLTables to build a list of tables (TableNamesArray[]). Let the  
   // user select a table and store the selected table in TableName.  
  
   // Use SQLColumns to build a list of the columns in the selected table  
   // (ColumnNamesArray). Set NumColumns to the number of columns in the  
   // table. Let the user select one or more columns and flag these columns  
   // in ColumnSelectedArray[].  
  
   // Build a SELECT statement from the selected columns.  
   CommaNeeded = FALSE;  
   Statement = (SQLCHAR*)malloc(8);  
   strcpy_s((char*)Statement, 8, "SELECT ");  
  
   for (i = 0 ; i = NumColumns ; i++) {  
      if (ColumnSelectedArray[i]) {  
         if (CommaNeeded)  
            strcat_s((char*)Statement, sizeof(Statement), ",");  
         else  
            CommaNeeded = TRUE;  
         strcat_s((char*)Statement, sizeof(Statement), (char*)ColumnNamesArray[i]);  
      }  
   }  
  
   strcat_s((char*)Statement, 15, " FROM ");  
   // strcat_s((char*)Statement, 100, (char*)TableName);  
  
   // Execute the statement . It will be executed once, do not prepare it.  
   // SQLExecDirect(hstmt, Statement, SQL_NTS);  
}  

Otra clase de aplicaciones que normalmente construye instrucciones SQL en tiempo de ejecución son entornos de desarrollo de aplicaciones. Sin embargo, las instrucciones que construyen están codificadas de forma rígida en la aplicación que están compilando, donde normalmente se pueden optimizar y probar.

Las aplicaciones que construyen instrucciones SQL en tiempo de ejecución pueden proporcionar una gran flexibilidad al usuario. Como se puede ver en el ejemplo anterior, que ni siquiera admitía operaciones comunes como cláusulas WHERE, cláusulas ORDER BY o combinaciones, la construcción de instrucciones SQL en tiempo de ejecución es mucho más compleja que las instrucciones de codificación rígida. Además, probar estas aplicaciones es un problema porque pueden construir un número arbitrario de instrucciones SQL.

Una posible desventaja de la construcción de instrucciones SQL en tiempo de ejecución es que se tarda mucho más tiempo en construir una instrucción que usar una instrucción codificada de forma rígida. Afortunadamente, esto raramente es motivo de preocupación. Estas aplicaciones tienden a ser intensivas en la interfaz de usuario y el tiempo que la aplicación dedica a construir instrucciones SQL suele ser pequeña en comparación con el tiempo que el usuario dedica a escribir criterios.