1、OR语句可能会破坏索引的作用引发表扫描,可以可以分解成Union语句。例如:
select a.cola,b.colb from taba a inner join tabb b on a.xxx=b.sss or a.ttt=b.yyy
可以转化为
select a.cola,b.colb from taba a inner join tabb b on a.ttt=b.yyy
union all
select a.cola,b.colb from taba a inner join tabb b on a.ttt=b.yyy
不过具体情形还是要看执行计划。
2、
TRUNCATE TABLE
Removes all rows from a table without logging the individual row deletions.
TRUNCATE TABLE is functionally the same as the DELETE statement with no WHERE
clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction
log resources.
TRUNCATE TABLE
[ { database_name.[ schema_name ]. | schema_name . } ]
table_name
[ ; ]
3、SQL中cross join,left join,right join ,full join,inner join 的区别
cross join 是笛卡儿乘积就是一张表的行数乘以另一张表的行数
left join 第一张表的连接列在第二张表中没有匹配是,第二张表中的值返回null
right join 第二张表的连接列在第一张表中没有匹配是,第一张表中的值返回null
full join 返回两张表中的行 left join+right join
inner join 只返回两张表连接列的匹配项
4、SQL中使用cast转化datetime为varchar时会丢失秒,最好使用convert, CONVERT(data_type,expression[,style]),最后那个style变量就是在转换datetime时使用的。
style数字在转换时间时的含义如下
-------------------------------------------------------------------------------------------------
Style(2位表示年份) | Style(4位表示年份) | 输入输出格式
-------------------------------------------------------------------------------------------------
- | 0 or 100 | mon dd yyyy hh:miAM(或PM)
-------------------------------------------------------------------------------------------------
1 | 101 | mm/dd/yy
-------------------------------------------------------------------------------------------------
2 | 102 | yy-mm-dd
-------------------------------------------------------------------------------------------------
3 | 103 | dd/mm/yy
-------------------------------------------------------------------------------------------------
4 | 104 | dd-mm-yy
-------------------------------------------------------------------------------------------------
5 | 105 | dd-mm-yy
-------------------------------------------------------------------------------------------------
6 | 106 | dd mon yy
-------------------------------------------------------------------------------------------------
7 | 107 | mon dd,yy
-------------------------------------------------------------------------------------------------
8 | 108 | hh:mm:ss
-------------------------------------------------------------------------------------------------
- | 9 or 109 | mon dd yyyy hh:mi:ss:mmmmAM(或PM)
-------------------------------------------------------------------------------------------------
10 | 110 | mm-dd-yy
-------------------------------------------------------------------------------------------------
11 | 111 | yy/mm/dd
-------------------------------------------------------------------------------------------------
12 | 112 | yymmdd
-------------------------------------------------------------------------------------------------
- | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
14 | 114 | hh:mi:ss:mmm(24小时制)
-------------------------------------------------------------------------------------------------
- | 20 or 120 | yyyy-mm-dd hh:mi:ss(24小时制)
-------------------------------------------------------------------------------------------------
- | 21 or 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制)
列出所有存储过程
SELECT * FROM SysObjects WHERE [xtype] = 'P'
查询存储过程的内容
EXEC Sp_HelpText '存储过程名'
查询空字符串
SELECT SPACE(空字符个数)
查看某个对象的全部依赖情况
SELECT distinct so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id = so.id
WHERE charindex('Object_Name', text) > 0
Select 赋值
select @counts=rowsCount from #tempRowCount
SQL引擎会遍历查询返回的数据,每条记录赋一次值,@counts的结果是查询返回的最后一条记录的值。
Delete Top
Delete top (1000) from table
可能的话,将表中所有的列都显示的声明为 NOT NULL,并且为丢失的或未知的项定义默认值。
posted on 2007-01-19 15:30
风生水起 阅读(199)
评论(2) 编辑 收藏 网摘 所属分类:
数据库相关