您现在的位置:首页>教程>编程开发>mssql数据库 > sqlserver基础语法实例应用(三) sqlserver基础语法实例应用(三) 感谢 3lian8 的投递 时间:2014-03-27 来源:三联教程 三、开发应用 1.按姓氏笔画排序: select * from tablename order by customername
您现在的位置:首页 > 教程 > 编程开发 > mssql数据库 > sqlserver基础语法实例应用(三)
sqlserver基础语法实例应用(三)
感谢 3lian8 的投递 时间:2014-03-27 来源:三联教程
三、开发应用
1.按姓氏笔画排序:
select * from tablename order by customername collate chinese_prc_stroke_ci_as //从少到多
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[name] varchar(6))
insert [a]
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五' union all
select 4,'赵六' union all
select 5,'孙七'
-->查询语句(按姓氏笔画排序)
select * from a order by [name] collate chinese_prc_stroke_ci_as
/*
id name
----------- ------
3 王五
5 孙七
1 张三
2 李四
4 赵六
(5 行受影响)
*/
2.数据库加密:
select encrypt('原始密码')
select pwdencrypt('原始密码')
select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')
select pwdencrypt('原始密码')
select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同
3.取回表中字段:
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)
4.查看硬盘分区:
exec master..xp_fixeddrives
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
exec master..xp_fixeddrives
/*
drive mb 可用空间
----- -----------
c 168
d 130379
e 57714
(3 行受影响)
*/
5.比较a,b表是否相等:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[name] varchar(6))
insert [a]
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五' union all
select 4,'赵六' union all
select 5,'孙七'
-->测试数据[b]
select * into [b] from [a]
if (select checksum_agg(binary_checksum(*)) from a)
=
(select checksum_agg(binary_checksum(*)) from b)
print '相等'
else
print '不相等'
/*
相等
*/
6.记录搜索:
开头到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
7:获取当前数据库中的所有用户表
select name from sysobjects where xtype='u' and status>=0
8:获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')
select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
select name from syscolumns where id=object_id('a')
select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = 'a')
/*
name
----------------------------------
id
name
(2 行受影响)
name
-----------------------------------
id
name
(2 行受影响)
*/
两种方式的效果相同
9:查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
10:查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='p'
11:查询用户创建的所有数据库
select * from master..sysdatabases d where sid not in(select sid from master..syslogins where)
或者
select dbid, name as db_name from master..sysdatabases where sid <> 0x01
12:查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'
13:不同服务器数据库之间的数据操作
--创建链接服务器
exec sp_addlinkedserver 'itsv ', ' ', 'sqloledb ', '远程服务器名或ip地址 '
exec sp_addlinkedsrvlogin 'itsv ', 'false ',null, '用户名 ', '密码 '
--查询示例
select * from itsv.数据库名.dbo.表名
--导入示例
select * into 表 from itsv.数据库名.dbo.表名
--以后不再使用时删除链接服务器
exec sp_dropserver 'itsv ', 'droplogins '
--连接远程/局域网数据(openrowset/openquery/opendatasource)
14、openrowset
--查询示例
select * from openrowset( 'sqloledb ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
--生成本地表
select * into 表 from openrowset( 'sqloledb ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
--把本地表导入远程表
insert openrowset( 'sqloledb ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)
select *from 本地表
--更新本地表
update b
set b.列a=a.列a
from openrowset( 'sqloledb ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b
on a.column1=b.column1
--openquery用法需要创建一个连接
--首先创建一个连接创建链接服务器
exec sp_addlinkedserver 'itsv ', ' ', 'sqloledb ', '远程服务器名或ip地址 '
--查询
select *
from openquery(itsv, 'select * from 数据库.dbo.表名 ')
--把本地表导入远程表
insert openquery(itsv, 'select * from 数据库.dbo.表名 ')
select * from 本地表
--更新本地表
update b
set b.列b=a.列b
from openquery(itsv, 'select * from 数据库.dbo.表名 ') as a
inner join 本地表 b on a.列a=b.列a
相关文章
标签:
[返回三联首页] [返回mssql数据库栏目] / [加入三联文集]
,【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!