sql如果语法不正确

时间:2013-01-15 17:02:11

标签: sql if-statement syntax

下面的sql语句(没有if)返回1行,1列= 8.但是如果我在SELECT和on<上得到语法错误。我试图做的是,如果少于50行返回测试为真。如果返回50行,则返回false。

感谢您的帮助

IF SELECT COUNT([VDeck_ID]) FROM [SecondLifeDatabases].[dbo].[YuGiOh-Cards]
    WHERE [User_ID] = 
            (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-Users] 
            WHERE [UUID] = 'D38C3475-C995-4EB2-85D0-F2A3ED1D23A0') 
                and [VDeck_ID] = 
                    (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-VDecks]
                        WHERE [DeckName] = 'NewDeckName')
    < 50
BEGIN
    UPDATE [SecondLifeDatabases].[dbo].[YuGiOh-Cards] 
    SET [VDeck_ID] = 
        (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-VDecks] 
        WHERE [User_ID] = 
            (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-Users] 
            WHERE [UUID] = 'D38C3475-C995-4EB2-85D0-F2A3ED1D23A0') 
                and [DeckName] = 'NewDeckName') 
                WHERE [ID] = '11'
END

2 个答案:

答案 0 :(得分:2)

将括号放在select子句中的if语句周围:

IF (SELECT COUNT([VDeck_ID]) FROM [SecondLifeDatabases].[dbo].[YuGiOh-Cards]
    WHERE [User_ID] = 
            (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-Users] 
            WHERE [UUID] = 'D38C3475-C995-4EB2-85D0-F2A3ED1D23A0') 
                and [VDeck_ID] = 
                    (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-VDecks]
                        WHERE [DeckName] = 'NewDeckName')
   )
    < 50
BEGIN
    UPDATE [SecondLifeDatabases].[dbo].[YuGiOh-Cards] 
    SET [VDeck_ID] = 
        (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-VDecks] 
        WHERE [User_ID] = 
            (SELECT [ID] FROM [SecondLifeDatabases].[dbo].[YuGiOh-Users] 
            WHERE [UUID] = 'D38C3475-C995-4EB2-85D0-F2A3ED1D23A0') 
                and [DeckName] = 'NewDeckName') 
                WHERE [ID] = '11'
END

答案 1 :(得分:0)

你需要括号......

IF (SELECT COUNT(..) FROM ...) > 50