Toad-for-SQL-Server-Freeware
1.查询哪些数据库对象使用了某个表
SELECT b.[name], a.[definition] FROM sys.all_sql_modules a, sysobjects b WHERE a.[object_id] = id AND definition LIKE '%表名%'
2.查询表的某一列,将结果变成用逗号分隔的字符串
select col+',' from mytable for xml path('')
排序:
create table pd(col1 varchar(5),col2 int) insert into pd select 'A',3 union all select 'A',2 union all select 'A',1 union all select 'B',2 union all select 'B',1 select a.col1, stuff((select ','+rtrim(b.col2) from pd b where b.col1=a.col1 order by b.col2 for xml path('')),1,1,'') 'col2' from pd a group by a.col1
3.查询有哪些表的表名包含“storeroom”
select * from sysobjects obj where LOWER(obj.name) LIKE N'%storeroom%' and xtype='U'
4.分组条件求和
DECLARE @t1 TABLE ( c1 NUMERIC (12), c2 VARCHAR (30) ) INSERT INTO @t1 (c1, c2) VALUES (1, 'a'); INSERT INTO @t1 (c1, c2) VALUES (2, 'a'); INSERT INTO @t1 (c1, c2) VALUES (3, 'b'); INSERT INTO @t1 (c1, c2) VALUES (4, 'b'); SELECT CASE WHEN max (c1) > 3 THEN sum (c1) ELSE 0 END AS c FROM @t1 GROUP BY c2; /* 结果: c 0 7 */
5.求某一天所在星期的周日
http://www.cnblogs.com/wsdj-ITtech/archive/2011/10/06/2199736.html
USE [MSSQL] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[My_OneDay_GetWeekFirstAndEndDay](@tmpDate DATETIME) RETURNS @tmpTable TABLE(FirstDay DATETIME , EndDay DATETIME) AS BEGIN INSERT INTO @tmpTable SELECT a.FirstDay,b.EndDay FROM ( SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy ) a LEFT JOIN ( SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay ) b ON a.ID = b.ID RETURN END
SELECT * from My_OneDay_GetWeekFirstAndEndDay('2010-09-01')
6.求时间段内周日的数量
http://www.cnblogs.com/wsdj-ITtech/archive/2011/10/06/2199736.html
CREATE FUNCTION [dbo].[MY_Range_GetWeekFirstAndEndDays](@tmpDateSTART DATETIME,@tmpDateEND DATETIME) RETURNS @tmpTable TABLE(WeekOrder INT,FirstDay DATETIME , EndDay DATETIME) AS BEGIN DECLARE @tmpDate DATETIME DECLARE @index INT SET @tmpDate=@tmpDateSTART SET @index=1 WHILE @tmpDate <=@tmpDateEND BEGIN INSERT INTO @tmpTable SELECT @index,a.FirstDay,b.EndDay FROM ( SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 0) AS FirstDAy) a LEFT JOIN ( SELECT 1 AS ID,DATEADD(wk, DATEDIFF(wk,0,@tmpDate), 6) AS EndDay) b ON a.ID = b.ID SET @tmpDate=DATEADD(DAY,7,@tmpDate) SET @index=@index+1 END RETURN END
SELECT * from My_Range_GetWeekFirstAndEndDays('2011-09-01','2011-10-06')
不使用临时表:
DECLARE @tmpDateSTART DATETIME DECLARE @tmpDateEND DATETIME SET @tmpDateSTART = '2015-1-1' SET @tmpDateEND = '2015-1-21' DECLARE @tmpDate DATETIME DECLARE @days INT SET @tmpDate = @tmpDateSTART SET @days = 0 WHILE @tmpDate <= @tmpDateEND BEGIN DECLARE @theDate DATETIME; SET @theDate = DATEADD (wk, DATEDIFF (wk, 0, @tmpDate), 6); IF @theDate > @tmpDateSTART AND @theDate < @tmpDateEND SET @days = @days + 1 SET @tmpDate = DATEADD (DAY, 7, @tmpDate) END SELECT @days
7.查看正在执行的
SELECT [Spid] = session_Id, ecid, [Database] = DB_NAME (sp.dbid), [User] = nt_username, [Status] = er.status, [Wait] = wait_type, [Individual Query] = SUBSTRING ( qt.text, er.statement_start_offset / 2, ( CASE WHEN er.statement_end_offset = -1 THEN LEN (CONVERT (NVARCHAR (MAX), qt.text)) * 2 ELSE er.statement_end_offset END - er.statement_start_offset) / 2), [Parent Query] = qt.text, Program = program_name, Hostname, nt_domain, start_time, datediff (second, start_time, getdate ()) FROM sys.dm_exec_requests er INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid CROSS APPLY sys.dm_exec_sql_text (er.sql_handle) AS qt WHERE session_Id > 50 -- Ignore system spids. AND session_Id NOT IN (@@SPID) -- Ignore this current statement. ORDER BY datediff (second, start_time, getdate ()) DESC
8.生成GUID
C#中用Guid.NewGuid().ToString()
Sql中用NEWID()
以上方法生成的是36位的GUID,如果需要转换成32位,则需要替换掉其中的'-'字符。
Sql中的方法:replace(newid(), '-', '')
9.时间段统计
--将时间转换为小时 --例如8:22分转换为9 --查询语句如下 SELECT cast ( datepart ( hh, dateadd ( mi, ( datediff (mi, CONVERT (VARCHAR (10), getdate (), 112), getdate ()) / 60 + 1) * 60, CONVERT (VARCHAR (10), getdate (), 112))) AS INT)
http://bbs.csdn.net/topics/190127317 --环境 declare @t table ( 时间 datetime, 金额 int ) insert @t select '2007-1-1 10:00:23', 8 union all select '2007-1-1 10:01:24', 4 union all select '2007-1-1 10:05:00', 2 union all select '2007-1-1 10:06:12', 3 union all select '2007-1-1 10:08:00', 1 union all select '2007-1-1 10:12:11', 5 select dateadd(mi,(datediff(mi,convert(varchar(10),时间,112),时间)/5+1)*5,convert(varchar(10),时间,112)) as 时间段, count(*) as 行数,sum(金额) as 总金额 from @t group by dateadd(mi,(datediff(mi,convert(varchar(10),时间,112),时间)/5+1)*5,convert(varchar(10),时间,112)) --结果 时间段 行数 总金额 ------------------------------------------------------ ----------- ----------- 2007-01-01 10:05:00.000 2 12 2007-01-01 10:10:00.000 3 6 2007-01-01 10:15:00.000 1 5 (所影响的行数为 3 行)
10.存储过程,有时执行很慢
现在的解决办法是,将存储过程中加个空格,alter一下。
exec 存储过程 with recompile
http://bbs.csdn.net/topics/340185343
http://my.oschina.net/HenuToater/blog/177175
http://havebb.com/b/post/produce-suddenly-slow.aspx
http://www.cnblogs.com/luckylei66/archive/2012/07/30/2615000.html
SQL优化之存储过程强制编译 ASP.NET调用SQL后台存储过程时,有时突然就变得很慢,在后台直接执行存储过程没问题,但在前台调用存储过程时就是很慢,而且在前台调用成功后,再次调用还是一样的慢,但更新一下存储过程再调用就很快了。但这始终不能彻底解决问题,过段时间又会出来同样的问题。环境(NET4.0+SQL2008R2) 解决办法: 方法一:在可能比较耗时的语句后面加上option(recompile) 方法二:强制编译存储过程 SQL Server 提供三种重新编译存储过程的方法: (1)、sp_recompile 系统存储过程强制在下次运行存储过程时进行重新编译。 示例:exec sp_recompile 存储过程名 (2)、创建存储过程时在其定义中指定 WITH RECOMPILE 选项,表明 SQL Server 将不对该存储过程计划进行高速缓存;该存储过程将在每次执行时都重新编译。 示例:Create Proc 存储过程名 WITH RECOMPILE AS 参数 (3)、在执行存储过程时指定 WITH RECOMPILE 选项,可强制对存储过程进行重新编译。仅当所提供的参数不典型,或者自创建该存储过程后数据发生显著更改时才应使用此选项。 示例:存储过程名 WITH RECOMPILE