Stored procedure with named parameter and calculation

Binesh Nambiar C picture Binesh Nambiar C · Jun 20, 2016 · Viewed 12.1k times · Source

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

Answer

Yobik picture Yobik · Jun 20, 2016

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;