런타임 시 생성된 SQL 문

임시 분석을 수행하는 애플리케이션은 일반적으로 런타임에 SQL 문을 빌드합니다. 예를 들어 스프레드시트를 사용하면 사용자가 데이터를 검색할 열을 선택할 수 있습니다.

// 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);  
}  

런타임에 SQL 문을 일반적으로 생성하는 또 다른 애플리케이션 클래스는 애플리케이션 개발 환경입니다. 그러나 생성한 문은 빌드하는 애플리케이션에서 하드 코딩되며, 일반적으로 최적화 및 테스트할 수 있습니다.

런타임에 SQL 문을 생성하는 애플리케이션은 사용자에게 엄청난 유연성을 제공할 수 있습니다. WHERE 절, ORDER BY또는 조인과 같은 일반적인 작업을 지원하지 않았던 이전 예제에서 볼 수 있듯이 런타임에 SQL 문을 생성하는 것은 하드 코딩 문보다 훨씬 더 복잡합니다. 또한 이러한 애플리케이션을 테스트하는 것은 임의의 수의 SQL 문을 생성할 수 있기 때문에 문제가 됩니다.

런타임에 SQL 문을 생성할 때 발생할 수 있는 단점은 하드 코딩된 문을 사용하는 것보다 문을 생성하는 데 훨씬 더 많은 시간이 걸린다는 것입니다. 다행히도, 이것은 거의 문제가되지 않습니다. 이러한 애플리케이션은 사용자 인터페이스를 많이 사용하는 경향이 있으며, 애플리케이션이 SQL 문을 생성하는 데 소요되는 시간은 일반적으로 사용자가 조건을 입력하는 데 소요되는 시간에 비해 작습니다.