本文主要是介绍MS-SQL server数据库开发精典技巧,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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)
2.查看硬盘分区:
EXEC master..xp_fixeddrives
3.比较A,B表是否相等:
if (select checksum_agg(binary_checksum(*)) from A)
=
(select checksum_agg(binary_checksum(*)) from B)
print '相等'
else
print '不相等'
4.记录搜索:
开头到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
5.如何修改数据库的名称:
sp_renamedb 'old_name', 'new_name'
6:获取当前数据库中的所有用户表
select Name from sysobjects where xtype='u' and status>=0
7:获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')
8:查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
9:查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='P'
10:查询用户创建的所有数据库
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
11:查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'
[n].[标题]:
Select * From TableName Order By CustomerName
[n].[标题]:
Select * From TableName Order By CustomerName
这篇关于MS-SQL server数据库开发精典技巧的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!