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 TABLE3、SQL中cross join,left join,right join ,full join,inner join 的区别
[ { database_name.[ schema_name ]. | schema_name . } ]
table_name
[ ; ]
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,并且为丢失的或未知的项定义默认值。
取整函数:
上取整ceiling() 例如:ceiling(4.1) 返回值为5
下取整floor() 例如: floor(4.9) 返回值4
Get Table Space Status: sp_spaceused @TableName
Grant/Revoke
Attach DB By Script(you can name for the new DB):
CREATE DATABASE ASGDMbck ON
( FILENAME = N'G:\Data\ASGDM.mdf' ),
( FILENAME = N'G:\Data\ASGDM_1.ndf'),
( FILENAME = N'G:\Data\ASGDM_2.ldf')
FOR ATTACH
你不能在进程中看到SQLServer实际占得内存,需要到性能查看器中去看。
Find out which procedures are set to execute as specific user :
select object_name(object_id) from sys.sql_modules where execute_as_principal_id = user_id('alice')
Measuring the Run Time of Queries
DBCC FREEPROCCACHE;
SET Statistics IO on
set statistics time on
exec spRptPonToolVerticalData_Adc2
@VerticalKey='194',
@Sites='bing.com',
@StartDate='Mar 1 2010 12:00AM',
@EndDate='May 30 2010 12:00AM',
@DateType='D',
@Alias='admin',
@ActionEntry='Ribbon'
set statistics time off
SET Statistics IO off