当前位置: 代码迷 >> Sql Server >> 一条SQL语句,该如何解决
  详细解决方案

一条SQL语句,该如何解决

热度:89   发布时间:2016-04-27 18:01:17.0
一条SQL语句
CREATE PROCEDURE [dbo].[MoveNodeUp] 
  @type_id int
AS
declare @lft int
declare @rgt int
declare @layer int
if exists (select 1 from tree where [email protected]_id)
  begin
  SET XACT_ABORT ON
  BEGIN TRANSACTION
  select @lft=lft,@rgt=rgt,@layer=layer from TreeView where [email protected]_id
  if exists (select * from TreeView where [email protected] and [email protected])
  begin
  declare @brother_lft int
  declare @brother_rgt int
  select @brother_lft=lft,@brother_rgt=rgt from TreeView where [email protected] and [email protected]
  update tree set lft=lft-(@[email protected]_lft+1) where lft>[email protected] and rgt<[email protected]
  update tree set lft=lft+(@[email protected]+1) where lft>[email protected]_lft and rgt<[email protected]_rgt
  update tree set rgt=rgt-(@[email protected]_lft+1) where rgt>@brother_rgt and rgt<[email protected]
  update tree set rgt=rgt+(@[email protected]+1) where lft>[email protected]_lft+(@[email protected]+1) and rgt<[email protected]_rgt
  end
  COMMIT TRANSACTION
  SET XACT_ABORT OFF  
  end
中的select 1 from tree where [email protected]_id这里的 1 是什么意思呢?谢谢大家

------解决方案--------------------
1 跟 * 一样,代表计数.
------解决方案--------------------
exists not exists 实际不返回列表,所以列表部分些什么都可以,写1只是一种习惯

------解决方案--------------------
SQL code
exists (select 1 from tree where [email protected]_id)---这里exists表示存在 所以在后面子查询中只要存在就可以了 所以没必要查询所有字段 只要是常量就可以了
------解决方案--------------------
就你这个具体问题来讲,是说明按照这个条件存在记录。也就是存在给定条件的信息。如果放在整个算法中来讲,则是另外一个层面了。另外一个天地。
------解决方案--------------------
SQL code
--这个你懂不?if exists (select * from tree where [email protected]_id)--用1只是为了减少返回的字段,因为只是判断条件是否成立,所以没必要查具体字段
------解决方案--------------------
HTML code
中的select 1 from tree where [email protected]_id这里的 1 是什么意思呢?谢谢大家
------解决方案--------------------
这是个人习惯..你不喜欢可以用其他的带他...exists 他不考虑字段的属性..你看一不爽你可以查以个 字段...比如表中有id,你就 if not exists(select id from tree where [email protected]_id) 也行
------解决方案--------------------
探讨
SQL code
exists (select 1 from tree where [email protected]_id)


---这里exists表示存在 所以在后面子查询中只要存在就可以了 所以没必要查询所有字段 只要是常量就可以了
  相关解决方案