Executing stored procedure through SAS not working the same as it does in SQL Server - sql

I have a stored procedure that I often execute within SQL Server. Without copying hundreds of lines of code into here, it basically does the following:
You enter a database and table name as parameters.
The procedure then calculates summary statistics on all fields in the table (average, sum etc.) and inserts them into a temporary table.
The summary statistics are then inserted into an existing meta table.
The temporary table of stats is then dropped.
The procedure itself works perfectly when executing through SQL Server.
However, executing the same procedure with the same parameters through SAS code (via WPS) does not return the same results. It kicks off the procedure and calculates the statistics, but then fails to insert the statistics into the existing table and fails to drop the temporary table. Despite this, no errors are returned in the SAS log. The SAS code to execute the procedure is here:
proc sql;
connect to odbcold(required="dsn=&SQLServer; database=_Repository;");
execute (SP_Meta_Stats
#DATABASE = &vintage.,
#TABLE_NAME = &table_name.) by odbcold;
disconnect from odbcold;
quit;
I can assure you the macro variables have been setup correctly because the procedure is kicking off correctly with the correct parameters. It's just not completing the procedure as it does directly in SQL.
Are there are any known limitations to executing SQL stored procedures through a proc sql statement in SAS? Any known reasons why this not be computing the same as it does in SQL, despite just executing the same procedure?
EDIT:
This seems to be some sort of connection issue. As if after a certain time lapses, WPS disconnects from the SQL connection. Because sometimes the temporary table has only computed stats for a handful of variables.

Related

Can I run multiple instances of the same stored procedure that includes temp tables in SQL Server

I'm trying to execute a SQL Server stored procedure through Python Pyodbc and to get the selection results printed out into .csv files. But this procedure is currently being used in other daily tasks so I'm worried that if my executing the procedure in python will interrupt the daily scheduled job process in SQL Server Agent. In the procedure, it creates several temporary tables #temp_a, #temp_b, and #temp_c. I'm wondering if these temp tables will break the scheduled jobs that include this procedure since there might be other procedures that will be creating temp table names using the same name such as #temp_a or #temp_b. The temp tables are created inside the procedure but with no delete query written. I could have tested this myself but the database I'm working on right now is just so fragile that I was told not to create tests. Thanks!
Yes
The temp-Tables will be created per Session.
I got the procedure execute by different Sessions on the same time there will be the same count of the temp-tables as sessions executed.
This temp-tables have each a different name:
#V_...._000000003EB1
#V_...._000000003EB8
The example above are the temp-Tables created by the same Procedure executed two times by different sessions at the same time.
So your scenario couldn't happen

Execute SQL Server stored procedure from Oracle

I have an Oracle procedure that executes a SQL Server stored procedure. The SQL Server stored procedure executes one of three stored procedures based on an id field passed it from the Oracle procedure. Two of the stored procedure execute okay but the third does not.
The third stored procedure has several nested stored procedure calls. It performs some calculations and then inserts to 3 different tables and then some additional calculations and updates one of the tables it just inserted to. If I execute the stored procedure in SSMS it runs without issue. When it is executed from Oracle side I get nothing.
Could this because of the nesting but because of the implicit commits I see it work using SMSS? I tried doing a BEGIN Transaction in the starting stored procedure on the MS SQL Server side and a commit at the end where it should return. Still nothing. I have Try and Catch blocks in the MS SQL stored procedures and don't receive any errors.
Any suggestions would be greatly appreciated.
Thank you.
Does nothing means none of the inserts or updates show up in the tables. No error messages
As far as posting code; what specifically would you be interested in seeing? There are 4 stored procedures which have 100s of lines of code each.

Manipulating data from a stored procedure by saving data into a table

Problem: I have a stored procedure in SQL Server 2012 and I want to put constraints to the output so I only get relevant information.
I am using Execute. The way I see it I have two options:
save the result of the execution into a table, so I can use it for different purposes
put constraints to the variables in Execute so I only get the results I want
The first method is discussed here:
Insert results of a stored procedure into a temporary table .
My code is (due to company information I can't share the whole thing):
create table #mtable ( .... )
Insert into #mtable
Execute [myProcedure]
The error:
An INSERT EXEC statement cannot be nested.
I assume the error is because of the code in the stored procedure. How can I fix that problem without looking into the code for the stored procedure. Is there another way where I can save the content in a table?
My problem can also be solved by proposal #2. Is it possible for me to manipulate the output from the stored procedure with something like:
Execute [myProcedure] where variable1 > 100

Running synchronous commands to between two sql servers

I'm running a stored procedure on server1 from my application. The stored procedure does a bunch of stuff and populate a table on server2 with the result from the procedure.
I'm using linked server to accomplish this.
When the stored procedure is done running the application continues and tries to do some manipulation of the result from the stored procedure.
My problem is that the results from the stored procedure has not been completely inserted into the tables yet, so the manipulation of the tables fails.
So my question is. Is it possible to ensure the insert into on the linked server is done synchronous? I would like to have the stored procedure not return until the tables on the linked server actually is done.
You can use an output parameter of the first procedure. When the table is create on the second server the output parameter value will be return to your application and indicates the operation is ready.
If the things are difficult then this you can try setting a different isolation level of your store procedure:
http://msdn.microsoft.com/en-us/library/ms173763.aspx
I found the reason for this strange behavior. There was a line of code in my stored procedure added during debug that did a select on a temporary mem table before the data in the same table was written to the linked server.
When the select statement was run, the control was given back to my application and at the same time the stored procedure continued running. I guess the stored procedure was running synchronously from the start.

SQL: Using Stored Procedure within a Stored Procedure

I have a few stored procedures that return the same set of data (same columns) to a user. The stored procedure called depends on certain conditions. These stored procedures are fairly intensive and are being run by every user of the system. I would like to create stored procedure that calls each of these procedures and stores the data on a separate table. I will then run this new stored procedure every 5 minutes or so and let the users pull from the new table.
T_OutboundCallList is a permanent table with the same columns as returned by the two stored procedures.
I would like something like the following but when I try to run this it just runs continuously and I have to stop the procedure.
BEGIN
TRUNCATE TABLE T_OutboundCallList
INSERT T_OutboundCallList EXECUTE p_LeadVendor_GetCallsForCallList
INSERT T_OutboundCallList EXECUTE p_CallLog_GetAbandonedCallsCallList
END
Each of the procedures (*CallList) return a list of calls to be made and I do want them entered into the new table in this order (LeadVendor calls before AbandonedCalls). I also need to clear the table before adding the calls as there may be new calls that need to be higher in the list.
Is there some problem with this procedure that I am not seeing?
Thanks,
Brian
Without seeing the code in your *CallList procs it is hard to say what issue you are having. You should have the insert commands inside of your nested procedure. You can use the results of a procedure to insert data, but not like you are above. It is using OPENROWSET, and I think you will be better off the way I suggested.