我有3个表,我需要从中提取数据,这些数据取决于其他表的结果,因此我尝试使用if then else结构并且遇到语法错误。就SQL专业知识而言,我可能会超过我的头脑。我在第一个if附近产生语法错误,然后在每个附近产生语法错误。这是一个简单的语法错误还是我试图做一些MS SQL 2000无法处理的事情?
感谢您的投入。
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
Go
Begin
Declare @vCasinoName varchar(35)
select @vCasinoName = Description from Gen_Casino
Create Table #AG_Report
(Slotnumber int,
SRID varchar(30),
SerialNumber varchar(15),
CAsset varchar(30),
Mfr varchar(15),
Denomination numeric(12, 2),
Description varchar(30),
Par real,
EPROMID varchar(15),
EPROMID2 varchar(15),
EPROMIDXL varchar(30),
Jurisdiction varchar(30),
Bios varchar(30),
BootChip varchar(30),
VersionChip varchar(30),
CasinoName varchar(35),
)
insert into #AG_Report
select s.SlotNumber,
s.SerialNumber,
m.Manufacturer,
s.Denomination,
s.Description,
s.Par,
s.EPROMID,
s.EPROMID2,
if (CF.FieldID = 6)
BEGIN
CF.Value as SRID,
END
else if (CF.FieldID = 7)
BEGIN
CF.Value as CAsset,
END
else if (CF.FieldID = 9)
BEGIN
else if (CF.FieldID = 9)
BEGIN
CF.value as EPROMIDXL,
else if (CF.FieldID = 13)
BEGIN
CF.Value as Jurisdiction,
END
else if (CF.FieldID = 11)
BEGIN
CF.Value as Bios,
END
else if (CF.FieldID = 12)
BEGIN
CF.Value as BootChip,
END
else if (CF.FieldID = 14)
BEGIN
CF.Value as VersionChip,
END
from CDS_Slotmast S, BB_SlotMastCustomField CF,BB_Mfr M,
where S.SlotMast_id = CF.SlotMast_ID
and CF.Revision = S.Revision
and S.Active = "Y"
order by s.Denomination, s,SlotNumber
Select * from #AG_Report
End
答案 0 :(得分:0)
详细说明@HABO所说的内容,而不是在select语句中:
if (CF.FieldID = 6)
BEGIN
CF.Value as SRID,
END
else if (CF.FieldID = 7)
试试这个:
CASE
WHEN (CF.FieldID = 6) THEN 'SRID' --You can return a value from a field or have another case statement here if you need to.
WHEN (CF.FieldID = 7)
...Continue your code
ELSE
...If you want an ELSE
END --End the case statement
答案 1 :(得分:0)
在SQL Server中无法在SELECT语句中使用IF。相反,您可以使用CASE语句。您也可以使用嵌套的案例,WHEN中的案例
答案 2 :(得分:0)
case语句是选择内部IF的正确方法,但从你的语句看起来,无论CF.FieldID的值如何,你总是返回CF.Value,但是尝试返回不同的别名专栏。您不能真正这样做 - 列别名需要对结果集中的每一行都相同。但是在返回数据方面,您可以在结果集中包含CF.FieldID和CF.Value,然后让应用程序代码完成工作。