KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

MSSQL Stored Procedure #704

Open Andrew Borell opened this topic on on Feb 23, 2019 - 5 comments

Andrew Borell commented on Feb 23, 2019

I cannot get an mssql stored procedure to work. Tried run()->render() and run()->debug() to see if I could even get a hint as to why no data is returning. Execution time is less than a second. The only thing about this stored proc that is a little crazy is it should return 51 columns, but I cannot see that being a problem.

I also tried to configure as PDO, but couldnt get pdo to work with sqlsrv:

Yes, I have the PDO drivers installed and they work in one of my own database classes.

	require_once ('c:\wamp\www\_priv\libs\koolreport\autoload.php');
	
	class ReportAr extends \koolreport\KoolReport {
	
		function settings()
		{
				return array(
				 "assets"=>array(
					"path"=> "c:\wamp\www\dbo\api\assets",
					"url"=>"/api/assets"
					),
				 "dataSources"=>array(
					"mssql"=>array(
						'host' => '172.16.0.40',
						'username' => 'myUser123',
						'password' => 'MyPa$s',
						'dbname' => '007 Test Database',
						'class' => "\koolreport\datasources\SQLSRVDataSource" 
					),
				),
			);
		}

		function setup()
		{
			
			$this->src('mssql')->query(
				"EXEC dbo.getArData	@pdate1 = '2018-01-01',	@pdate2 = '2019-01-01' " )
				->pipe($this->dataStore("report_ar"))->data();
		
		}
	}	
Andrew Borell commented on Feb 23, 2019

Console logs this error, which was not particularly helpful to me in a cursory review because it seems to only indicate that an asset is missing.

Uncaught ReferenceError: KoolReport is not defined
    at <anonymous>:2:1
    at DOMEval (jquery-3.3.1.js:111)
    at domManip (jquery-3.3.1.js:5762)
    at jQuery.fn.init.append (jquery-3.3.1.js:5898)
    at jQuery.fn.init.<anonymous> (jquery-3.3.1.js:5992)
    at access (jquery-3.3.1.js:3939)
    at jQuery.fn.init.html (jquery-3.3.1.js:5959)
    at setContent ((index):323)
    at Object.success ((index):299)
    at fire (jquery-3.3.1.js:3268)

That error references this:

KoolReport.widget.init({"js":["\/api\/assets\/4672722620\/jquery.min.js",["\/api\/assets\/18025843270\/table.js"]],"css":["\/api\/assets\/18025843270\/table.css"]},function(){
    ktable5c719ec0acb601 = new KoolReport.koolphp.table('ktable5c719ec0acb601',{"cKeys":[],"removeDuplicate":[],"paging":null});
        });

This is only happening when I execute a stored procedure. Selects are working as expected.

Andrew Borell commented on Feb 23, 2019

changed exec to implicit parameters and I saw this error:

Cannot read property 'aDataSort' of undefined

I know it must be returning some data because if I configure the date range where I expect no results, I get this error:

TypeError: Cannot read property 'parentNode' of null
Andrew Borell commented on Feb 23, 2019

I now have PDO working but encounter the same problem. Profiled MSSQL server. I can see the query hitting the database and returning the data.

declare @p1 int
set @p1=NULL
exec sp_prepexec @p1 output,N'@P1 nvarchar(10),@P2 nvarchar(10)',N'EXEC getArData @P1, @P2 ',N'2019-01-01',N'2019-01-05'
select @p1

When I var_dump() the datastore I see no rows.

C:\wamp\www\_priv\reports\ReportAr.view.php:4:
object(koolreport\core\DataStore)[16]
  protected 'rows' => 
    array (size=0)
      empty

I execute the same profiled code in SSMS where I plainly see the result set. So why would the data be missing in the datastore? Maybe too many columns? Maybe some character in the data? Spaces in a few column names that I should alias?

Andrew Borell commented on Feb 24, 2019

Tested a few things out and solved the problem. I hope this saves someone else a ton of time.

I dont think any rational person under most circumstances would write a stored procedure without a requirement to execute more than 1 query in the procedure. When you execute more than 1 statement there is more than 1 count performed on the rows. This is a problem; One that is easily corrected with SET NOCOUNT ON . I would also encourage using SET ANSI_WARNINGS ON at that rate, and SET ARITHABORT ON for performance reasons because SSMS enables these by default and you might experience a sharp performance decline without explicitly setting these options and spend a day trying to figure that one out too.

e.g.)

USE [my_ar_database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[getArData] 

	@pdate1 char(10),
	@pdate2 char(10)
AS
	SET ARITHABORT ON;
	SET ANSI_WARNINGS ON;
	SET NOCOUNT ON;  
	
	BEGIN 
		select * into #tmpArData from [some_numbers_table] x where CAST(x.my_date as DATE) >= CAST(@pdate1 as DATE) and CAST(x.my_date as DATE) < CAST(@pdate2 as DATE);
		select * from #tmpArData y where CAST(isnull(y.balance,0) as DECIMAL(19,2)) > CAST(0 as DECIMAL(19,2)) ;
	END 

The most important part of this post as it relates to the original issue is SET NOCOUNT ON. If you have more than 1 statement in your mssql stored procedure and do not explicitly use this option in your procedure, you will get no data back with PDO. If you use ODBC I dont think this would be a problem, but I am not gonna spend any time testing something I dont intend to use.

KoolReport commented on Feb 24, 2019

Oh great! Thank you very much, Andrew! This will help anyone who want to use stored procedures.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

None