getting incomplete result executing mssql stored procedure in codeigniter - sql-server

this gives accurate result in mssql server
execute proc_Attn_Mon_General 801, '2018-04-14', '2018-05-14', 0
but this gives incomplete result
$query = $this->db->query("execute proc_Attn_Mon_General #emp_id='$emp_id', #date_from='$start_date', #date_to='$end_date', #Aflag=0");
return $query->result_array();
or this
$query = $this->db->query("execute proc_Attn_Mon_General ".$emp_id.",'".$start_date."','".$end_date."', ".$Aflag." ");
return $query->result_array();
am i doing something wrong?

the query works fine but this could be issue of your slow network connection

Related

dbHasCompleted always returns TRUE

I'm using R to do a statistical analysis on a SQL Server 2008 R2 database. My database client (aka driver) is JDBC and thereby I'm using RJDBC package.
My query is pretty simple and I'm sure that query would return a lot of rows (about 2 million rows).
SELECT * FROM [maindb].[dbo].[users]
My R script is as follows.
library(RJDBC);
javaPackageName <- "com.microsoft.sqlserver.jdbc.SQLServerDriver";
clientJarFile <- "/home/abforce/mystuff/sqljdbc_3.0/enu/sqljdbc4.jar";
driver <- JDBC(javaPackageName, clientJarFile);
conn <- dbConnect(driver, "jdbc:sqlserver://192.168.56.101", "username", "password");
query <- "SELECT * FROM [maindb].[dbo].[users]";
result <- dbSendQuery(conn, query);
dbHasCompleted(result)
In the codes above, the last line always returns TRUE. What could be wrong here?
The fact of function dbHasCompleted always returning TRUE seems to be a known issue as I've found other places in the Internet where people were struggling with this issue.
So, I came with a workaround. Instead of function dbHasCompleted, we can use conditional statement nrow(result) == 0.
For example:
result <- dbSendQuery(conn, query);
repeat {
chunk <- dbFetch(result, n = 10);
if(nrow(chunk) == 0){
break;
}
# Do something with 'chunk';
}
dbClearResult(result);

"Cursor type changed" error on Perl OLE32 MSSQL dateadd function results

The following sql "select DATEADD(day, DATEDIFF(day, 2, GETDATE()), '20:00:00') as A" runs perfectly fine in Microsoft sql query.
However in perl, it complains with the following error "Description: [Microsoft][ODBC SQL Server Driver]Cursor type changed".
I double checked and my code can run select and update statements with no issues so I am a bit stumbled about the cursor error.
Note that I have not included my connection string but have illustrated that I am using win32::OLE.
use Win32::OLE;
$conn->{ConnectionString} = "...";
$conn->open; # open connection to the DB
$state = $conn ->state; #1 means connected
if($state ne "1"){...
$mssql_select = "select DATEADD(day, DATEDIFF(day, 2, GETDATE()), '20:00:00')
as A";
$rs->Open( $mssql_select, $conn);
my $error_collection = $conn->Errors();
my $ecount = $error_collection->Count;
my ($is_message, $real_error_found);
foreach my $error (in $error_collection)
{
#output error statements
$is_message = ($error->{SQLState} eq "01000" && $error->{NativeError}==0);
$real_error_found=1 unless $is_message;
$status = "ERROR # " . $error->{Number}
. "\n Description: " . $error->{Description}
. "\nSource: " . $error->{Source} . "\n";
}
Results in "Description: [Microsoft][ODBC SQL Server Driver]Cursor type changed".
Any brainstormnig ideas the group can provide would be appreciated.
I figured it out, see below
Solved it, turns out this is just a warning, sql server does not know what type of cursor will be returned, the results actually get returned. See http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e15141e7-3084-487d-a60f-47afac046a55/odbcsql-cursor-error

Multiple result sets and return value handling while calling sql server stored procedure from Groovy

I have MS SQL Server stored procedure (SQL Server 2012) which returns:
return value describing procedure execution result in general (successfull or not) with return #RetCode statement (RetCode is int type)
one result set (several records with 5 fields each)
another result set (several records with 3 fields each)
I calling this procedure from my Groovy (and Java) code using Java's CallableStatement object and I cannot find right way to handle all three outputs.
My last attempt is
CallableStatement proc = connection.prepareCall("{ ? = call Procedure_Name($param_1, $param_2)}")
proc.registerOutParameter(1, Types.INTEGER)
boolean result = proc.execute()
int returnValue = proc.getInt(1)
println(returnValue)
while(result){
ResultSet rs = proc.getResultSet()
println("rs")
result = proc.getMoreResults()
}
And now I get exception:
Output parameters have not yet been processed. Call getMoreResults()
I tried several approaches for some hours but didn't find correct one. Some others produced another exceptions.
Could you please help me with the issue?
Thanks In Advance!
Update (for Tim):
I see rc while I launched code:
Connection connection = dbGetter().connection
CallableStatement proc = connection.prepareCall("{ ? = call Procedure_Name($param_1, $param_2)}")
boolean result = proc.execute()
while(result){
ResultSet rs = proc.getResultSet()
println(rs)
result = proc.getMoreResults()
}
I see rc as object: net.sourceforge.jtds.jdbc.JtdsResultSet#1937bc8

Joomla DB Object with where and Now()

can someone help me with this? I want to query some data for my joomla 2.5
template from the db.
The known mysql syntax
$result = mysql_query("SELECT * FROM `jos_fieldsattach_values` WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY");
works as expexted but if I try to write this in the joomla db syntax the query fails
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$db->setQuery($query);
$query
->select(array('*'))
->from('#__fieldsattach_values');
->where('WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY');
$result = $db->loadObjectList();
print_r($result);
It seems somethings wrong with the where clause ?
Thankful for you answers,
tony
First of all. When you develop anything with Joomla you should turn on FULL Error reporting (Configuration/Server/Error Reporting/Development) then set system debugging (Configuration/System/Debug System/Yes). It will show all errors, those from system and those from php/mysql. But remember make sure website is not visible for other users to. Now, your code should look more like this:
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select("*")->from('#__fieldsattach_values')->where('value > NOW() AND value < DATE_ADD(NOW(),INTERVAL 5 DAY)');
$db->setQuery($query);
$result = $db->loadObjectList();
print_r($result);

Codeigniter - pass single query result to controller with json - if exists

Wanting to pass a single codeigniter query result to my controller using json.
my model function is:
function get_linked_loads_qty($q){
$sql = $this->db->query("
IF EXISTS(
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
)
begin
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
END
ELSE BEGIN
select 1 as qtylinkedorders
from customer_Order_lines
where CustomerOrderID='$q'
group by CustomerOrderID
END
");
$query = $this->db->get();
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row[qtylinkedorders])); //build an array
}
return $row_set;
}
}
and my controller is:
function get_linked_loads_qty(){
$this->load->model('Sales_model');
if (isset($_POST['data'])){
$q = strtolower($_POST['data']);
$data = $this->Sales_model->get_linked_loads_qty($q);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
}
The sql works 100% in mssql. the codeigniter error is:
<p>Error Number: 42000</p><p>[Microsoft][SQL Server Native Client 11.0][SQL Server]Must specify table to select from.</p><p>SELECT *</p>
as mentioned the query executes perfectly in mssql.
any assistance welcome. Thanks.
I believe the $this->db->get() is not needed, and is what causes the error. The $this->db->query("blah...") will run the query and return the result. See CodeIgniter DB Query Docs

Resources