Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4113706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:21:37+00:00 2026-05-20T22:21:37+00:00

How can I run MSSQL .sql files with php? I have a collection of

  • 0

How can I run MSSQL .sql files with php?

I have a collection of MS SQL files like the following which were used with ToadSQL to retrieve data in the past:

-- 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; 

I’m now building an application in to automate the process but I don’t want to reinvent the wheel with the queries. How can I run MSSQL .sql files with php

It’s made particularly complex by the fact that some query two tables and use ‘union’ is this possible?

Hope there are some SQL heads out there who can help.

Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:21:38+00:00Added an answer on May 20, 2026 at 10:21 pm

    In CodeIgniter I’ve come up with the following:

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

    It’s as simple as getting the file contents and running it as a query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically have many huge delimited files that I know I can import as a
If I run the command-line program mysql like mysql -u someone -p < sql-file
I'm following the documentation almost word for word, except for two adjustments which I
I have a large SQL script that creates my database (multiple tables, triggers, and
I cant figure out how to load from the server the essential initial data
I have some report and plenty of parameters inside that report. One of parameter
In my Ubuntu 10.10, following this tutorial , I don't face step 2. I
Currently I'm working on a on-line webapplication for construction materials. Companies can log in
I keep getting this issue with PDO, where this error crops up in my
What's your advice/points for developers to follow or avoid in the development and early

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.