如何分离SQL查询

时间:2014-10-19 14:05:28

标签: sql sql-server tsql

假设我有以下查询

select userName, password, email 
from tblUsersProfile 
where email = '145' or userName = '112'

select userName, password, email 
from tblUsersProfile 
where email = '80' or userName = '70'

select userName, password, email 
from tblUsersProfile 
where email = '129' or userName = '169'

select userName, password, email 
from tblUsersProfile 
where email = '1' or userName = '2'

select 
    AVG(PokemonLevel) as AvgLevel, PokemonId 
from 
    tblUsersPokemons 
group by 
    PokemonId 
order by 
    PokemonId asc

insert into tblOnlineUsersCounts  
   select 
      (select 
          COUNT(LastMoveTime) 
       from 
          tblUsersProfile 
       where 
          LastMoveTime > DATEADD(HOUR, -24, sysutcdatetime())),  
      (select 
          COUNT(LastMoveTime) 
       from 
          tblUsersProfile 
       where 
          LastMoveTime > DATEADD(DAY, -7, sysutcdatetime())),
      (select 
          COUNT(LastMoveTime) 
       from 
          tblUsersProfile 
       where 
          LastMoveTime > DATEADD(DAY, -30, sysutcdatetime()))

select 
    count(Id) as TotalCount, PokemonId 
from 
    tblUsersPokemons 
group by 
    PokemonId 
order by 
    PokemonId asc

现在,当我在SQL Server Management Studio中执行它们时,它会尝试全部,如果一个失败则全部失败,这意味着所有这些都被计为单个查询

此外,当我右键单击SSMS并单击分析数据库引擎优化顾问中的查询时,它还将它们视为单个查询,而不是每个查询都是独立查询

所以我想以SSMS应该对待它们的方式对它们进行格式化,因为每个SSMS都是一个单独的查询

所以即使一个失败,SSMS也应该执行其他工作

达到此目的的正确格式是什么?

非常非常

这些贬值是什么?

1 个答案:

答案 0 :(得分:3)

如果您的任何查询中出现错误,您应使用TRY-CATCH进行此错误处理。

此外,在您的查询之间使用GO是一种选择。