When I am trying to carry out following statement from Management Studio, it is executed successfully –
exec [sp_GetAllBillsForDate] '03/06/2012','03/06/2012'

But when I change it to
exec [sp_GetAllBillsForDate] getdate(), getdate()

it is generating error
Incorrect syntax near ')'.
whats wrong with this?
Thanks for sharing your time.
The answer is that you can’t pass a function as an argument to a stored procedure parameter.
If you just want to use the current date/time when you don’t want to pass values in, why not make those parameters optional by supplying a default value inside the procedure? This will save you from having to type it, declare local variables, and even more importantly from passing those useless tokens from client applications.
You should also avoid ambiguous date formats like m/d/y and d/m/y. If it wasn’t March right now, I’d have no idea whether you meant March 6 or June 3. And when you run your code somewhere with different regional or language settings, SQL Server might get it wrong too. State it unambiguously in a clear format (e.g.
YYYYMMDD) immune to language, region or human perception issues.Anyway here is the procedure with optional parameters:
Now the code for a hard-coded date:
And to get todays:
(Also a good idea to explicitly name your parameters. Then you don’t have to worry about the parameter order changing. And also to always use the schema prefix when referencing or creating all objects.)