SQLDataReader call not returning any records (works if using SQLCommand.ExecuteNonQuery)

mtlmonk
Member Posts: 48
Hello,
I've got a SQL Stored Procedure that is called using one input parameter (sales order no.) and two output parameters (one text and one decimal field).
If I call the SQLCommand.ExecuteNonQuery and retrive the 2 output parameters, it works. However when I translate this to a SQLCommand.ExecuteReader call, the SQLDataReader returned has no records, no fields. It will not return any records, so not entering the loop.
Here's the code:
SQLCommand := SQLCommand.SqlCommand();
SQLCommand.CommandType := SQLCommand.CommandType.StoredProcedure;
// Create SQLConnection Object
SQLConnection := SQLConnection.SqlConnection('Server='+'serverxyz'+';'
+ 'Database='+'dbxyz'+';'
+ 'Integrated Security=True');
// Open SQL Connection
SQLConnection.Open;
SQLCommand.CommandText('CallSPFromNAV'); // Pass Procedure name in CommandText Parameter
SQLCommand.Connection := SQLConnection;
// Supply First Parameter: @CustomerNo
// Initailize SQL Parameter Object
SqlParamCycleCode := SqlParamCycleCode.SqlParameter();
// Parameter for Procedure
SqlParamCycleCode.ParameterName := '@CustomerNo';
// Datatype for parameter
SqlParamCycleCode.DbType := SQLDbType.String;
// Set direction to Input for input parameters
SqlParamCycleCode.Direction := SqlParamCycleCode.Direction.Input;
// Value that supplied to parameter
SqlParamCycleCode.Value := 'customerxyz';
// Provide size to passed parameter
SqlParamCycleCode.Size := 250;
// Add SQL Parameter Object to SQLCommand object
SQLCommand.Parameters.Add(SqlParamCycleCode);
// Supply 2nd Parameter: @ResourceNo
SqlParamCycleCode := SqlParamCycleCode.SqlParameter();
SqlParamCycleCode.ParameterName := '@ResourceNo';
SqlParamCycleCode.DbType := SQLDbType.String;
// Set direction to Output for output parameters
SqlParamCycleCode.Direction := SqlParamCycleCode.Direction.Output;
// No need to provide value in case on output parameter [But when direction is both type InputOutput then pass value]
SqlParamCycleCode.Value := '';
// When you want use the result from the SP then make sure to set the Size otherwise you will only get few characters in response
SqlParamCycleCode.Size := 250;
SQLCommand.Parameters.Add(SqlParamCycleCode);
// Supply 3rd Parameter: @Amount
SqlParamCycleCode := SqlParamCycleCode.SqlParameter();
SqlParamCycleCode.ParameterName := '@Amount';
SqlParamCycleCode.DbType := SQLDbType.Decimal;
// Set direction to Output for output parameters
SqlParamCycleCode.Direction := SqlParamCycleCode.Direction.Output;
// No need to provide value in case on output parameter [But when direction is both type InputOutput then pass value]
SqlParamCycleCode.Value := '';
// When you want use the result from the SP then make sure to set the Size otherwise you will only get few characters in response
SqlParamCycleCode.Size := 250;
SQLCommand.Parameters.Add(SqlParamCycleCode);
// To execute the procedure
{**********************************THIS DOES NOT WORK****************************************
SQLDataReader := SQLCommand.ExecuteReader;
WHILE(SQLDataReader.Read) DO
BEGIN
message('enter read');
//Get Resource No.
ResourceCode := FORMAT(SQLDataReader.GetString(0));
//Get Amount
Amount := SQLDataReader.GetDecimal(1);
END;
*****************************THIS DOES NOT WORK}
{ ***************************THIS WORKS (I made sure to only do a Select top 1*************************************************
SQLCommand.ExecuteNonQuery; //This only retrieves one record because I put Select top 1 in the SP
resourcecode := format(SQLCommand.Parameters.Item('@ResourceNo').Value);
evaluate(amount, format(SQLCommand.Parameters.Item('@Amount').Value));
***************************THIS WORKS*************************************************}
MESSAGE('Returned Resource: %1, %2',ResourceCode, FORMAT(Amount));
// Close SQL Connection
SQLConnection.Close;
// Clear SQLCommand Object
CLEAR(SQLCommand);
// Clear SQLConnection Object
CLEAR(SQLConnection);
********************************************
Here's the Stored Procedure:
*******************************************
ALTER PROCEDURE [dbo].[CallSPFromNAV]
-- Add the parameters for the stored procedure here
@CustomerNo varchar(20),
@ResourceNo varchar(20) output,
@Amount decimal output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
--SELECT top 1 @ResourceNo = [Resource_no], @Amount = [companyxyz$Sales Header].[Prepayment _] <---- this line is for ExecuteNonQuery call
SELECT @ResourceNo = [Resource_no], @Amount = [companyxyz$Sales Header].[Prepayment _]
from [companyxyz$Sales Header]
where [Sell-to Customer No_] = @CustomerNo
return
END
I've got a SQL Stored Procedure that is called using one input parameter (sales order no.) and two output parameters (one text and one decimal field).
If I call the SQLCommand.ExecuteNonQuery and retrive the 2 output parameters, it works. However when I translate this to a SQLCommand.ExecuteReader call, the SQLDataReader returned has no records, no fields. It will not return any records, so not entering the loop.
Here's the code:
SQLCommand := SQLCommand.SqlCommand();
SQLCommand.CommandType := SQLCommand.CommandType.StoredProcedure;
// Create SQLConnection Object
SQLConnection := SQLConnection.SqlConnection('Server='+'serverxyz'+';'
+ 'Database='+'dbxyz'+';'
+ 'Integrated Security=True');
// Open SQL Connection
SQLConnection.Open;
SQLCommand.CommandText('CallSPFromNAV'); // Pass Procedure name in CommandText Parameter
SQLCommand.Connection := SQLConnection;
// Supply First Parameter: @CustomerNo
// Initailize SQL Parameter Object
SqlParamCycleCode := SqlParamCycleCode.SqlParameter();
// Parameter for Procedure
SqlParamCycleCode.ParameterName := '@CustomerNo';
// Datatype for parameter
SqlParamCycleCode.DbType := SQLDbType.String;
// Set direction to Input for input parameters
SqlParamCycleCode.Direction := SqlParamCycleCode.Direction.Input;
// Value that supplied to parameter
SqlParamCycleCode.Value := 'customerxyz';
// Provide size to passed parameter
SqlParamCycleCode.Size := 250;
// Add SQL Parameter Object to SQLCommand object
SQLCommand.Parameters.Add(SqlParamCycleCode);
// Supply 2nd Parameter: @ResourceNo
SqlParamCycleCode := SqlParamCycleCode.SqlParameter();
SqlParamCycleCode.ParameterName := '@ResourceNo';
SqlParamCycleCode.DbType := SQLDbType.String;
// Set direction to Output for output parameters
SqlParamCycleCode.Direction := SqlParamCycleCode.Direction.Output;
// No need to provide value in case on output parameter [But when direction is both type InputOutput then pass value]
SqlParamCycleCode.Value := '';
// When you want use the result from the SP then make sure to set the Size otherwise you will only get few characters in response
SqlParamCycleCode.Size := 250;
SQLCommand.Parameters.Add(SqlParamCycleCode);
// Supply 3rd Parameter: @Amount
SqlParamCycleCode := SqlParamCycleCode.SqlParameter();
SqlParamCycleCode.ParameterName := '@Amount';
SqlParamCycleCode.DbType := SQLDbType.Decimal;
// Set direction to Output for output parameters
SqlParamCycleCode.Direction := SqlParamCycleCode.Direction.Output;
// No need to provide value in case on output parameter [But when direction is both type InputOutput then pass value]
SqlParamCycleCode.Value := '';
// When you want use the result from the SP then make sure to set the Size otherwise you will only get few characters in response
SqlParamCycleCode.Size := 250;
SQLCommand.Parameters.Add(SqlParamCycleCode);
// To execute the procedure
{**********************************THIS DOES NOT WORK****************************************
SQLDataReader := SQLCommand.ExecuteReader;
WHILE(SQLDataReader.Read) DO
BEGIN
message('enter read');
//Get Resource No.
ResourceCode := FORMAT(SQLDataReader.GetString(0));
//Get Amount
Amount := SQLDataReader.GetDecimal(1);
END;
*****************************THIS DOES NOT WORK}
{ ***************************THIS WORKS (I made sure to only do a Select top 1*************************************************
SQLCommand.ExecuteNonQuery; //This only retrieves one record because I put Select top 1 in the SP
resourcecode := format(SQLCommand.Parameters.Item('@ResourceNo').Value);
evaluate(amount, format(SQLCommand.Parameters.Item('@Amount').Value));
***************************THIS WORKS*************************************************}
MESSAGE('Returned Resource: %1, %2',ResourceCode, FORMAT(Amount));
// Close SQL Connection
SQLConnection.Close;
// Clear SQLCommand Object
CLEAR(SQLCommand);
// Clear SQLConnection Object
CLEAR(SQLConnection);
********************************************
Here's the Stored Procedure:
*******************************************
ALTER PROCEDURE [dbo].[CallSPFromNAV]
-- Add the parameters for the stored procedure here
@CustomerNo varchar(20),
@ResourceNo varchar(20) output,
@Amount decimal output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
--SELECT top 1 @ResourceNo = [Resource_no], @Amount = [companyxyz$Sales Header].[Prepayment _] <---- this line is for ExecuteNonQuery call
SELECT @ResourceNo = [Resource_no], @Amount = [companyxyz$Sales Header].[Prepayment _]
from [companyxyz$Sales Header]
where [Sell-to Customer No_] = @CustomerNo
return
END
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions