2016-11-21 47 views
0

enter image description here创建表,并使用if语句

enter image description here

我有这个问题,我真的不明白

我需要帮助理解和回答在座它

+1

让我先说这不是现实世界的情景,但我想他们试图让你应用不同的概念。你对这个问题的理解有什么困难?简而言之,他们要求您获取RACES_COMPLETED在500到900之间的所有行,这是一条基本的SELECT语句。你对SQL Server有什么经验? – bassrek

+0

没有太先进,但我理解得很好 –

+0

我认为有人希望您使用SQL Server Management Studio将某些行选入临时表中。打印消息需要另一个SQL语句。还有其他的可能性。 –

回答

1
--Create the table on the fly with the condition stated 
SELECT 
    TEAM_NAME, 
    RACES_COMPETED 
INTO [TOP TEAMS] 
FROM YourTable 
WHERE RACES_COMPETED >= 500 and RACES_COMPETED <= 900 


--Store the number of teams in the new table in a variable for ease of use 
DECLARE @topTeams INT 
SET @topTeams = (SELECT COUNT(*) FROM [TOP TEAMS]) 


--If there are teams in the table, print the number of teams. If there aren't any, print the other statment 
IF @topTeams > 0 
    BEGIN 
     SELECT 'NO. OF TOP TEAMS: ' + CAST(@topTeams AS VARCHAR) 
    END 
ELSE 
    BEGIN 
     SELECT 'NO TOP TEAMS EXIST' 
    END 

是使用TEMP TALBE的相同代码

--Drop the TEMP TABLE if it exists 
IF OBJECT_ID('tempdb..#TOP_TEAMS') IS NOT NULL DROP TABLE #TOP_TEAMS 

--Create the table on the fly with the condition stated 
SELECT 
    TEAM_NAME, 
    RACES_COMPETED 
INTO #TOP_TEAMS 
FROM YourTable 
WHERE RACES_COMPETED >= 500 and RACES_COMPETED <= 900 


--Store the number of teams in the new table in a variable for ease of use 
DECLARE @topTeams INT 
SET @topTeams = (SELECT COUNT(*) FROM #TOP_TEAMS) 


--If there are teams in the table, print the number of teams. If there aren't any, print the other statment 
IF @topTeams > 0 
    BEGIN 
     SELECT 'NO. OF TOP TEAMS: ' + CAST(@topTeams AS VARCHAR) 
    END 
ELSE 
    BEGIN 
     SELECT 'NO TOP TEAMS EXIST' 
    END 
+1

非常感谢。非常感谢 –