2014-10-19 97 views
-3

假设我有以下疑问我怎么能单独的SQL查询

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应该执行其它工作的人

什么是实现这一目标的正确格式?

Ty非常多

什么是这些downvotes为?

+0

您应该使用'TRY'和'CATCH'进行错误处理。其他人可能会建议在你的陈述之间使用“GO”。 – 2014-10-19 14:07:33

+0

@RaduGheorghiu例如,当我捕获这个查询在分析器它被视为一个单一的批处理查询,而不是每个人都是独立的查询 – MonsterMMORPG 2014-10-19 14:09:27

+0

@RaduGheorghiu去看起来像工作非常多:) – MonsterMMORPG 2014-10-19 14:10:16

回答

3

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

另外,在您的查询之间使用GO是一个选项。

+0

TY答案去似乎是在做伎俩。这背后的逻辑是什么?你能解释一下吗? – MonsterMMORPG 2014-10-19 14:12:16

+0

@MonsterMMORPG就像你在你的问题中所说的那样,如果你查看Profiler,它看起来好像是一批查询。通过使用'GO',你实际上可以分多次查询。所以,每个查询都是独立运行的,独立于其他查询。您可以阅读MSDN上GO和TRY-CATCH的文档以获取更多详细信息。 – 2014-10-19 14:14:13

+0

我总是标记为解决,但我没有得到为什么得到downvotes – MonsterMMORPG 2014-10-19 18:08:19