这篇文章主要讲解了“MSSQL基本语法实例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“MSSQL基本语法实例分析”吧!
刷新本地缓存
Ctrl+Shift+R
查询
select *from [table]
修改
1、普通更新
UPDATE [table] set [字段]=[values]
2、关联表更新
UPDATE A SET A1 = B1, A2 = B2, A3 = B3 FROM A, B WHERE A.ID = B.ID
删除(数据)
delete [table]
删除(表)
drop [table]
条件查询
select *from [table] where [字段]=[values]
事务
1 开始事务: begin transaction
2 提交事务:commit transaction
3 回滚事务: rollback transaction
4 结束事务: 提交或回滚事务都将结束事务
begin transactiondeclare @errorSum int --定义变量,用于累计事务执行过程中的错误update bank set currentMoney=currentMoney-800 where customerName='张三'set @errorSum=@errorSum+@@error --累计是否有错误update bank set currentMoney=currentMoney+800 where customerName='李四'set @errorSum=@errorSum+@@error --累计是否有错误print '查看转帐事务过程中的余额'select * from bankif @errorSum>0 begin print '交易失败,回滚事务.' rollback transaction endelse begin print '交易成功,提交事务,写入硬盘,永久保存!' commit transaction endgoprint '查看转帐后的余额'select * from bankgo
查询所有表名
select * from sys.tables
查询所有表包含的字段名
select * from sys.columns
查询所有字段说明
select * from sys.extended_properties
根据表查询所含字段说明
SELECT t.[name] AS 表名,c.[name] AS 字段名,cast(ep.[value]as varchar(100)) AS [字段说明]FROM sys.tables AS tINNER JOIN sys.columns AS c ON t.object_id = c.object_idLEFT JOIN sys.extended_properties AS epON ep.major_id = c.object_id AND ep.minor_id = c.column_id WHERE ep.class =1AND t.name='UTB_GBNT_PROJ_INFO'--------表名
创建视图
create view ViewName(字段1,字段2)as( select 字段1, 字段2 from [table] )
数据字典
SELECT t.FieldExp 名称,t.ColumnName 代码,case t.FieldDataType when 'bigint' then t.FieldDataTypewhen 'int' then t.FieldDataTypewhen 'datetime' then t.FieldDataTypewhen 'numeric' then t.FieldDataTypewhen 'nvarchar' then (cast(t.FieldDataType as varchar(100))+'('+cast(t.Fieldlength as varchar(100))+')')when 'decimal' then (cast(t.FieldDataType as varchar(100))+'('+cast(t.Fieldlength as varchar(100))+','+cast(t.Scale as varchar(100))+')')end as 数据类型,t.Fieldlength 长度,t.Scale 精确度,case t.FieldNullablewhen 0 then '不为空'when 1 then '' end as 说明from (select colorder=C.column_id, FieldExp=ISNULL(PFD.[value],N''), ColumnName=C.name, FieldDataType=T.name, Fieldlength=COLUMNPROPERTY(c.object_id ,c.name ,'PRECISION'), Scale=C.scale,FieldNullable=C.is_nullable--FieldDefVal=D.definitionFROM sys.columns C INNER JOIN sys.objects O ON C.[object_id]=O.[object_id] AND (O.type='U' or O.type='V') AND O.is_ms_shipped=0 INNER JOIN sys.types T ON C.user_type_id=T.user_type_id LEFT JOIN sys.default_constraints D ON C.[object_id]=D.parent_object_id AND C.column_id=D.parent_column_id AND C.default_object_id=D.[object_id] LEFT JOIN sys.extended_properties PFD ON PFD.class=1 AND C.[object_id]=PFD.major_id AND C.column_id=PFD.minor_id LEFT JOIN sys.extended_properties PTB ON PTB.class=1 AND PTB.minor_id=0 AND C.[object_id]=PTB.major_id LEFT JOIN ( SELECT IDXC.[object_id], IDXC.column_id, Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END, PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END, IndexName=IDX.Name FROM sys.indexes IDX INNER JOIN sys.index_columns IDXC ON IDX.[object_id]=IDXC.[object_id] AND IDX.index_id=IDXC.index_id LEFT JOIN sys.key_constraints KC ON IDX.[object_id]=KC.[parent_object_id] AND IDX.index_id=KC.unique_index_id INNER JOIN ( SELECT [object_id], Column_id, index_id=MIN(index_id) FROM sys.index_columns GROUP BY [object_id], Column_id ) IDXCUQ ON IDXC.[object_id]=IDXCUQ.[object_id] AND IDXC.Column_id=IDXCUQ.Column_id AND IDXC.index_id=IDXCUQ.index_id ) IDX ON C.[object_id]=IDX.[object_id] AND C.column_id=IDX.column_id WHERE O.name='--tablename--')as t ORDER BY colorder,ColumnName
SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,字段序号=a.colorder, 字段名=a.name, 标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '√'else '' end, 主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in ( SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid ))) then '√' else '' end, 类型=b.name, 占用字节数=a.length, 长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'), 小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0), 允许空=case when a.isnullable=1 then '√'else '' end, 默认值=isnull(e.text,''), 字段说明=isnull(g.[value],'') FROM syscolumns a left join systypes b on a.xtype=b.xusertype inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' left join syscomments e on a.cdefault=e.id left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id left join sys.extended_properties f on d.id=f.major_id and f.minor_id =0 --where d.name='要查询的表' --如果只查询指定表,加上此条件 order by a.id,a.colorder
感谢各位的阅读,以上就是“MSSQL基本语法实例分析”的内容了,经过本文的学习后,相信大家对MSSQL基本语法实例分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!