$sql="";
$sql .= "insert into ".$destination." ("."<br/>";
$sql .= $column_stat.")<br/>";
$sql .= "select ".$column_source;
$sql .= "from ".$tablesource."<br/>";
if ($objResult["condition"] <> ''){
$sql.= " where ".$objResult["condition"];
$sql .= $dfcondition;
$sql .= " and convert(varchar,".$columndate.",23) between "." '".$startdate." ' and '".$enddate." ' "."<br/>";
}
$result = mssql_query($sql) or die ("Error Query [".$sql."]");
echo $sql;
ซึ่งพอทำการ echo มา ก็จะได้คำสั่งดังนี้ค่ะ
Code
insert into Transaction (
dl_datetime,
CONID,
sessionid,
msisdn,
operator,
mformat,
model,
codetype,
code,
typeid,
partner,
mediacode,
channel,
insert_date,
table_source,
dl
)
select convert(varchar,(cast([date] as varchar)+' '+cast([time] as varchar)),120) ,
1,
sessionID,
msisdn,
network,
mformat,
model,
'MOBILE',
code,
type,
partner,
ads_partner,
channel,
getdate(),
'Download',
1
from Download
where partner IN ('1111') and channel not in('WAP') and msisdn NOT IN ('661xxxxxxx', '661xxxyyyy') AND network<>'LOCAL_HOST' and channel <> 'MONITOR' and convert(varchar,date,23) between '2013-07-15 ' and '2013-07-15 '
ถ้าเอาคำสั่งนี้ไปรันมน Sql Server ก็สามารถรันได้นะคะ แต่ที่นี้ต้องการให้เอา sql ที่ได้นี้ไป inset ลงเบสนะคะ ไม่ทราบว่าต้องเขียนโค้ดประมาณไหนค่ะ
<?php
// Connect to MSSQL and select the database
mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');
// Make a query that will fail
$query = @mssql_query('SELECT * FROM [php].[dbo].[not-found]');
if (!$query) {
// The query has failed, print a nice error message
// using mssql_get_last_message()
die('MSSQL error: ' . mssql_get_last_message());
}
?>
Date :
2013-07-17 16:49:51
By :
mr.win
No. 2
Guest
Code (PHP)
$sql = "insert into ".$destination." ("."<br/>";
$sql .= $column_stat.")<br/>";
$sql .= "select ".$column_source;
$sql .= "from ".$tablesource."<br/>";
if ($objResult["condition"] <> ''){
$sql.= " where ".$objResult["condition"];
$sql .= $dfcondition;
$sql .= " and convert(varchar,".$columndate.",23) between "." '".$startdate." ' and '".$enddate." ' "."<br/>";
}
$result = @mssql_query($sql);
if (!$result) {
die('MSSQL error:' . mssql_get_last_message());
}
//echo $sql;