Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

sqserver目录下产生大量的dump故障转储文件,磁盘空间减少,系统比较慢。

分析log知,有一个错误,msdb。
运行dbcc checkdb('msdb'),发现sysjobhistory索引错误
重建索引:alter index clust on sysjobhistory rebuild
重新运行dbcc checkdb('msdb'),仍不解决
因job比较少,直接删除此索引解决。 drop index nc1 on sysjobhistory

参考: SqlServer上的 *.mdmp文件


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

查看日志发现是系统文件损坏,master库

重建master库

F:/Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=MSSQLSERVER /SQLSYSADMINACCOUNTS=Administrators /SAPWD=xxx /SQLCOLLATION=Chinese_PRC_CI_AS

sapwd是密码

重建后重新加载各用户库


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

CREATE FUNCTION [dbo].[UNIX_TIMESTAMP]
(
    @DATE DATETIME=''
)
       
RETURNS DECIMAL
AS
BEGIN
  --注意时区,北京时间统一减8变成格林尼治时间
   IF @DATE='' SET @DATE=GETDATE()
  SET @DATE=DATEADD(HOUR,-8,@DATE)
   RETURN DATEDIFF(SECOND,'1970-01-01',@DATE)
      
END

CREATE FUNCTION [dbo].[FROM_UNIXTIME]
(
    @i DECIMAL
)
       
RETURNS VARCHAR(20)
AS
BEGIN
 RETURN CONVERT(VARCHAR(20),DATEADD(SECOND,@i+28800,'1970-01-01'),120) --北京时间要加回8*3600=28800秒
END

引自:MSSQL(SQL SERVER)中获取UNIX时间戳


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

在操作中,有时要用几个数据造成一查询表处理数据,通常的做法是select union all,现提供另一种方法:

SELECT *
FROM (VALUES (1,2),(3,4),(5,6)) AS t(a,b)

相当于
SELECT *
FROM(
 SELECT 1 a, 2 b
  UNION ALL   
  SELECT 3 a,4 b
  UNION ALL   
  SELECT 5 a,6 b

)t


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
--设置周一为第一天,默认为周日为第一天
SET DATEFIRST 1

--获取本周是今年的第几个星期
SELECT DATEPART(week,GETDATE())

--今天是第几个季度
SELECT DATEPART(quarter,GETDATE())

--按周统计数据,要求显示每周开头与结尾时间
declare @num int,@year varchar(4),@date datetime
select @num=45
select @year='2008-01-01'
select @date=dateadd(wk,@num-1,@year)
select dateadd(dd,1-datepart(dw,@date),@date),dateadd(dd,7-datepart(dw,@date),@date)