⑴ start with ... connect by语句,如何用标准sql实现
大致如下,用withas实现递归查询
(如果数据库不支持withas,只能编程用循环实现):
withsubqry(p_id,c_id)
as
(
selectc_idp_id,c_id
fromtemp
wherec_id=1
unionall
selecttemp.p_id,temp.c_id
fromtemp,subqry
wheresubqry.c_id=temp.p_id
)
selectc_idfromsubqry;
⑵ MS SQL 如何实现Oracle的 Start with...Connect by 这种语句
通用表达式CTE
⑶ sql 语句 startwith 啥意思啊
从 zp 表 perwonID=45 开始 , 这个start with connect by是oracle中 connect by prior 递归算法 你写的___是这个树结构的递归条件,举个例子
select * from test
start with personID=1
connect by prior personID=personID+1 and prior age=age
;
上面的语句查找出了从1开始,并且personID逐渐+1 递增的,并且 age 相同的哪些个数据.
⑷ start with---contect by prior求详解,刚接触此SQL
start with---contect by prior
这个一般是用于做 树型结构查询的。
Oracle 特有的。
http://hi..com/wangqing999/blog/item/6de2443bfa41b6cd7c1e7137.html
⑸ sql 知道父节点,查询所有的子节点,运用游标,递归,存储过程
呃,因为我不清楚你的表里,这个BPROD
char(15)
老爸
里面记录的值是不是和BMWHS
对应的,所以只能说个大概。
select
sys_connect_by_path(username,'>')
"Path"
from
tmbm
start
with
id=1
connect
by
prior
id=parentid;
其中,id是你要遍历的起始点,比如你想从
厂号=XX
的这个父节点开始寻找他所有的子节点,这里就换成
start
with
BMWHS=XX
然后,connect
by
prior
id=parentid,这里id=parentid,简单解释就是寻找其他记录里,parentid和我的id相同的记录,也就是找子节点。
应该是换成你的BMWHS=BPROD(我不知道你的BPROD
BCHLD
和哪个属性是对应的,是BMWHS吗?)
如果是的话就是下面这样(username是你要返回的值,假设你还是要返回
BSEQ
序号)
select
sys_connect_by_path(BSEQ,'>')
"Path"
from
tmbm
start
with
BMWHS=XX
connect
by
prior
BMWHS=BPROD;
⑹ oracle connect by 和 start with 的用法
给你看个例子你就明白了
SQL> SELECT * FROM DEP;
DEPID DEPNAME UPPERDEPID
----------- -------------------------------------------------------------------------------- -----------
0 General Deparment
1 Development 0
2 QA 0
3 Server Development 1
4 Client Development 1
5 TA 2
6 Porject QA 2
--
SQL> SELECT RPAD( ' ', 2*(LEVEL-1), '-' ) || DEPNAME "DEPNAME",
CONNECT_BY_ROOT DEPNAME "ROOT",
CONNECT_BY_ISLEAF "ISLEAF",
LEVEL ,
SYS_CONNECT_BY_PATH(DEPNAME, '/') "PATH"
FROM DEP
START WITH UPPERDEPID IS NULL
CONNECT BY PRIOR DEPID = UPPERDEPID;
DEPNAME ROOT ISLEAF LEVEL PATH
------------------------------ ------------------- ---------- ---------- --------------------------------------------------------------------------------
General Deparment General Deparment 0 1 /General Deparment
-Development General Deparment 0 2 /General Deparment/Development
---Server Development General Deparment 1 3 /General Deparment/Development/Server Development
---Client Development General Deparment 1 3 /General Deparment/Development/Client Development
-QA General Deparment 0 2 /General Deparment/QA
---TA General Deparment 1 3 /General Deparment/QA/TA
---Porject QA General Deparment 1 3 /General Deparment/QA/Porject QA
1. CONNECT_BY_ROOT 返回当前节点的最顶端节点
2. CONNECT_BY_ISLEAF 判断是否为叶子节点,如果这个节点下面有子节点,则不为叶子节点
3. LEVEL 伪列表示节点深度
4. SYS_CONNECT_BY_PATH函数显示详细路径,并用“/”分隔
⑺ start with,connect by prior原理详解
你这个表可以用id 和parent_id这俩字段组出树状结构
start with id = 999 表示从节点999开始
connect by prior id=parent_id 表示999这个节点往上/前 查找树分支
http://jingyan..com/article/5d368d1e182bb93f60c05784.html
⑻ sql如何写
创建之前判断该数据库是否存在
if exists (select * from sysdatabases where name='databaseName')
drop database databaseName
go
Create DATABASE databasename
on primary-- 默认就属于primary文件组,可省略
(
/*--数据文件的具体描述--*/
name=‘databasename_data’,-- 主数据文件的逻辑名称
filename=‘'所存位置:\databasename_data.mdf’, -- 主数据文件的物理名称
size=数值mb, --主数据文件的初始大小
maxsize=数值mb, -- 主数据文件增长的最大值
filegrowth=数值%--主数据文件的增长率
)
log on
(
/*--日志文件的具体描述,各参数含义同上--*/
name='databasename_log', -- 日志文件的逻辑名称
filename='所存目录:\databasename_log.ldf', -- 日志文件的物理名称
size=数值mb, --日志文件的初始大小
filegrowth=数值mb--日志文件的增长值
)
删除数据库
drop database databasename
备份sql server
--- 创建备份数据的 device
USE master
EXEC sp_admpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 开始备份
BACKUP DATABASE pubs TO testBack
创建新表
create table tabname(col1 type1 [not null] [primary key] identity(起始值,递增量)
,col2 type2 [not null],..)--primary key为主键 identity表示递增数量
根据已有的表创建新表:
A:go
use 原数据库名
go
select * into 目的数据库名.dbo.目的表名 from 原表名(使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
创建序列
create sequence SIMON_SEQUENCE
minvalue 1 -- 最小值
maxvalue 999999999999999999999999999 -- 最大值
start with 1 -- 开始值
increment by 1 -- 每次加几
cache 20;
删除表
drop table tabname--这是将表连同表中信息一起删除但是日志文件中会有记录
删除表中信息
delete from tabname-这是将表中信息删除但是会保留这个表
增加一个列
Alter table tabname add colname coltype
删除一个列
Alter table tabname drop column colname
添加主键
Alter table tabname add primary key(col)
说明:删除主键:Alter table tabname drop primary key(col)
创建索引
create [unique] index idxname on tabname(col…。)
删除索引:drop index idxname on tabname
注:索引是不可更改的,想更改必须删除重新建。
创建视图
create view viewname as select statement
删除视图:drop view viewname
基本的sql语句
(1) 数据记录筛选:
sql="select * from 数据表 where字段名=字段值 order by字段名[desc]"
sql="select * from 数据表 where字段名like '%字段值%' order by 字段名 [desc]"
sql="select top 10 * from 数据表 where字段名=字段值 order by 字段名 [desc]"
sql="select top 10 * from 数据表 order by 字段名 [desc]"
sql="select * from 数据表 where字段名in ('值1','值2','值3')"
sql="select * from 数据表 where字段名between 值1 and 值2"
(2) 更新数据记录:
sql="update 数据表 set字段名=字段值 where 条件表达式"
sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
(3) 删除数据记录:
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)
(4) 添加数据记录:
sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"
sql="insert into 目标数据表 select * from 源数据表" (把源数据表的记录添加到目标数据表)
(5) 数据记录统计函数:
AVG(字段名) 得出一个表格栏平均值
COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计
MAX(字段名) 取得一个表格栏最大的值
MIN(字段名) 取得一个表格栏最小的值
SUM(字段名) 把数据栏的值相加
引用以上函数的方法:
sql="select sum(字段名) as 别名 from 数据表 where 条件表达式"
set rs=conn.excute(sql)
用 rs("别名") 获取统计的值,其它函数运用同上。
查询去除重复值:select distinct * from table1
(5) 数据表的建立和删除:
CREATE TABLE 数据表名称(字段1 类型1(长度),字段2 类型2(长度) …… )
(6) 单列求和:
SELECT SUM(字段名) FROM 数据表
编辑本段
最新语句
查询数据库中含有同一这字段的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = 's3')
根据出生日期可以算出年龄:
select datediff(year,scrq,'2013') as 年龄 from page_shsjgrgl
根据当前年份自动算出年龄
select datediff(year,csny,cast(YEAR(GETDATE()) as char))
年
select year(djsj) from page_shsjgrgl
月
select month(djsj) from page_shsjgrgl
日
select day(djsj) from page_shsjgrgl
在同一数据库中复制表结构:
select * into a from b where 1<>1
当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'aa' 中的标识列插入显式值。
set identity_insert aa ON----设置打开,
批量插入:
insert into aa(Customer_ID, ID_Type, ID_Number) select Customer_ID, ID_Type, ID_Number from TCustomer;
set identity_insert aa OFF---关闭
不同数据库之间的复制:
复制结构:
select * into test.dbo.b from GCRT.dbo.page_shsjgrgl where 1<>1
复制内容:
insert into test.dbo.b(xm,ssdq) select xm,ssdq from GCRT.dbo.page_shsjgrgl
查看数据库中所有的数据表表名:
select name from SysObjects where type='u'
查看数据库中所有表含有同一字段的表:
select name from sysobjects where xtype = 'u' and id in(select id from syscolumns where name = '同一字段')
查看数据表中的所有字段:
select name from Syscolumns where id=object_id('表名')
查询数据库时随机10条记录:
select top 10 * from td_areacode order by newid()
修改字段类型:
ALTER TABLE 表名 ALTER COLUMN 字段名 varchar(30) NOT NULL
use ZHJIANGJGYL
declare @temp nvarchar(30)
set @temp = 'ZWI4'
select hllx from page_yljg_zyry where hllx not in(
select
case @temp when ''
then ''
else b1 end
from (
select * from TD_Code where page_en='page_yljg_zyry' and B2='ZWI'
) s where s.b1 !=
case @temp when '' then '' else @temp end
)
更改数据库表字段类型:
alter table page_shsjgrgl alter column s1 int
高级查询
A:UNION 运算符
UNION 运算符通过组合其他两个结果表(例如TABLE1 和TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随UNION 一起使用时(即UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自TABLE1 就是来自TABLE2。
B: EXCEPT 运算符
EXCEPT 运算符通过包括所有在TABLE1 中但不在TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随EXCEPT 一起使用时(EXCEPT ALL),不消除重复行。
C:INTERSECT 运算符
INTERSECT 运算符通过只包括TABLE1 和TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当ALL 随INTERSECT 一起使用时(INTERSECT ALL),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
外连接
A、left outer join:
左外连接(左连接):结果集既包括连接表的匹配行,也包括左连接表的所有行。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B:right outer join:
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
C:full outer join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
编辑本段
判断对象
判断数据库是否存在
if exists (select * from sysdatabases where name= '数据库名')
dropdatabase[数据库名]
判断表是否存在
if not exists (select * from sysobjects where [name] = '表名' and xtype='U')
begin
--这里创建表
end
判断存储过程是否存在
if exists (select * from sysobjects whereid = object_id(N'[存储过程名]') and OBJECTPROPERTY(id, N'IsProcere') = 1)
dropprocere[存储过程名]
判断临时表是否存在
if object_id('tempdb..#临时表名') isnot null
droptable#临时表名
判断视图是否存在
--SQL Server 2000
IF EXISTS (SELECT * FROM sysviews WHEREobject_id = '[dbo].[视图名]'
--SQL Server 2005
IF EXISTS (SELECT * FROM sys.views WHERE object_id = '[dbo].[视图名]'
判断函数是否存在
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[函数名]') and xtype in (N'FN', N'IF', N'TF'))
dropfunction[dbo].[函数名]
获取创建信息
SELECT[name],[id],crdate FROM sysobjects where xtype='U'
/*
xtype 的表示参数类型,通常包括如下这些 C = CHECK约束 D = 默认值或DEFAULT约束 F = FOREIGNKEY约束 L = 日志 FN = 标量函数 IF = 内嵌表函数 P = 存储过程 PK = PRIMARYKEY约束(类型是K) RF = 复制筛选存储过程 S = 系统表 TF = 表函数 TR = 触发器U = 用户表 UQ = UNIQUE约束(类型是K) V = 视图 X = 扩展存储过程 */
判断列是否存在
if exists(select * from syscolumns whereid=object_id('表名') and name='列名')
altertable表名dropcolumn列名
判断列是否自增列
if columnproperty(object_id('table'),'col','IsIdentity')=1
print '自增列'
else
print '不是自增列'
SELECT * FROM sys.columns WHEREobject_id=OBJECT_ID('表名')
AND is_identity=1
判断表中是否存在索引
if exists(select * from sysindexes whereid=object_id('表名') and name='索引名')
print '存在'
else
print '不存在
查看数据库中对象
SELECT * FROM sysobjects WHEREname='对象名'
select * from table(所要查询的表名) where coloum(条件)
编辑本段
提升
复制表
(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1
法二:select top 0 * into b from a
拷贝表
(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(x, y, z) select d,e,f from a;
跨数据库之间表的拷贝
(具体数据使用绝对路径) (Access可用)
insert into b(x, y, z) select d,e,f from a in ‘具体数据库’ where 条件
例子:。.from b in '"&Server.MapPath("."&"\data.mdb" &"' where..
子查询
(表名1:a 表名2:b)
select a,b,c from a where a IN (select d from b 或者: select a,b,c from a where a IN (1,2,3)
显示文章最后时间
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
外连接查询
(表名1:a 表名2:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
在线视图查询
(表名1:a
select * from (Select a,b,c FROM a) T where t.a > 1;
between的用法
between为查询某字段的指定范围,限制查询数据范围时包括了边界值,not between不包括边界值
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2
in 的使用方法
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
删除主表没有的信息
两张关联表delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1
四表联查问题
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....
日程安排提前五分钟
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
一条sql 搞定数据库页
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段= a.主键字段 order by a.排序字段
前10条记录
select top 10 * from table1 where 范围
选择排名
选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等。)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
派生结果表
包括所有在TableA 中但不在TableB和TableC 中的行并消除所有重复行而派生出一个结果表
(select a from tableA except (select a from tableB) except (select a from tableC)
随机取出10条数据
select top 10 * from tablename order by newid()
随机选择记录
select newid()
删除重复记录
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
列出数据库里的表名
select name from sysobjects where type='U'
列出表里的所有的
select name from syscolumns where id=object_id('TableName')
列示排列
列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
显示结果:
type vender pcs
电脑A 1
电脑A 1
光盘B 2
光盘A 2
手机B 3
手机C 3
初始化表table1
TRUNCATE TABLE table1
选择从10到15的记录
select top 5 * from (select top10 * from (select top 15 * from table order by id asc) table_别名 order by id desc) table_2 order by id
数据类型转换
declare @numid int
declare @id varchar(50)
set @numid=2005
set @id=convert(varchar,@numid)
通过上述语句完成数据类型Int转换成varchar,其他转换类似,可参看convert函数
⑼ 高分求一条SQL语句
如果是oracle , 就可以用1个查询语句完成。
用 start with ... connect by 语句。
具体写法是:
select level, branchid , branchname, parentid
from branch
start with branchid = 参数
connect by prior branchid = parentid
这样就可以查询你所给的部门ID以及他的下属所有部门,
将这个结果当作一个表, 用join语句就能实现。
select a.operatorid , a.operatorname, a.branchid ,b.branchname
from operator a , (select level, branchid , branchname, parentid
from branch
start with branchid = 参数
connect by prior branchid = parentid
) b
where a.branchid = b.branchid
如果你要的是ms-sql的话, 据我所知, 是不能用1条查询语句完成的。
你可以用store procere, 用 游标(cursor)和循环来完成你所给的部门以及下所有属部门的查询,并存放到一个临时表里面。
并用这个结果,再用join语句实现, 这个方法比较麻烦。
当初设计表结构和部门代码结构的时候, 你应该实现想到这样的事情,
多加一个字段来表示他的级别, 或者部门代码采用
1000
1100
1101
1102
1200
1201
1202
这种结构, 那么做起来就不这么麻烦了。
⑽ sql start with connect by prior SQL server 哪个版本支持 我用的是sql2008 看着貌似不支持start关键字
用 with .. as实现递归。
withsubqry(id,name,pid) as(
select id,name,pid from test1 where id= 5 --start with
union all
select test1.id,test1.name,test1.pid from test1,subqry
where test1.pid = subqry.id --connect by
)
select* from subqry;