SQL声明大全

1.随机选择3记录

    select top 3 * from tablename newid()

2.随机选记录

    select newid().

3.删除反复记录

1) delete from tablename where
id not in (select max(id) from tablename group by col1,col2,...)

2)select distinct * into temp fromtablename

delete fromtablename

       insert into tablename select * from temp

4.列出数据库里全部的表名

select name from sysobjects where type='U'

5.列出表里的全部的列名

select name from syscolumns where id=object_id('TableName')

6.选择从10到15的记录

select top 5 * from(select top 15 * from tablename order by id asc) table_别名 order by id desc

7.压缩数据库

dbcc shrinkdatabase(dbname)

8.按姓氏笔画排序

Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as //从少到多

9.数据库加密

select encrypt('原始password')

    select pwdencrypt('原始password')

    select pwdcompare('原始password','加密后password') = 1--同样;否则不同样 encrypt('原始password')

    select pwdencrypt('原始password')

    select pwdcompare('原始password','加密后password') = 1--同样;否则不同样

10.查看硬盘分区

EXEC master..xp_fixeddrives

11.比較两表是否相等

if(select checksum_agg(binary_checksum(*))from A)=(select checksum_agg(binary_checksum(*))from B)

       print '相等'

    else

       print '不相等'

12.杀掉全部的事件探察器进程:

    DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocesses

    WHERE program_name IN('SQL profiler',N'SQL 事件探查器')

    EXEC sp_msforeach_worker '?

'

13.获取当前数据库中的全部用户表

    select Name from sysobjects where xtype='u' and status>=0

14.查看与某一个表相关的视图、存储过程、函数

    select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'

15.查看当前数据库中全部存储过程

    select name as 存储过程名称 from sysobjects where xtype='P'

16.查询用户创建的全部数据库

    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 <> 0x01

17.创建数据库

create database dbname

18.删除数据库

drop database dbname

19.备份sql server

    --- 创建 备份数据的 device

    USE master

    EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'

    --- 開始 备份

    BACKUP DATABASE pubs TO testBack

20.批量替换某一字段中的某一keyword

update S set Sname=Replace(Sname,'张','刘')

21.从一个表中选择一个字段,插入到还有一个表的指定字段中

insert into S (Sname)select distinct Ename from Expert

版权声明:本文博主原创文章。博客,未经同意不得转载。

上一篇:关于JavaScript打印去掉页眉页脚


下一篇:css实现div水平垂直居中