发生数据库错误错误号:42000/102

时间:2018-01-19 03:29:31

标签: php sql sql-server codeigniter

现在我正在使用php框架CodeIgniter开发一个Web应用程序。

我的网站使用SQL Server 2008数据库连接(sqlsrv)。我想使用这样的SQL查询:

SELECT *
FROM (SELECT *, ROW_NUMBER () OVER (ORDER BY No Progress) as line
      FROM MASTER_IMP
     ) a
WHERE line> = $ limit and line <= $ offset

然后我把查询放在我的CI模型中 当我运行它时,会出现如下错误:

=============================================== ============

发生数据库错误

错误号码:42000/102

[Microsoft] [SQL Server的ODBC驱动程序13] [SQL Server]&#39; =&#39;附近的语法不正确。

SELECT *
FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY NoPengajuan) as baris
      FROM MASTER_IMP
     ) a
WHERE baris >= 10 and baris <=

文件名:C:/xampp/htdocs/ci-indoasia/system/database/DB_driver.php

行号:691

=============================================== ============

这是我的mvc

我的模特:

&#13;
&#13;
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_MASTER_IMP extends CI_Model
{
	function data($limit,$offset){ //$limit yaitu urutan record yg mulai pertama ditampilkan, $offset yaitu jumlah record yg akan ditampilkan
		$kueri = $this->load->database('sqlserver', TRUE);
		return $kueri->query
		("SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY NoPengajuan) as baris FROM MASTER_IMP ) a WHERE baris >= $limit and baris <= $offset")->result();		
	}
 
	function jumlah_data(){
		return $this->db->get('MASTER_IMP')->num_rows();
	}
}	
&#13;
&#13;
&#13;

我的控制员:

&#13;
&#13;
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Server1_IMPORT_STATISTIC extends CI_Controller
{	
	function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url'));
		$this->load->model('server1/IMPORT_STATISTIC/model_MASTER_IMP');
	}
	
	public function MASTER_IMP()
	{
		$this->load->database();
		$jumlah_data = $this->model_MASTER_IMP->jumlah_data();
		$this->load->library('pagination');
		$config['base_url'] = base_url().'server1/IMPORT_STATISTIC/MASTER_IMP';
		$config['total_rows'] = $jumlah_data;
		$config['per_page'] = 10;
		
		$config['first_link']       = 'Pertama';
        $config['last_link']        = 'Terakhir';
        $config['next_link']        = 'Berikut';
        $config['prev_link']        = 'Sebelum';
        $config['full_tag_open']    = '<div class="pagging text-center"><nav><ul class="pagination justify-content-center">';
        $config['full_tag_close']   = '</ul></nav></div>';
        $config['num_tag_open']     = '<li class="page-item"><span class="page-link">';
        $config['num_tag_close']    = '</span></li>';
        $config['cur_tag_open']     = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close']    = '<span class="sr-only">(current)</span></span></li>';
        $config['next_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['next_tagl_close']  = '<span aria-hidden="true">&raquo;</span></span></li>';
        $config['prev_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['prev_tagl_close']  = '</span>Next</li>';
        $config['first_tag_open']   = '<li class="page-item"><span class="page-link">';
        $config['first_tagl_close'] = '</span></li>';
        $config['last_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['last_tagl_close']  = '</span></li>';
		
		$from = $this->uri->segment(3);
		$this->pagination->initialize($config);		
		$data['MASTER_IMP'] = $this->model_MASTER_IMP->data($config['per_page'],$from);
		$this->load->view('server1/IMPORT_STATISTIC/MASTER_IMP', $data);
	}
		//$kueri = $this->load->database('sqlserver', TRUE);
		//var_dump($kueri->query("select top 5 * from MASTER_IMP order by NoPengajuan;")->result_array());
}
&#13;
&#13;
&#13;

我的观点:

&#13;
&#13;
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Indo Asia | Database IMPORT STATISTIC</title>
			<?php include 'application/views/komponen/header.php'; //meta, css, js ?>
	</head>
<body>
	<?php include 'application/views/komponen/navbar.php'; //navbar ?>

<div class="container text-center">    
	<div class="row content">
		<div class="col-sm-12 text-left"> 
			<div class="panel panel-default">
				<div class="panel-heading">
					<h2>Database IMPORT STATISTIC</h2>
				</div>
				<div class="panel-body">
					<div class="table-responsive">
						<table class="table table-hover table-bordered">
							<tr>
								<th>No</th>
								<th>NoPengajuan</th>
								<th>TanggalPIB</th>
								<th>TanggalBLAWB</th>
								<th>PelabuhanAsal</th>
								<th>PelabuhanTujuan</th>
								<th>Importir</th>
								<th>AlamatImportir</th>
								<th>PPJK</th>
								<th>AlamatPPJK</th>
								<th>Pemasok</th>
								<th>AlamatPemasok</th>
								<th>NegaraPemasok</th>
								<th>HS</th>
								<th>UraianBarang</th>
								<th>Quantity</th>
								<th>KodeSatuan</th>
								<th>Berat</th>
								<th>HargaCIF</th>
								<th>MataUang</th>
								<th>NPWP</th>
							</tr>
							<?php 
							$no = $this->uri->segment('3') + 1;
							foreach($MASTER_IMP as $baris){ 
							?>
							<tr style="white-space:nowrap">
								<td align="right"><?php echo number_format($no++, 0, ',', '.') ?></td>
								<td><?php echo $baris->NoPengajuan ?></td>
								<td><?php echo $baris->TanggalPIB ?></td>
								<td><?php echo $baris->TanggalBLAWB ?></td>
								<td><?php echo $baris->PelabuhanAsal ?></td>
								<td><?php echo $baris->PelabuhanTujuan ?></td>
								<td><?php echo $baris->Importir ?></td>
								<td><?php echo $baris->AlamatImportir ?></td>
								<td><?php echo $baris->PPJK ?></td>
								<td><?php echo $baris->AlamatPPJK ?></td>
								<td><?php echo $baris->Pemasok ?></td>
								<td><?php echo $baris->AlamatPemasok ?></td>
								<td><?php echo $baris->NegaraPemasok ?></td>
								<td><?php echo $baris->HS ?></td>
								<td><?php echo $baris->UraianBarang ?></td>
								<td><?php echo $baris->Quantity ?></td>
								<td><?php echo $baris->KodeSatuan ?></td>
								<td><?php echo $baris->Berat ?></td>
								<td><?php echo $baris->HargaCIF ?></td>
								<td><?php echo $baris->MataUang ?></td>
								<td><?php echo $baris->NPWP ?></td>
							</tr>
							<?php } ?>
						</table>
					</div>
					<?php 
					echo $this->db->count_all_results('MASTER_IMP').' record(s) ditampilkan'; 
					echo $this->pagination->create_links();
					?>
				</div>
			</div>
		</div>
	</div>
</div>

	<?php include 'application/views/komponen/footer.php'; //footer ?>
</body>
</html>
&#13;
&#13;
&#13;

查询参考:https://blogs.msdn.microsoft.com/sqlserver/2006/10/25/limit-in-sql-server/

请帮助查询错误在哪里?

谢谢

0 个答案:

没有答案