SQL递归查询部门
create function [dbo].[DeptTree](@initDeptCode varchar(10))/*定义函数c_tree,输入参数为初始节点DeptCode*/
returns @t table(DeptCode varchar(10),UpDeptCode varchar(10),lev int)/*定义表t用来存放取出的数据*/
begin
declare @i int/*标志递归级别*/
set @i=1
insert @t select DeptCode,UpDeptCode,@i from vi_dept where DeptCode=@initDeptCode
while @@rowcount<>0
begin
set @i=@i+1
insert @t select a.DeptCode,a.UpDeptCode,@i from vi_dept as a,@t as b
where b.DeptCode=a.UpDeptCode and b.lev=@i-1
end
return
end
select * from dbo.DeptTree('0000')
相关文档:
---返回表达式中指定字符的开始位置
select charindex('c','abcdefg',1)
---两个字符的值之差
select difference('bet','bit')
---字符最左侧指定数目
select left('abcdef',3)
---返回字符数
select len('abcdefg')
--转换为小字符
select lower('ABCDEFG')
--去左空格后
select ltrim(' &nbs ......
行列转换
create table test(id int,name varchar(20),quarter int,profile int)
insert into test values(1,'a',1,1000)
insert into test values(1,'a',2,2000)
insert into test values(1,'a',3,4000)
insert into test values(1,'a',4,5000)
insert into test values(2,'b',1,3000)
insert into test values(2, ......
UNION指令的目的是将两个SQL语句的结果合并起来。从这个角度来看, 我们会产生这样的感觉,UNION跟JOIN似乎有些许类似,因为这两个指令都可以由多个表格中撷取资料。 UNION的一个限制是两个 SQL 语句所产生的栏位需要是同样的资料种类。另外,当我们用 UNION这个指令时,我们只会看到不同的资料值 (类似 SELECT DISTINCT) ......
What Is SQL?
SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a language designed specifically for communicating with databases.
Unlike other languages (spoken languages like English, or programming languages like Java or Visual Basic), SQ ......