MSSQL数据库操作使用语句
1.按姓氏笔画排序:
Select * from TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.分页SQL语句
select * from(select (row_number() OVER (ORDER BY tab.ID Desc)) as rownum,tab.* from 表名 As tab) As t where rownum between 起始位置 And 结束位置
3.获取当前数据库中的所有用户表
select * from sysobjects where xtype='U' and category=0
4.获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')
5.查看与某一个表相关的视图、存储过程、函数
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
6.查看当前数据库中所有存储过程
select name as 存储过程名称 from sysobjects where xtype='P'
7.查询用户创建的所有数据库
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
8.查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'
9.使用事务
在使用一些对数据库表的临时的SQL语句操作时,可以采用SQL SERVER事务处理,防止对数据操作后发现误操作问题
开始事务
Begin tran
Insert Into TableName Values(…)
SQL语句操作不正常,则回滚事务。
回滚事务
Rollback tran
SQL语句操作正常,则提交事务,数据提交至数据库。
提交事务
Commit tran
10. 按全文匹配方式查询
字段名 LIKE N'%[^a-zA-Z0-9]China[^a-zA-Z0-9]%'
OR 字段名 LIKE N'%[^a-zA-Z0-9]China'
OR 字段名 LIKE N'China[^a-zA-Z0-9]%'
OR 字段名 LIKE N'China
11.计算执行SQL语句查询时间
declare @d datetime
set @d=getdate()
select * from SYS_ColumnProperties select [语句执行花费时间(毫秒)]=datediff(ms,@d,getdate())
12、说明:几个高级查询运算词
A: UNION 运算符
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TA
相关文档:
当数据服务器和Web服务器部署在不同的服务器上时,会用到分布式事务,需要对两个服务器的MSDTC进行配置。
打开“管理工具――组件服务”,以此打开“组件服务――计算机”,在“我的电脑”上点击右键。在MSDTC选项卡中,点击“安全配置”按钮。 ......
今天在聊天系统中需要系统执行一个多层嵌套查询。
一开始语句如下总出现错误:原来是在]='123') 后我多加了一个 as tb1
改为如下后,正确运行。
select * from ( select top(10) * from ( select top(100) * from (select [chatcontent].[senderid],[chatcontent].[id] ,[chatcontent].[toid] ,[chatc ......
1.
--将汉字转换为拼音首字母
CREATE function GetAllPY(@str nvarchar(4000))
returns nvarchar(4000)
--WITH ENCRYPTION
as
begin
declare @intLen int
declare @strRet nvarchar(4000)
declare @temp nvarchar(100)
set @intLen &nb ......
How is MSSQL using memory?
http://www.sqlhacks.com/Administration/Memory-Usage
Memory is one the most important factor affecting MSSQL performance.
As an administrator, you should be monitoring the memory
regularly. When Microsoft SQL Server runs out of memory, it will use
virtual memory: ie: ......
--此代码实现SQL数据库远程备份,放到作业里面执行可以自动备份数据库、自动删除@keepNDays天前备份。
--此代码将本地所有的用户数据库备份到共享目录“\\backupServerIp\ShareName\数据库备份”下。
--并删除天前的备份文件。要备份成功必须能够对共享目录有操作权限!
sp_configure 'xp_cmdshell',1 ......