I have a stored proc, say, "call_Me" with few parameters:
Declare @Greet varchar(100) = 'Hi ||User||'
Exec Call_Me 1,'something', @Greet --parameters: bit, string, string
during the call, I want to be able to replace the
||User||
bit with something else. normally, in a select statement, I would do this:
select 1, 'something', Replace(@Greet, '||User||', u.Username) from UserTable
which works fine, But today, for the first time, I am trying to use it in exec statement, the error says its expecting select, I tried adding select in every possible (and sensible) way but it just didnt seem to work out.
How can I use a replace during an execute statement call?
Many thanks in advance!
The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: The search is case-insensitive. Tip: Also look at the STUFF() function.
The Replace statement is used to replace all occurrences of a specified string value with another string value. The Replace statement inserts or replaces values in a table. Use the Replace statement to insert new rows in a table and/or replace existing rows in a table.
You'd need to format @Greet before passing it to the sproc:
Declare @Greet varchar(100) = 'Hi ||User||'
SELECT @Greet = Replace(@Greet, '||User||', u.Username)
FROM UserTable u
WHERE Id = 1
Exec Call_Me 1,'something', @Greet --parameters: bit, string, string
You can only use a literal or a variable reference in a procedure call:
[ { EXEC | EXECUTE } ]
{
[ @return_status= ]
{ module_name [ ;number ] | @module_name_var }
[ [ @parameter= ] { value
| @variable [ OUTPUT ]
| [ DEFAULT ]
}
]
[ ,...n ]
[ WITH RECOMPILE ]
}
[;]
Use this:
Declare @Greet varchar(100) = 'Hi ||User||'
Declare @param VARCHAR(100) = REPLACE(@Greet, '||User||', 'replacement')
Exec Call_Me 1,'something', @param
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With