在PHP中运行Microsoft.SQL文件

在PHP中运行Microsoft.SQL文件,php,sql-server,Php,Sql Server,如何使用php运行MSSQL.sql文件? 我有一组MS SQL文件,如下所示,过去与ToadSQL一起用于检索数据: -- RUN AS SCRIPT SO THAT MONDAY IS FIRST DAY OF THE WEEK set datefirst 1; select top 10 artist_name, album_name, sum(units) total_units, sum(sales) total_sales from ( -- Sales by album sel

如何使用php运行MSSQL.sql文件?

我有一组MS SQL文件,如下所示,过去与ToadSQL一起用于检索数据:

-- RUN AS SCRIPT SO THAT MONDAY IS FIRST DAY OF THE WEEK
set datefirst 1;

select top 10 artist_name, album_name, sum(units) total_units, sum(sales) total_sales from (

-- Sales by album
select c.[name] album_name, d.[name] artist_name, sum(b.price) sales, count(b.Id) units from [order] a, orderalbum b , album c, artist d
where b.albumid=c.id 
and c.ArtistId = d.Id
and a.successful=1 and a.id=b.orderid
and datepart(ww, a.OrderDate) = datepart(ww, getDate())-1
and datepart(yyyy, a.OrderDate) = datepart(yyyy, getDate())
group by c.[name], d.[name]

union

-- Sales by album variant
select d.[name] album_name, e.[name] artist_name, sum(b.price) sales, count(b.Id) units from [order] a, orderalbum b , albumvariant c, album d, artist e
where d.id=c.albumid and  b.albumvariantid=c.id   
and d.ArtistId = e.Id
and a.successful=1 and a.id=b.orderid
and datepart(ww, a.OrderDate) = datepart(ww, getDate())-1
and datepart(yyyy, a.OrderDate) = datepart(yyyy, getDate())
group by d.[Name], e.[Name]

) x
group by x.album_name, x.artist_name
having sum(x.sales) > 0
order by total_units desc; 
我现在正在中构建一个应用程序来自动化这个过程,但我不想用查询重新发明轮子。如何使用php运行MSSQL.sql文件

由于一些查询两个表并使用“union”这一事实,它变得特别复杂。这可能吗

希望有一些SQL主管可以提供帮助


Ben

那么您的目标是创建一个php文件,该文件基于输入文件运行查询,然后将数据输出为html


这不就是在php中将文件读入字符串,然后通过ODBC或类似的方式执行吗?

在CodeIgniter中,我提出了以下建议:

function runsql($file = '')
{

    $this->db_download = $this->load->database('download', TRUE);

    $sql = file_get_contents($file);

    $query = $this->db_download->query($sql);
    $result = $query->result();

    return $result;

}

只需获取文件内容并将其作为查询运行即可。

重要细节缺失,这是适用于Windows还是Unix的PHP?Microsoft提供了MSSQL连接“驱动程序”,但仅适用于Windows,因此每个驱动程序的答案都不同。