I have a stored procedure in SQL Server.
Based on a parameter #hasLocationChanged, part of the stored procedure should be run (or not):
IF NOT EXISTS (SELECT 1 FROM PF_Condition WHERE SegmentId = #routeId)
SET #isUpdate = 0
ELSE
SET #isUpdate = 1
IF #hasLocationChanged = 1
BEGIN
EXEC fnUpdateConditionDataByRouteId #routeId, #isUpdate
EXEC fnUpdateTrafficDataByRouteId #routeId, #isUpdate
EXEC dbo.InsertmappedConsPlanningandConsHistory #routeId, #AgencyId, #LastSurfType, #LPDepth
END
When I am passing value 1 for #hasLocationChanged from the application it is getting accepted since the third stored procedure within the second IF condition block of code is running but the first two aren't. Is there any way I can understand what is causing the problem. Since I am trying to run this stored procedure using an application it is not returning me with any kind of error.
The stored procedure which I thought wasn't running had a block of code commented out which shouldn't
Related
I am trying to execute multiple stored procedures using only one input parameter in SSIS using Execute SQL Task but I keep getting this error:
[Execute SQL Task] Error: Executing the query "EXEC sample_stored_proc1..." failed with the following error: "Value does not fall within the expected range.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Here is a sample code inside SQL TASK for calling the stored procedures:
EXEC sample_stored_proc1 ?;
EXEC sample_stored_proc2 ?;
EXEC sample_stored_proc3 ?;
NOTES
I tried the code with only 1 stored procedure and it works but adding 1 or more stored procedures is giving me the above error.
using OLE DB connection
I found one solution, I created a new Stored Procedure with an input parameter and put this in EXECUTE SQL TASK instead. this will be used to execute all the stored procedures above.
ALTER PROCEDURE [dbo].[xp_EXEC_ALL_sample_SPs] #Start_Date as DATE
AS
BEGIN
EXECUTE sample_stored_proc1 #Start_Date;
EXECUTE sample_stored_proc2 #Start_Date;
EXECUTE sample_stored_proc3 #Start_Date;
END
Please let me know if there are better solution
when you execute a stored procedure it returns a execution status, so when you ran three procedure it returned multiple result sets, you can simply ignore the result set returned by
DECLARE #result int;
DECLARE #start_date DATE = ?;
EXEC #result = sample_stored_proc1 #start_date;
EXEC #result = sample_stored_proc2 #start_date;
EXEC #result = sample_stored_proc3 #start_date;
doing so will suppress multiple result sets from the query
I'm updating a stored procedure that checks for the presence of a local file, using a string as input. It's used as a sanity check for some SSIS packages, the only output it gives is whether it fails to find the file, in which case it will return an SQL Error.
The procedure can handle wildcards, so I was thinking of expanding the procedure to return the name of the file it finds (And a new error if it gets multiple returns), which would be useful for dealing with packages that variable filenames but use flat file connectors. Adding an output parameter to the procedure means that you can't use the procedure unless you declare a variable to hold the output though.
Is there a way to structure the procedure so that it only gives an output if someone specifies that they want said output?
CREATE PROC dbo.MySP #i1 INT
,#i2 INT = NULL OUTPUT
AS
BEGIN
SELECT 1
END
GO
EXEC dbo.MySP 1
GO
DECLARE #i INT
EXEC dbo.MySP 1
,#i OUTPUT
GO
DECLARE #i INT
EXEC dbo.MySP 1
,#i
GO
Consider the following stored procedure:
ALTER PROCEDURE Administration.SetAndRetrieveNewPurchaseOrderNumber
#PurchaseOrderNumber INT OUTPUT
AS
BEGIN
SET NOCOUNT ON
UPDATE Administration.KeyNumbers
SET PurchaseOrderNumber += 1
WHERE RowId = 1
SET #PurchaseOrderNumber = (SELECT kn.PurchaseOrderNumber
FROM Administration.KeyNumbers kn
WHERE kn.RowId = 1)
END
GO
I can use this easily from within my application by simply executing the procedure and passing in by reference a suitably named parameter.
I now find myself wanting to execute the procedure listed above in another stored procedure. I tried the following, but it doesn't appear to work (either with or without the # symbol in the parameter part of the stored procedure being called;
DECLARE #PurchaseOrderNumber INT
EXEC Administration.SetAndRetrieveNewPurchaseOrderNumber(#PurchaseOrderNumber)
What is the correct way to do this, or in reality should there be a separate procedure for use in circumstances like this that only produces a scalar result?
You need to add the output keyword when passing in the parameter.
For example:
Declare #output int;
Exec storedproc(#parameter output)
I want to rerun a stored procedure with a pass-thru. How do I get it to return a value if it runs properly or fails?
Right now, my code is returning nothing regardless of whether the stored properly is running without errors or not.
proc sql;
connect to odbc(noprompt="Driver={DataDirect 6.1 SQL Server Wire Protocol};Host=99.999.999.99;Port=9999;Database=ReportServer02;Uid=&userid.;Pwd=&password.;");
create table blob as select * FROM CONNECTION TO odbc
(declare #r int
exec dbo.DivByZero;
select #r as DivByZero;);
disconnect from odbc;
run;
If you return something properly (as #mallan1121 describes), then you should be able to do it with less code, actually:
create table blob as select * FROM CONNECTION TO odbc
(
exec dbo.DivByZero;
);
The RC should be automatically selected into blob.
You can also look at the &SQLXRC and &SQLXMSG macro variables after the SP has run (even if you use execute ... instead of select from); those ought to have the return code and any returned message in them.
You can use a return code with RETURN() within your stored procedure. For example:
IF #divisor = 0
BEGIN
RETURN(1) --Error!
END
ELSE
BEGIN
--do some work
RETURN(0) --Success!
END
And then when you call just get the return code.
DECLARE #rc int
EXEC #rc = dbo.DivByZero
SELECT #rc
MSDN Article on SPROC return codes: https://technet.microsoft.com/en-us/library/ms190778(v=sql.105).aspx
i need to execute several SQL statements inside an stored procedure, save the result into a variable for later use.
SET NOCOUNT OFF
DECLARE #P TABLE
#P = SELECT d.Periodo
from [SISACT].DEPRECIACIONES d, [SISACT].CONTROL_PERIODO c
where c.vigente = 1 AND d.Periodo = c.periodo
IF ##ROWCOUNT > 0
PRINT 'Periodo ya depreciado'
So later i can do something like this
UPDATE [SISACT].ACTIVOS_FIJOS set ultimo_periodo = #p.periodo ...
I know the #P = SELECT is wrong, i saw an example where they used another stored procedure instead of a SELECT statement, i want to know if this is possible without using a stored procedure. If so, how? I'm just going to use the query once in only one stored procedure so i see no point into putting it in another stored procedure. I'm new to T-SQL and SQL server , i'm learning on my own as well and i'm sorry if this is an stupid question.
P.S: The query in this case should return just one record. I'm using SQL SERVER 2008 Express.
Thank you for any help in advance.
I think that what you want.
SET NOCOUNT OFF
DECLARE #P as CHAR(6)
SELECT p# = d.Periodo from [SISACT].DEPRECIACIONES d, [SISACT].CONTROL_PERIODO c where c.vigente = 1 AND d.Periodo = c.periodo
IF ##ROWCOUNT > 0
PRINT 'Periodo ya depreciado'
and later you can use
UPDATE [SISACT].ACTIVOS_FIJOS set ultimo_periodo = #p