Error when trying to execute stored procedure - sql-server

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.

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.

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

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.

Receiving an error when executing a stored procedure that calls a linked server

I am in the process of setting up dynamic Reporting Services Shared Data Source using Linked Server. In doing so, I have created a stored procedure that requires a parameter and in testing the stored procedure (before setting everything else in SSRS) I am receiving an error. The stored Procedure code looks like this:
create procedure [dbo].[SelectFromServer3]
#ServerName sysname
as
begin
set nocount on;
declare #sql nvarchar(max)
set #sql = N'Select * from ' + quotename(#ServerName)+ '
.remotesvrnm.dbo.CUSTOMERS_DISAM_TABLE'
exec sp_executesql #sql
end
When I execute the following query to test the stored procedure:
EXEC SelectFromServer3 #ServerName = 'SQLSVRInstanceNM'
I receive the following error:
"Msg 208, Level 16, State 1, Line 1
Invalid object name 'remotesvrnm.dbo.CUSTOMERS_DISAM_TABLE'."
However, if I query the linked server directly with a query like:
EXEC ('SELECT * from remotesvrnm.dbo.CUSTOMERS_DISAM_TABLE') AT LNKDSRVRNM
The data is returned as expected. Even using an openquery statement works as expected:
Select * from openquery (LNKDSRVRNM, 'select * from remotesvrnmdbo.CUSTOMERS_DISAM_TABLE')
The guide that I am using to set this up I pulled from a Web search. I am at a loss as to how to resolve this. Any guidance would be greatly appreciated.
Thanks.
Can you use print #sql in your stored procedure to see what it evaluates to? Paste that into a new query window and see if you get the same error. If yes, please paste it here.
The code as shown works perfectly for me.
Thank you #PaulHoke for the suggestion to add to the stored procedure. I found that I had two things wrong... I found that I was missing a space in my stored procedure code. I was missing a space between the single quote and the .remotesvrnm.dbo.CUST....'.
The second issue was that when executing the stored procedure, I needed to use the name of the linked server, not the SQL Server Instance name. --The guide that I have been following, that I found on-line, was poorly written.

How to Execute SQL Server Stored Procedure in SQL Developer?

I've been given a user account to a SQL Server database that only has privileges to execute a stored procedure. I added the JTDS SQL Server JDBC jar file to SQL Developer and added it as a Third Party JDBC driver. I can successfully log in to the SQL Server database. I was given this syntax for running the procedure:
EXEC proc_name 'paramValue1' 'paramValue2'
When I run this as either a statement or a script, I get this error:
Error starting at line 1 in command:
EXEC proc_name 'paramValue1' 'paramValue2'
Error report:
Incorrect syntax near the keyword 'BEGIN'.
I tried wrapping the statement in BEGIN/END, but get the same error. Is it possible to call the procedure from SQL Developer? If so, what syntax do I need to use?
You don't need EXEC clause. Simply use
proc_name paramValue1, paramValue2
(and you need commas as Misnomer mentioned)
You are missing ,
EXEC proc_name 'paramValue1','paramValue2'
You need to do this:
exec procName
#parameter_1_Name = 'parameter_1_Value',
#parameter_2_name = 'parameter_2_value',
#parameter_z_name = 'parameter_z_value'
EXECUTE [or EXEC] procedure_name
#parameter_1_Name = 'parameter_1_Value',
#parameter_2_name = 'parameter_2_value',
#parameter_z_name = 'parameter_z_value'
I know this is the old one. But this may help others.
I have added SP calling function between BEGIN/END. Here is a working script.
ALTER Proc [dbo].[DepartmentAddOrEdit]
#Id int,
#Code varchar(100),
#Name varchar(100),
#IsActive bit ,
#LocationId int,
#CreatedBy int,
#UpdatedBy int
AS
IF(#Id = 0)
BEGIN
INSERT INTO Department (Code,Name,IsActive,LocationId,CreatedBy,UpdatedBy,CreatedAt)
VALUES(#Code,#Name,#IsActive,#LocationId,#CreatedBy,#UpdatedBy,CURRENT_TIMESTAMP)
EXEC dbo.LogAdd #CreatedBy,'DEPARTMENT',#Name
END
ELSE
UPDATE Department SET
Code = #Code,
Name = #Name,
IsActive = #IsActive,
LocationId = #LocationId,
CreatedBy = #CreatedBy,
UpdatedBy = #UpdatedBy,
UpdatedAt = CURRENT_TIMESTAMP
where Id = #Id
You need to add a , between the paramValue1 and paramValue2. You missed it.
EXEC proc_name 'paramValue1','paramValue2'
EXEC proc_name #paramValue1 = 0, #paramValue2 = 'some text';
GO
If the Stored Procedure objective is to perform an INSERT on a table that has an Identity field declared, then the field, in this scenario #paramValue1, should be declared and just pass the value 0, because it will be auto-increment.
There are two ways we can call stored procedure
CALL database name'. 'stored procedure name(parameter values);
example:- CALL dbs_nexopay_sisd1_dec_23.spr_v2_invoice_details_for_invoice_receipt_sub_swiss(1, 1, 1, 1);
From your MySQL workbench also you can do that.
i. Right-click on stored procedure.
ii. Send to SQL editor
iii. Procedure call.
If you simply need to excute your stored procedure
proc_name 'paramValue1' , 'paramValue2'...
at the same time you are executing more than one query like one select query and stored procedure you have to add
select * from tableName
EXEC proc_name paramValue1 , paramValue2...
The stored procedures can be run in sql developer tool using the below syntax
BEGIN
procedurename();
END;
If there are any parameters then it has to be passed.
Select * from Table name ..i.e(are you save table name in sql(TEST) k.
Select * from TEST then you will execute your project.

Resources