比如输入两个int变量2013和3,然后输出2013-3-1
但是目前出错
ALTER PROCEDURE CountOrderRORInMonth
@OrderId varchar(100) = null,
@year int = null,
@month int =null
AS
BEGIN
PRINT '@OrderId is:'+CONVERT(varchar, @OrderId);
PRINT '@year is:'+CONVERT(varchar, @year);
PRINT '@month is:'+CONVERT(varchar, @month);
Set DateFormat YMD;
DECLARE @temp varchar,@beginROR datetime,@endROR datetime;
SELECT @temp=(CONVERT(varchar, @year)+'-' + CONVERT(varchar, @month) +'-1');
PRINT '@temp is:'+CONVERT(varchar, @temp);
--SELECT @beginROR=@temp;
--SET @beginROR=CAST(@temp AS datetime);
--PRINT '@beginROR is:'+CONVERT(varchar, @beginROR);
END
输出的结果为啥是
EXEC CountOrderRORInMonth @OrderId='2013022048265668',@year=2013,@month=3;
GO
Warning: [FreeTDS][SQL Server]@OrderId is:2013022048265668
[FreeTDS][SQL Server]@year is:2013
[FreeTDS][SQL Server]@month is:3
[FreeTDS][SQL Server]@temp is:2
为2呢?奇怪
如果直接
SELECT CONVERT(varchar, 2013)+'-' + CONVERT(varchar, 3) +'-1'
得到
+----------+
| |
+----------+
| 2013-3-1 |
+----------+
1 rows in set (0.06 sec)
比较疑惑,存储过程为什么不能得到正确内容
------解决方案--------------------
习惯不好
DECLARE @temp varchar
---》
DECLARE @temp varchar(20)
------解决方案--------------------
转换可以不规定长度,定义一定要规定长度