Jump to content

SQL Query is there an easier way


alanl1
Go to solution Solved by mac_gyver,

Recommended Posts

Hi professionals

 

I have some code as follows

 
/*  The queries to be prepared for execution */
/*******************************************************/
$stmt = sqlsrv_query( $conn, $sql);
$stmt1 = sqlsrv_query( $conn, $sql1);
$droptable1 = sqlsrv_query( $conn, $droptable);
$bulkinsert1 = sqlsrv_query( $conn, $bulkinsert);
/*******************************************************/
 
/*  Check the database connection is good */
/*******************************************************/
 if( $conn === false) {
     die( print_r( sqlsrv_errors(), true) );
 }
/*******************************************************/
 
/*  Prepare the drop table query. */
/*******************************************************/
  if(!$droptable1 = sqlsrv_prepare( $conn, $droptable))
  {
    echo "Could not prepare statement";
   die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the drop table query.  */
 if( !sqlsrv_execute( $droptable1))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/*******************************************************/
 
/*  Prepare the Create table statement. */
/*******************************************************/
 if(!$stmt = sqlsrv_prepare( $conn, $sql))
  {
   echo "Could not prepare statement";
   die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the Create table statement. */
 if( !sqlsrv_execute( $stmt))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/*******************************************************/

  
/*  Prepare the BULK insert statement. */
/*******************************************************/
 if(!$bulkinsert1 = sqlsrv_prepare( $conn, $bulkinsert))
  {
    echo "Could not prepare statement";
    die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the BULK insert statement. */
 if( !sqlsrv_execute( $bulkinsert1))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/*******************************************************/
 
/*  Prepare the Cleansing data statement. */
/*******************************************************/
 if(!$stmt1 = sqlsrv_prepare( $conn, $sql1))
  {
    echo "Could not prepare statement";
    die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the Cleansing data statement. */
 if( !sqlsrv_execute( $stmt1))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/******************************************************/
/*  Release the Queries */
/*******************************************************/
sqlsrv_free_stmt( $droptable1);
sqlsrv_free_stmt( $stmt);
sqlsrv_free_stmt( $bulkinsert1);
sqlsrv_free_stmt( $stmt1);
/*******************************************************/
 

is there a better way to prepare, execute and realease them rather than repeating them one by one?

 

 

 

thanks in advance

Edited by alanl1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.