Home Database Mysql Tutorial SQLServer基础语法实例应用(三)

SQLServer基础语法实例应用(三)

Jun 07, 2016 pm 04:20 PM
sqlserver Base Example application grammar

您现在的位置:首页>教程>编程开发>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数据库栏目] / [加入三联文集]

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

See all articles