日期:2014-05-18  浏览次数:20492 次

存储过程用IF语句出错
SQL code


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- ================================

-- ================================
ALTER PROCEDURE [dbo].[usp_OQC_BadChart]
(
    @B_Yer varchar(4),
    @B_Month varchar(2),
    --@B_Type varchar(30),
    @B_Class char(1)
)
AS
if(@B_Class=='1')
{
select BadContent,count(1) as BNO
from T_OQC_ProductionPlan
where Year(OQCTime)=@B_Yer and month(OQCTime)=@B_Month and BadContent!='无' and Models like 'DVS%'
group by BadContent
}
else
{
select BadContent,count(1) as BNO
from T_OQC_ProductionPlan
where Year(OQCTime)=@B_Yer and month(OQCTime)=@B_Month and BadContent!='无' and Models like 'DXM%'
group by BadContent
}



提示错误

消息 102,级别 15,状态 1,过程 usp_OQC_BadChart,第 12 行
'=' 附近有语法错误。
消息 102,级别 15,状态 1,过程 usp_OQC_BadChart,第 18 行
'}' 附近有语法错误。
消息 102,级别 15,状态 1,过程 usp_OQC_BadChart,第 25 行
'}' 附近有语法错误。

------解决方案--------------------
SQL code
ALTER PROCEDURE [dbo].[usp_OQC_BadChart]
(
    @B_Yer varchar(4),
    @B_Month varchar(2),
    --@B_Type varchar(30),
    @B_Class char(1)
)
AS
if(@B_Class='1')
select BadContent,count(1) as BNO
from T_OQC_ProductionPlan
where Year(OQCTime)=@B_Yer and month(OQCTime)=@B_Month and BadContent!='无' and Models like 'DVS%'
group by BadContent

else

select BadContent,count(1) as BNO
from T_OQC_ProductionPlan
where Year(OQCTime)=@B_Yer and month(OQCTime)=@B_Month and BadContent!='无' and Models like 'DXM%'
group by BadContent

------解决方案--------------------
SQL code

declare @i int set @i=1
if(@i=1) --用一个等号就行
select 'a'