温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SQL Server常用语句

发布时间:2020-05-23 13:57:11 来源:亿速云 阅读:925 作者:鸽子 栏目:关系型数据库

1.sp_helptext是显示规则、默认值、未加密的存储过程、用户定义函数、触发器或视图的文本。

2.SQL 查询某字段数据所在的表

select a.name as 表名 from sysobjects as a left join syscolumns as b on a.id=b.id where b.name='字段名'

1>根据已知字段查询表:

select a.name from sysobjects a join syscolumns b on a.id=b.id where b.name='字段名'

2>查询符合此字段值的记录:

select * from 表名 where 字段名=字段值 (表名是步骤一查询出来的名称)

3.获取当前数据库中的所有用户表
select Name from sysobjects where xtype='u' and status>=0

4.列出数据库里所有的表名
select name from sysobjects where type='U'

5.获取某一个表的所有字段
select name from syscolumns where id=object_id('表名')

select name from syscolumns where id in (select id from sysobjects where type = 'u' and name = '表名')

两种方式的效果相同

6.查询某一个表的字段和数据类型
select column_name,data_type from information_schema.columns
where table_name = '表名'

7.取回表中字段:
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)

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='V'

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 name='sa')
或者
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01

12.1=1,1=2的使用,在SQL语句组合时用的较多

“where 1=1” 是表示选择全部    “where 1=2”全部不选,
如:
if @strWhere !=''
begin
set @strSQL = 'select count() as Total from [' + @tblName + '] where ' + @strWhere
end
else
begin
set @strSQL = 'select count(
) as Total from [' + @tblName + ']'
end

我们可以直接写成

错误!未找到目录项。
set @strSQL = 'select count(*) as Total from [' + @tblName + '] where 1=1 安定 '+ @strWhere

13.包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)

14.删除重复记录
1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
2),select distinct into temp from tablename
delete from tablename
insert into tablename select
from temp
评价: 这种操作牵连大量的数据的移动,这种做法不适合大容量但数据操作
3),例如:在一个外部表中导入数据,由于某些原因第一次只导入了一部分,但很难判断具体位置,这样只有在下一次全部导入,这样也就产生好多重复的字段,怎样删除重复字段

alter table tablename
--添加一个自增列
add  column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b)  from tablename group by column1,column2,...)
alter table tablename drop column column_b

15.SQL两表之间:根据一个表的字段更新另一个表的字段

  1. 写法轻松,更新效率高:

update table1

set field1=table2.field1,

field2=table2.field2

from table2

where table1.id=table2.id

  1. 常规方式,种写法相当于一个 Left join, 以外面的where为更新条数,如果不加where就是所有记录

update table1

set field1=(select top 1 field1 from table2 where table2.id=table1.id)

where table1.id in (condition)

16.比较A,B表是否相等:
if (select checksum_agg(binary_checksum(*)) from A)

(select checksum_agg(binary_checksum(*)) from B)

print '相等'
else
print '不相等'

17.请求其空间使用信息的表、索引视图或队列的限定或非限定名称

exec sp_spaceused 'tablename'

18.查看硬盘分区:
EXEC master..xp_fixeddrives

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI