select t.CatID , t.parentID , case when exists(select 1 from tb where parentID = t.catid) then 'Y' else 'N' end from tb t
解决方案 »
- SQL2005对表设置主键时提示-超时时间已到
- update TXET字段 并在字段后面增加字符‘aa’的如何写
- 如何查询每个表实际占用多大的磁盘空间
- 直接在SQL软件上建表(不用代码),好像只能设一列不能重复吧!!
- 如果用脚本实现将一个聚集索引改成非聚集索引.表结构和名称随你写.我就看一下语法.谢谢...
- 利用触发器同步更新两个数据库的问题
- 怎样把数据库表中导出的XML文件导入到数据库表中呀?(AdO+SQL SERVER)
- 小弟请各位大侠帮个忙!
- 急!!如何删除用ALTER TABLE 语句的 DEFAULT 创建的具有默认值的字段
- Oracle
- null使用随机的历史数据替换问题请教
- 求SQL语句,谢谢
goupdate t
set t.ifsubid=1
from tb t
where exists(select 1 from tb where parentID=t.CatID)
insert into tb values(1 ,0)
insert into tb values(2 ,0)
insert into tb values(3 ,0)
insert into tb values(5 ,0)
insert into tb values(6 ,1)
insert into tb values(7 ,3)
insert into tb values(8 ,3)
goselect t.CatID , t.parentID ,
case when exists(select 1 from tb where parentID = t.catid) then 'Y' else 'N' end ,
(select count(1) from tb where parentID = t.catid)
from tb tdrop table tb/*CatID parentID
----------- ----------- ---- -----------
1 0 Y 1
2 0 N 0
3 0 Y 2
5 0 N 0
6 1 N 0
7 3 N 0
8 3 N 0(所影响的行数为 7 行)
*/
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([CatID] int,[parentID] int,[catname] varchar(14),[catdesc] int,[isenable] int,[seq] int,[islink] int,[linkurl] varchar(5))
insert [tb]
select 1,0,'政府信息公开35',3335,1,135,0,'33355' union all
select 2,0,'信息公开目录',null,1,2,null,null union all
select 3,0,'近期公开信息',1,3,0,null,null union all
select 5,0,'最后一次测试',null,1,4,null,null union all
select 6,1,'测试父77',6677,0,6677,0,'6777' union all
select 7,3,'测试子专题',10,1,10,1,'aaaa' union all
select 8,3,'子专题2',22333,1,233,1,'2222'
alter table tb add ifsubid int default 0;
goupdate tb set ifsubid=0
goupdate t
set t.ifsubid=1
from tb t
where exists(select 1 from tb where parentID=t.CatID)
select * from tb/**CatID parentID catname catdesc isenable seq islink linkurl ifsubid
----------- ----------- -------------- ----------- ----------- ----------- ----------- ------- -----------
1 0 政府信息公开35 3335 1 135 0 33355 1
2 0 信息公开目录 NULL 1 2 NULL NULL 0
3 0 近期公开信息 1 3 0 NULL NULL 1
5 0 最后一次测试 NULL 1 4 NULL NULL 0
6 1 测试父77 6677 0 6677 0 6777 0
7 3 测试子专题 10 1 10 1 aaaa 0
8 3 子专题2 22333 1 233 1 2222 0(所影响的行数为 7 行)
**/