System.Data.SqlClient.SqlException procedure has no parameters and arguments were supplied. How should I go about fixing this error? - sql-server

I built a database application in Visual Studio. When I run, I get this error:
System.Data.SqlClient.SqlException: procedure sp_select_number_of_films has no parameters and arguments were supplied.
I know what the error means, but I am not sure how to fix it in my SQL code. I have tried a bunch of small tweaks, but I still get the same error.
Stored procedure:
CREATE PROCEDURE [dbo].[sp_select_number_of_films]
(#REC_ID_OUTPUT int OUTPUT)
AS
SELECT COUNT(*)
FROM Film.Title;
EXEC dbo.sp_select_number_of_films
GO

It should be called as:
DECLARE #sth INT;
Exec dbo.sp_select_number_of_films #sth OUTPUT;
But then inside stored procedure you need to assign output variable:
CREATE PROCEDURE [dbo].[sp_select_number_of_films]( #REC_ID_OUTPUT int OUTPUT)
AS SELECT #REC_ID_OUTPUT = COUNT(*)
FROM Film.Title;
And you should avoid naming stored procedures with "sp_" prefix.

Related

Execute multiple stored procedures with one input parameter SSIS

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

Declaring DATETIME in Stored proc MS SQL

I am trying to get System Datetime for a column when a new row is inserted or updated into a table using stored Proc in MS SQL. How can I achieve it?
I have tried below code
CREATE PROCEDUCE test_Cl_INSERT
#SRC_ID int,
#CREATED_BY datatime
AS
BEGIN
INSERT into dbo.CL_Batch(SRC_ID, Created_BY)
VALUES(#SRC_ID, CURRENT_TIMESTAMP)
END
EXEC dbo.test_Cl_INSERT
#SRC_ID=44
ERROR : #CREATED_BY parameter missing
This will work:
CREATE PROCEDURE test_Cl_INSERT
#SRC_ID int
AS
BEGIN
INSERT into dbo.CL_Batch(SRC_ID, Created_BY)
VALUES(#SRC_ID, CURRENT_TIMESTAMP)
END
EXEC dbo.test_Cl_INSERT
#SRC_ID=44
Your procedure signature is:
CREATE PROCEDUCE test_Cl_INSERT
#SRC_ID int,
#CREATED_BY datatime
You attempt to execute as:
EXEC dbo.test_Cl_INSERT #SRC_ID=44
Do you see something missing? You should. Your procedure has 2 parameters but you provide only 1 when you attempt to execute it. That is your problem. As already noted, you don't use that paramter within the logic of the procedure so why does it exist at all?
You must execute your procedure like this:
EXEC dbo.test_Cl_INSERT #SRC_ID=44, #CREATED_BY = '20201124 12:49';
Notice I just assigned a random value to the parameter since it (the parameter) is not used within your procedure code. That solves the question you ask. However, you have more important issues to consider.

How do i execute a stored procedure with a parameterised output from within another stored procedure

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)

Error when trying to execute stored procedure

When trying to run a stored procedure, I get the following error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '#CSVPath'.
I'm trying to execute it using the following:
EXEC dbo.ProcessData #CSVPath = 'D:\Data.csv'
My stored procedure starts like this:
ALTER PROCEDURE [dbo].[ProcessDataData]
#CSVPath varchar(MAX) --Path to CSV containing data.
AS
BEGIN
{query}
END
I don't know what I'm doing wrong when passing the parameter value.
Thank you.
Assuming Your proc name has the word data only once, I have just managed this successfully:
create PROCEDURE [dbo].[ProcessData]
#CSVPath varchar(MAX) --Path to CSV containing data. AS BEGIN
select #CSVPath END
EXEC dbo.ProcessData #CSVPath = 'D:\Data.csv'
Output:
D:\Data.csv
First confirm with stored procedure name and use the same SP name so that u will not get this kind of errors
You are executing wrong sp
use below query it will execute
EXEC dbo.ProcessDataData #CSVPath = 'D:\Data.csv'
This is work...your Procedure name is [dbo].[ProcessDataData] and you execute EXEC dbo.ProcessData....please like that execute EXEC dbo.ProcessDataData its work for mee...
ALTER PROCEDURE [dbo].[ProcessDataData]
#CSVPath varchar(MAX) --Path to CSV containing data.
AS
BEGIN
-- Query to execute data
END
EXEC dbo.ProcessDataData #CSVPath = 'D:\Data.csv'
Hope Its Work !!!
Happy Coding !!!
Try it, may be it works for you;
EXEC dbo.ProcessData 'D:\Data.csv'
It'll auto map your data to the parameter: #CSVPath. Otherwise, may be the issue was in your query. If issue still exists, share your complete StoredProcedure for better judgment and solution.

Validating missing parameter from procedure calls

I have a stored procedure which in turn calls another procedure passing parameters:
Alter procedure pTest
#IDOB datetime,
#IName nvarchar(200)
as
Exec pStaffInsert #IDOB
When I open this procedure in Management Studio, I get the following warning, which is absolutely correct:
Procedure or function 'pStaffInsert' expects parameter '#IName', which was not supplied.
It should have been
Alter procedure pTest
#IDOB datetime,
#IName nvarchar(200)
as
Exec pStaffInsert #IDOB, #IName
I similarly have close to 500+ procedures in my application. Is there a way by which I can run a validation on all the procedures calling another with missing parameters?
I tried
sp_refreshsqlmodule
but it does not help in this regard.
Thank you.

Resources