[英]PHP mssql_query delete, create and select from #temp table
我在尝试着:
我在第57行收到“解析错误: C:\\xampp\\htdocs\\test.php
解析错误。
任何帮助将不胜感激!
简短信息:
下面的PHP代码:
$query = "IF OBJECT_ID('tempdb..#temp') IS NOT NULL DROP TABLE #temp"
$result = mssql_query($query);
$query = "
SELECT *
INTO #temp
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY key
ORDER BY moment desc) AS Seq, *
FROM reportstable) t
WHERE Seq = 1
and moment between '$_GET[od] $time' and '$_GET[do] $time1'"
$result = mssql_query($query);
$query = "select * from #temp"
$result = mssql_query($query);
提前致谢!
由于多种原因,这将无法正常工作。
mssql_query()
之后, #temp
mssql_query()
都会消失。 如果您需要一个临时表在多个上下文中持久保存,则它必须是全局临时表- ##temp
。 $query
,并确保您的日期时间格式正确。 综上所述,假设您已修复SQL注入漏洞,则在此处使用临时表毫无意义且浪费资源。 以下内容就足够了:
$query = "
SELECT *
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY key
ORDER BY moment desc) AS Seq, *
FROM reportstable) t
WHERE Seq = 1
and moment between '$_GET[od] $time' and '$_GET[do] $time1'"
$result = mssql_query($query);
由于缺少分号,引发了分析错误。 并且##必须始终存在,以便能够查询该表。
// to delete the temp table
$query = "IF OBJECT_ID('tempdb..##temp') IS NOT NULL DROP TABLE ##temp";
$result = mssql_query($query);
// to initialize the temp table
$query = "
SELECT *
INTO ##temp
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY test_key
ORDER BY test_moment desc) AS Seq, *
FROM reports_table) t
WHERE Seq = 1
and test_moment between '$_GET[od] $time' and '$_GET[do] $time1'";
$result = mssql_query($query);
// to query the temp table
select test_key
from ##temp
where x = 0
and y = 1
and z = 3
ps以上内容仅在同一会话中起作用。 如果您从不同的来源在同一时间调用同一页面,则由于尝试删除临时表而将引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.