Squirrel SQL Editor does not return multiple results from a stored procedure with 3 select statements - sybase

I have a stored procedure which is run in Squirrel SQL editor for Sybase. The stored procedure has 3 select statements, but when executed , it returns only Ithe result of first select statement.
What I have tried: Executed the stored procedure using Java SQL API. I see the same behavior. Also, executed the same stored procedure in Toad for Sybase and see the same behavior.
Our company uses an 3rd party database execution tool for legacy C++ apps. That library returns all the 3 result sets as expected. We are removing this library for some reason.

Use CallableStatement#execute() to execute the stored procedure which returns multiple ResultSets.
It returns a boolean.
If true : If current result is a ResultSet
If false : The current result is a UpdateCount
What is a UpdateCount?
When you execute a stored procedure in SQL Editor, you will get some information in Console saying, for example, "4 rows affected". This message is called an UpdateCount.
Database returns these messages as well as the resultsets in the response to API.
Traverse through the Results returned.
boolean hasResults = stmt.execute();
while(true) {
if(hasResults) {
//Has results is true if Current result is ResultSet
//Add code here to parse your result values
} else {
if(stmt.getUpdateCount() == -1) break; // you have reached the end of all data (updatecount + resultset)
}
hasResults = stmt.getMoreResults(); // GetMoreResults return true if the next result is ResultSet else it returns false if its updatecount.
}
If you don't want the UpdateCounts to be returned as part of results from database, you include the statement SET NOCOUNT ON in your stored procedures

Related

SQL Server procedure that did not complete when it executed from R

When I ran a procedure from R, it stops in the middle of the execution. But if I ran it directly from SQL Server, it completes the execution.
Here is the code (there is not a lot to show):
connection<-odbcDriverConnect("SERVER=server_name;DRIVER={SQL Server};DATABASE=DB;UID=RUser;PWD=****")
stringEXEC<-"EXEC [dbo].[LongProcedure]"
data<-sqlQuery(channel = connection,query = stringEXEC,errors = TRUE)
Some remarks:
the procedure is calling for 12 another procedures. and each of the 12 creating a specific table (it's very long query to print it here in the question)
And there is no error.
Why is this happening?
I ran into a similar issue. Currently, you can only execute SELECT statement from R, not stored procedures.
If you prefer working in R-Studio, I suggest executing the results of your stored procedure into a table in SQL Server first, then using that table in R. You'll still get the benefit of scalability with that compute context.
Passing T-SQL select statement to sp_execute_external_script

SSIS Execute SQL Task Stored Procedure returning empty result sets

I have a simple stored procedure that returns 1 row with 3 columns. I am trying to create an SSIS package that reuses the values in these columns later. If I run a simple select query everything is happy but when I run the stored procedure I get the following error:
[Execute SQL Task] Error: Executing the query "rs_UpdateMemberExtract"
failed with the following error: "Unable to populate result columns
for single row result type. The query returned an empty result set.".
Possible failure reasons: Problems with the query, "ResultSet"
property not set correctly, parameters not set correctly, or
connection not established correctly.
I've set up an ado.net connection and the proc runs correctly in SSMS, I have the task set up like follows:
ConnectionType: ADO.NET
Connection: ServerName.connectionName
SQLSourceType: Direct Input
SQLstatment: Name of Proc (rs_UpdateMemberExtract)
IsQueryStoredProcedure: True
I have each parameter mapped to a variable and the direction set to Output
I have the name of each column that should be returned in the result set and mapped to the associated variable.
The task works if I set the ResultSet to 'None',
Any ideas What I've missed?
Thanks
If an SQL Task Editor expects results returned in a row … but there are no records to return then the following SQL fills a null return with a space. Note : can also be used to return zero if a numeric “return row” is expected
SELECT '' + ISNULL ((SELECT x FROM y WHERE (z = ?)), '') AS myfield

Very strange SQL Server query behaviour

I have a very strange issue with a SQL query.
IF NOT EXISTS ([special query here])
BEGIN
SELECT 1;
END
ELSE
BEGIN
SELECT 2;
END
The query above outputs: 2.
However when I replace the SELECT 1; part with a large query containing create tables etc. multiple errors are thrown. How is it possible that SQL Server executes code inside the case of an IF statement while that case is not true?
If you are changing schema, the parser will look to see that all the entities exist before running it.
ALTER TABLE myTable ADD aNewColumn INT
UPDATE myTable SET aNewColumn = 0
This will produce an error.
You can use dynamic sql, as long as you aren't taking in parameters from a client.
EXEC sp_executesql N'UPDATE myTable SET aNewColumn = 0'
Syntax errors are syntax errors, whether you're in a conditional branch that'll run or a conditional branch that won't run. Parsing occurs before execution, and must be successful.
Consider this analogous example written in C++:
int main()
{
if (false) {
acbukasygdfukasygdaskuygdfas##4r9837y214r
}
}
You can't write that nonsense line even though it's inside of a block that'll never run, because the program's intended meaning cannot be determined by the compiler.

SSIS SQL Task: strange value for stored procedure output parameter with default value

Here's a simplified version of my sproc:
CREATE PROCEDURE usp_AddSomething
#i_input1 int,
#o_output1 int = 1 OUTPUT,
#o_output2 int = 2 OUTPUT
AS
-- Do stuff
If I don't explicitly set values for #o_output1 or #o_output2 within the sproc then I expect to see the default output values of 1 and 2. It works this way when I call EXEC usp_AddSomething ... from a normal SQL script. However, when I run the same statement in an SSIS SQL Task like so:
EXEC ? = usp_AddSomething
#i_input1 = ?,
#o_output1 = ? OUTPUT,
#o_output2 = ? OUTPUT
I get seemingly random values (e.g. 15305391 and 69085360) instead of the default 1 and 2.
But if the values are explicitly set somewhere in the sproc like so:
...
SELECT #o_output1 = 1
SELECT #o_output2 = 2
RETURN 0
Then I get the expected values of 1 and 2 in SSIS.
My SSIS vars are Int32, the Parameter Mapping on the SQL task uses LONG and the ordinal positions (i.e. 0, 1, 2) are setup correctly under Parameter Name.
It's easy enough to redundantly initialize the values of the output parameters to match their defaults, but I'd really like to know WHY it's not working. Maybe another quirky SSIS thing? SQL Server 2005 SP4.
A good way to see what's going on is to start a trace with profiler and see the t-sql that ssis is sending to the database engine. Then try executing that from ssms and see if you get the same results.

Stored Procedure Syntax

My stored procedure is called as below from an SQL instegartion package within SQL Server 2005
EXEC ? = Validation.PopulateFaultsFileDetails ? , 0
Though i'm not sure what the ? means
When this SQL statment is called, both question marks (?) will be replaced. The first will be replaced by a variable which will receive the return value of the stored procedure. The second will be replaced by a value which will be passed into the stored procedure. The code to use this statement will look something like this (pseudocode):
dim result
SQL = "EXEC ? = Validation.PopulateFaultsFileDetails ? , 0"
SQL.execute(result, 99) // pass in 99 to the stored proc
debug.print result
This gives you 3 advantages:
you can re-use the same bit of SQL with different values
you can pick up the return value and test for success/error
if the value you are passing in is a string, it should be correctly escaped for you, reducing the risk of SQL injection vulnerabilities in your app.
The ? stands fora variable, to be precise, a parameter. The first ? is the return value of the stored prcoedure and the second one is the first parameter of the stored procedure
Thanks I appreciate the answer.
I was able to successfully execute the stored procedure using
DECLARE #FaultsFileName varchar
DECLARE #FaultsFileID int
EXEC #FaultsFileID = Validation.PopulateFaultsFileDetails 'SameMonth Test.txt' , #FaultsFileID
SELECT #FaultsFileID
But when I pass the input parameter as 'SameMonth Test.txt' in the Integration Package I get an error which says:
Parameter names cannot be a mixture of ordinal and named types.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

Resources