
用友维护人员常用SQL语句.doc
24页用友维护人员常用 SQL 语句(下)数据库加密:select encrypt('原始密码')select pwdencrypt('原始密码')select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')select pwdencrypt('原始密码')select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同-----------------------------------------------------------------取回表中字段:declare @list varchar(1000),@sql nvarchar(1000) select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表 A'set @sql='select '+right(@list,len(@list)-1)+' from 表 A' exec (@sql)-----------------------------------------------------------------查看硬盘分区:EXEC master..xp_fixeddrives-----------------------------------------------------------------比较 A,B 表是否相等:if (select checksum_agg(binary_checksum(*)) from A)=(select checksum_agg(binary_checksum(*)) from B)print '相等'elseprint '不相等'-----------------------------------------------------------------杀掉所有的事件探察器进程:DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocessesWHERE program_name IN('SQL profiler',N'SQL 事件探查器')EXEC sp_msforeach_worker '?'-----------------------------------------------------------------记录搜索:开头到 N 条记录Select Top N * From 表----------------------N 到 M 条记录(要有主索引 ID)Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID Desc----------------------N 到结尾记录Select Top N * From 表 Order by ID Desc-----------------------------------------------------------------修改数据库的名称:sp_renamedb 'old_name', 'new_name' -----------------------------------------------------------------获取当前数据库中的所有用户表select Name from sysobjects where xtype='u' and status>=0-----------------------------------------------------------------获取某一个表的所有字段select name from syscolumns where id=object_id('表名')-----------------------------------------------------------------查看与某一个表相关的视图、存储过程、函数select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'-----------------------------------------------------------------查看当前数据库中所有存储过程select name as 存储过程名称 from sysobjects where xtype='P'-----------------------------------------------------------------查询用户创建的所有数据库select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')或者select dbid, name AS DB_NAME from master..sysdatabases where sid AND > OR -----------------------------------------------------------------在 group by 一个记录集时,所有的 null 组成一组。
-----------------------------------------------------------------带有 group by 子句的 select 中可以有 where 子句,但是 where 子句必须放在 group by 前面 -----------------------------------------------------------------如果 group by 子句中用了 all,即 group by all xxx ,则不符合检索条件的记录也显示,但不参与统计 -----------------------------------------------------------------having 中,只能包含 group by 子句中 指定的列,也可以包含统计函数where 中可指定任何列,但是不能用统计函数 -----------------------------------------------------------------having 子句从最终结果中将不满足该条件的分组去掉 -----------------------------------------------------------------不带 group by 子句时也可以使用 having 子句,并将整个查询结果作为一个组,但是,由于出现在选择列表中的列 和出现在 having 子句中的列必须是 group by 子句中的列,所以,当不带 group by 子句时,不能在 having 子句和 选择列表中直接使用列名,只能使用统计函数。
-----------------------------------------------------------------当在 group by 子句后指定 order by 子句时,只能在 order by 子句中指定 group by 子句中的列或者统计函数 -----------------------------------------------------------------在进行 union 运算时,自动删除结果中的重复行,如果使用 all 选项 ,则可以将所有行显示在结果中:union all -----------------------------------------------------------------在 union 时,合并结果集中的列名有第一个查询给出,所以后面进行排旬时一定要注意order by 子句中的字段名 -----------------------------------------------------------------可以通过 select fieldslist into 新表名 from 表名,来创建一个新表,并将当前表中的数据全部插入到新 表中,但是做这个操作之前需要保证数据库选项 select into/bulkcopy 设置为 true。
方法如下: use master /*设置命令必须在 master 数据库中进行*/ sp_dboption 数据库 a 名,"select into/bulkcopy",true /*设置数据库选项*/ use 数据库 a 名 checkpoint /*使设置结果生效*/ -----------------------------------------------------------------如果要将统计结果或者计算结果插入到新表中,必须以标题的形式给出列名,如: select a,b=avg(c) into mm from nn group by a -----------------------------------------------------------------随即取出 N 条记录的方法: select top N * from 表名 order by newid() -----------------------------------------------------------------创建唯一约束:create table a(b int not nul constraint 约束名 unique,c char(10) null) 或者:create table a(b int,c char(10),constraint 约束名 unique(b)) -----------------------------------------------------------------为变量赋值方法:set @xxx=??? ,如果变量的值取自一个查询的话,需要用 select, 如:select @xxx=??? from ??? where ???? 如果要返回一个记录集,但是不是从一个表格,而是全部是系统变量或自定义变量组成。
则不必写 from 子句:select ??,??,?? -----------------------------------------------------------------创建一个返回一张表的函数: create function fn_Tree(@Id int) returns table @tb (id int ,fid int) as begin insert @tb select id,fid from tablename where fid=@id while exists (select 1 from tablename where fid in (select id from @tb) and id not in (select id from @tb) ) insert @tb select id,fid from tablename where fid in (select id from @tb) and id not in (select id from @tb) return end 表的字段为 id,fid insert @tb select 语句,将查询结果插。
