if存储过程中的语句

时间:2016-10-06 14:08:35

标签: c# sql

我正在尝试找出解决此问题的最佳方法。

我有一个数据库表,用于在大学完成的教学活动,大学有几个校区正在教学我正在尝试根据下拉列表显示每个校园的gridview。我必须根据房间更新活动表字段CampusOccurred。客房为EMG12,OG114,DS15。

如果房间开始于: -

电子 - 赚取校园 O - Oden Campus
D - Donen Campus

My idea at the moment is:- 
For each row in Activites 
If Room MyString.ToCharArray()[0] = E 
Then Update CampusOccurred = Earn 
Else If Room MyString.ToCharArray()[0] = O
Then Update CampusOccurred = Odean 

我可以自己动手显示下拉列表和gridview,但我不确定最好的方法来解决上述问题。是否可以在SQL中执行If语句?

2 个答案:

答案 0 :(得分:2)

您可以编写一个执行以下操作的SQL查询:

select . . .,
       (case when left(Room, 1) = 'E' then 'Earn Campus'
             when left(Room, 1) = 'O' then 'Oden Campus'
             when left(Room, 1) = 'D' then 'Donen Compus'
        end) as Campus

如果您从数据库中以这种方式获取数据,那么就可以为您完成工作。

注意:大多数但不是所有数据库都支持left()。所有人都支持相同的功能。

答案 1 :(得分:2)

如果我理解您的请求,您需要更新SELECT ST.C.value('(Name/text())[1]', 'nvarchar(max)') as STName, ST.C.value('(Description/text())[1]', 'nvarchar(max)') as STDescription, RT.C.value('(Name/text())[1]', 'nvarchar(max)') as RTName, RT.C.value('(Description/text())[1]', 'nvarchar(max)') as RTDescription, RT.C.value('(Quantity/text())[1]', 'nvarchar(max)') as RTQty, RT.C.value('(Code/text())[1]', 'nvarchar(max)') as RTCode, NT.C.value('(Name/text())[1]', 'nvarchar(max)') as NTName, NT.C.value('(Description/text())[1]', 'nvarchar(max)') as NTDescription, NT.C.value('(PrimaryVlan/text())[1]', 'nvarchar(max)') as NTPVLAN, NT.C.value('(SecondaryVlan/text())[1]', 'nvarchar(max)') as NTSVLAN, NT.C.value('(Domain/text())[1]', 'nvarchar(max)') as NTDomain, NT.C.value('(RamSize/text())[1]', 'nvarchar(max)') as NTRam, NT.C.value('(CpuCores/text())[1]', 'nvarchar(max)') as NTCpus, NT.C.value('(VmTemplate/text())[1]', 'nvarchar(max)') as NTVMTemplate, NT.C.value('(ScomInstall/text())[1]', 'nvarchar(max)') as NTSCOM, NT.C.value('(SccmInstall/text())[1]', 'nvarchar(max)') as NTSCCM, NT.C.value('(Drives/DriveParameter[Letter="C"]/Capacity)[1]','int') AS CDrive, NT.C.value('(Drives/DriveParameter[Letter="D"]/Capacity)[1]','int') AS DDrive, NT.C.value('(Drives/DriveParameter[Letter="E"]/Capacity)[1]','int') AS EDrive, NT.C.value('(Drives/DriveParameter[Letter="F"]/Capacity)[1]','int') AS FDrive, NT.C.value('(Drives/DriveParameter[Letter="G"]/Capacity)[1]','int') AS GDrive from @XML.nodes('ACME/dbTemplates/ServiceTemplate') as ST(C) outer apply ST.C.nodes('Roles') as RO(C) outer apply RO.C.nodes('RoleTemplate') as RT(C) outer apply RT.C.nodes('NodeTemplate') as NT(C) ORDER BY STName,RTName 语句:

case