Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure with named parameter and calculation

I'm calling a stored procedure with named parameter.

exec MySP @name = 'binesh', @amount = @amt, @date = @date

It's working fine for me.

But when I'm trying

exec MySP2 @name = 'binesh', @amount = -@amt, @date = @date

or

exec MySP3 @name = 'binesh', @amount = @amt, @date = convert(varchar(25), @date, 131)

I get a syntax error.

Is this mandatory that I need to create separate variables for each (common sense tells it won't be the way). So what is the syntax for this?

Thanks all

Binesh

like image 437
Binesh Nambiar C Avatar asked Jun 20 '16 02:06

Binesh Nambiar C


People also ask

Can pass 3 types of parameters to stored procedures What are they?

As a program, a stored procedure can take parameters. There are three types of parameters: IN, OUT and INOUT.

How do you call a stored procedure with parameters?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

Can a stored procedure use parameters?

Parameters are used to exchange data between stored procedures and functions and the application or tool that called the stored procedure or function: Input parameters allow the caller to pass a data value to the stored procedure or function.

What is difference between UDF and Storedprocedure?

UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be. UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.


1 Answers

You can not construct input "in-line" for stored procedures. You must resolve the inputs prior to using them.

For example, you need to do something like this (as well as resolve the other parameters) ...

declare
    @date varchar(25) = convert(varchar(25), @date, 131);

exec MySP3 @name = 'binesh', @amount = @amt, @date = @date;
like image 152
square_particle Avatar answered Sep 28 '22 20:09

square_particle