存储过程日期参数不使用cast

时间:2022-09-23 09:34:38

I have a stored procedure with parameters:

我有一个带参数的存储过程:

CREATE PROCEDURE [dbo].[sp_SCOPRO_HLP] (                            
  @dDateFrom DATE = NULL
, @dDateTo DATE = NULL                        
)
AS
-- rest of procedure

My question is, why this is working:

我的问题是,为什么这行得通:

EXEC sp_SCOPRO_HLP 
  @dDateFrom = '2017-01-01'
, @dDateTo = '2017-01-31'   

but this is not working (it gaves me syntax error):

但这并不奏效(这让我犯了语法错误):

EXEC sp_SCOPRO_HLP 
  @dDateFrom = cast('2017-01-01' as date)
, @dDateTo = cast('2017-01-31' as date)

Syntax error:

语法错误:

存储过程日期参数不使用cast

1 个解决方案

#1


3  

Following to https://connect.microsoft.com/SQLServer/feedback/details/352110/t-sql-use-scalar-functions-as-stored-procedure-parameters

后到https://connect.microsoft.com/SQLServer/feedback/details/352110/t-sql-use-scalar-functions-as-stored-procedure-parameters

You can't use functions like parameters.

不能使用参数之类的函数。

All the members of SQL community would agree that acceptable form should be - less code, good readability.

SQL社区的所有成员都认为可接受的表单应该是:代码少,可读性好。

So the best that you can do is:

所以你能做的最好的事情是:

DECLARE @dateFromDate = cast('2017-01-01' as date)
DECLARE @dateFromTo = cast('2017-01-31' as date)

EXEC sp_SCOPRO_HLP 
  @dDateFrom = @dateFromDate
, @dDateTo = @dateFromTo

#1


3  

Following to https://connect.microsoft.com/SQLServer/feedback/details/352110/t-sql-use-scalar-functions-as-stored-procedure-parameters

后到https://connect.microsoft.com/SQLServer/feedback/details/352110/t-sql-use-scalar-functions-as-stored-procedure-parameters

You can't use functions like parameters.

不能使用参数之类的函数。

All the members of SQL community would agree that acceptable form should be - less code, good readability.

SQL社区的所有成员都认为可接受的表单应该是:代码少,可读性好。

So the best that you can do is:

所以你能做的最好的事情是:

DECLARE @dateFromDate = cast('2017-01-01' as date)
DECLARE @dateFromTo = cast('2017-01-31' as date)

EXEC sp_SCOPRO_HLP 
  @dDateFrom = @dateFromDate
, @dDateTo = @dateFromTo