当我运行显示错误如下

问题描述:

declare @Curmonth  Datetime,
         @curyear Datetime,
         @Start_date varchar(20),
         @Batch_id varchar(20)

 set @curyear = select datepart(year,getdate())  - 5
 set @Curmonth = select datepart(month,getdate()) + 1


select cbm_batch_id, cmn_minor_code,convert(char(12),cbm_batch_start_dt,106) as Start_date
from co_batch_master 
where cmj_major_code = 'REUC' and cbm_active <> 'D' and 
datepart(year,cbm_batch_start_dt) = @curyear and datepart(month,cbm_batch_start_dt) = @Curmonth



当我运行时显示错误如下;


when i run shows error as follows;

Incorrect syntax near the keyword 'select'.
Incorrect syntax near the keyword 'select'.



请帮帮我。



什么是我上面的查询中的错误



Rgds,

Narasiman P


please help me.

what is the mistake in my above query

Rgds,
Narasiman P

修改波纹管块

Modify bellow block
set @curyear = select datepart(year,getdate()) - 5
set @Curmonth = select datepart(month,getdate()) + 1

as

<pre lang="sql">select @curyear=datepart(year,getdate()) - 5
select @Curmonth =  datepart(month,getdate()) + 1</pre>


Hello



问题在这里





设置@curyear = 选择 datepart(年,getdate()) - 5

设置@Curmonth = 选择 datepart(月,getdate())+ 1



而不是 删除datepart中的select





set @curyear = datepart(year,getdate() ) - 5

设置@Curmonth = datepart(月,getdate())+ 1





b $ b







享受编码
Hello

Problem is here


set @curyear = select datepart(year,getdate()) - 5
set @Curmonth = select datepart(month,getdate()) + 1

Instead Of remove the select in datepart


set @curyear = datepart(year,getdate()) - 5
set @Curmonth = datepart(month,getdate()) + 1







Enjoy coding


你好,



首先我会建议年份和月份变量不要使用datetime数据类型,因为它不会给你想要的输出。 。因为2009或20012不是日期时间类型数据..你可以使用smallint年和月..

和第二个当你将任何查询或表达式的输出分配给一个varibale时把它放nside括号()。



在你的情况下,你可以使用以下线...





Hello ,

first i would recommend that for year and month variables dont use datetime datatype as it will not give you desired output.. as 2009 or 20012 is not a datetime type data.. you can use smallint for year and month..
and second when you assign output of any query or expression to a varibale put it inside parenthesis ().

well in your case you can use below lines..


declare @Curmonth  tinyint,
         @curyear smallint


 set @curyear = datepart(year,getdate())  - 5
 set @Curmonth = datepart(month,getdate()) + 1


select @curyear,@curmonth



b $ b或



or

declare @Curmonth  tinyint,
         @curyear smallint


 select @curyear = datepart(year,getdate())  - 5
 select @Curmonth = datepart(month,getdate()) + 1









or

declare @Curmonth  tinyint,
         @curyear smallint


 set @curyear = (select datepart(year,getdate())  - 5)
 set @Curmonth = (select datepart(month,getdate()) + 1)


select @curyear,@curmonth