0

代码工作正常,除了

SELECT DISTINCT isnull(@FEATURE + ', ', ',') + Feature 
    FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 where (rf2.roomid =   rf.roomid)
    for xml path('')), 

这给我带来了问题。我不知道如何去掉第一个逗号。我已经尝试过子字符串,但它删除了所有逗号(我猜是因为它在 select 语句中)。我什至尝试过 COALESCE(@FEATURE + ', ', '') + FEATURE但我最终得到了相同的结果,要么没有逗号,要么在末尾或开头多了一个逗号。

DECLARE @FEATURE VARCHAR(500) 
-- INSERT INTO BookableRooms IF NOT EXISTS ROOM NUMBER 
INSERT INTO dbo.BookableRooms_test (FK_Building_code,
                                    Room_num,Other_room_name,
                                    Capacity_from,
                                    Capacity_to,
                                    Accessiblity,AV_book_separate,Flooring_type,Features,
                                    Food_drink_allowed, Alcohol_allowed,Internet,
                                    [Owner],FK_BookingContact_ID,
                                    LCD_projector,Computer,FK_SpaceManager_code,Last_updated_date,Last_updated_by,SpecialFlag)
SELECT DISTINCT CASE
            WHEN rf.[Building Reference] is null or rf.[Building Reference] = '' THEN 'HH'
            ELSE rf.[Building Reference] 
        END,
        [ROOMID],[Description],
       (SELECT MIN(rf2.CAPACITY) FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] AS rf2 WHERE rf2.ROOMID = rf.ROOMID),
       (SELECT MAX(rf2.CAPACITY) FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] AS rf2 WHERE rf2.ROOMID = rf.ROOMID),  
       dbo.iszero([Wheelchair Accessible]), dbo.iszero(rf.[Separate AV]),dbo.isflat(rf.[Flat Floor]),
       (
        SELECT DISTINCT isnull(@FEATURE + ', ', ',') + Feature 
        FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 where (rf2.roomid = rf.roomid)
        for xml path('')),
       dbo.iszero(rf.[Food and Drink]),dbo.iszero(rf.[Alcohol]),dbo.iszero(rf.[Room Internet]),
       'HH',5,
       dbo.iszero(rf.[Digital Projector]),dbo.iszero(rf.[Computer]),'HH',GetDate(),'System',dbo.iszero(rf.[Restricted])

 FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf  where NOT EXISTS(
    SELECT 1 FROM dbo.BookableRooms_test b WHERE 
    b.Room_num = rf.ROOMID);
4

3 回答 3

3

使用 Stuff 但在子查询的外侧:

stuff ((SELECT DISTINCT ',' + Feature 
        FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 
        WHERE (rf2.roomid = rf.roomid)
        for xml path('')), 1, 1, '')
于 2012-04-12T20:24:59.727 回答
2

使用STUFF函数删除前导逗号。

SELECT DISTINCT STUFF(',' + Feature 
    FROM [128.100.194.219, 1435].[EMSData].[dbo].[vOSLRoomFeatures] as rf2 where (rf2.roomid =   rf.roomid)
    for xml path('')),1,1,''), 
于 2012-04-12T20:22:32.670 回答
0

我不确定您要达到的目标。如果我的猜测是错误的,最好告诉我们在哪种情况下您期望得到什么结果。

尝试这个

CASE WHEN @FEATURE IS NULL THEN NULL ELSE @FEATURE + ', ' END
于 2012-04-12T20:18:16.777 回答