Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
sqlserver提供两种类型geometry(平面几何),geography(球面)用来存储地图数据。
有人用此竟画出了图形,效果见下图,sql
sqlserver提供两种类型geometry(平面几何),geography(球面)用来存储地图数据。
有人用此竟画出了图形,效果见下图,sql
1,报表生成器:report builder:提供不具程序基础的使用者通过简易图形化开发界面就能达到数据查询与报表的建立。
2,BIDS:目标是专业的开发人员。
3,数据区域:
4,共享数据源:一个报表可一个数据源(内嵌数据源),也可多个报表一个数据源,即共享数据源。
5,报表结构:页眉,页脚,主体及数据。
6,Tablix:数据表(table)+矩阵(matrix),在报表上放置数据源后,就会出现。
7,行组:可认为sql中的group。
8,数据显示格式,在字段的format属性中修改,对应的有:
引自BOL
下表列出了常见的 .NET Framework 数字格式设置字符串。
格式字符串 | 名称 |
---|---|
C 或 c |
货币 |
D 或 d |
十进制 |
E 或 e |
科学型 |
F 或 f |
固定点 |
G 或 g |
常规 |
N 或 n |
数字 |
P 或 p |
百分比 |
R 或 r |
往返 |
X 或 x |
十六进制 |
可以对许多格式字符串进行修改,以包括用于定义小数点右侧位数的精度说明符。例如,格式设置字符串 D0 将数字格式设置为小数点后面的位数为零。您还可以使用自定义格式字符串,例如 #,###。
下表列出了常见的 .NET Framework 日期格式设置字符串。
格式字符串 | 名称 |
---|---|
d |
短日期 |
D |
长日期 |
t |
短时间 |
T |
长时间 |
f |
完整日期/时间(短时间) |
F |
完整日期/时间(长时间) |
g |
常规日期/时间(短时间) |
G |
常规日期/时间(长时间) |
M 或 m |
月日 |
R 或 r |
RFC1123 模式 |
Y 或 y |
年月 |
9,交互式报表
10,图表、微图形与仪表
11,部署
12 订阅
13 权限认证方式:windows,或表单或企业内部自定义。
14,内部系统接入,URL传参方式,可生成html,excel,word,pdf等格式
15,可在webfrom,windowsfrom项目中引入报表进行开发。
SQLServer
BI的核心,其中从下到上包括三个部分,SSIS,SSAS,SSRS
SSIS负责ETL以及整体BI的调度。图形话界面比较直观。
SSAS,分析服务,包括Cube和数据挖掘。它也是跟我们通常所见的表和库相同的另外一种独立的库。
SSRS,报表,包括订阅和发布等功能,最新的版本集成了dundas的一些东西,比之前效果好那么一点。
以上三个模块的开发都是通过visual studio shell。
比较常见的方案:
Windows Server
IIS
SQLServer->SSAS
ETL层自定义框架
前端利用第三方组件自行开发
优点,ETL和UI自己开发,可以解决比较复杂的需求。相对来说对于UI层差别很大,比如据说微软内部很多部门就是自己用Excel去连数据。
缺点,开发维护的成本高。
aaaaaa
ALTER TABLE frmuser_0315 ADD useralias AS (account+'.net') PERSISTED 计算列可用于: 1,对于复杂的计算自动保存值 2,复杂查询利用计算列checksum取值,避免like %% 注:默认是不持久的,即列并不物理存在,只是逻辑上的,不能建立索引。 指定在指定列中添加或删除 PERSISTED 属性。 该列必须是由确定性表达式定义的计算列。 对于指定为 PERSISTED 的列,数据库引擎将以物理方式在表中存储计算值;并且,当更新了计算列依赖的任何其他列时,这些值也将被更新。 通过将计算列标记为 PERSISTED,可以对确定(但不精确)的表达式中定义的计算列创建索引。 相关视图: SELECT TOP 50 * FROM sys.computed_columns
Declare @keytosearch varchar(max), @Database_Selected varchar(50) set @keytosearch ='%london%' set @Database_Selected= 'Northwind' Declare @Table varchar(100), @Table_Name Cursor, @Count_Column int, @Result nvarchar(4000),@ID int,@ID_inserted int,@Count_Table int, @data_type varchar(10) set @ID_inserted=0 set @Count_Table=0 DECLARE @column varchar(max), @Column_Name CURSOR --Variable Delaration end --Second Cursor start declare @informationName varchar(50), @SysName varchar(50), @Var varchar(5) set @informationName=@Database_Selected+'.'+'information_schema.COLUMNS' Set @SysName=@Database_Selected+'.'+'sys.objects' Set @Var='u' --Database Selected start Create Table #SearchResult(ID int identity(1,1) not null, TableName varchar(50), ColumnName varchar(50)) Create Table #SearchTestResult(ID int identity(1,1) not null, ColumnName varchar(max)) Create Table #Table_Name(ID int identity(1,1) not null, table_name varchar(50)) IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='Column_Nam') drop table Column_Nam insert into #Table_Name exec ('Select name from '+@SysName +' where type= '''+@Var+'''') --First Cursor open SET @Table_Name = CURSOR FOR Select table_name from #Table_Name open @Table_Name Fetch Next from @Table_Name into @Table WHILE @@FETCH_STATUS = 0 BEGIN set @Count_Table =@Count_Table+1 --Second cursor opened --print 'Select column_name from '+@informationName +' where table_name= '''+@Table+''''s Exec('SELECT column_name,data_type INTO Column_Nam FROM '+ @informationName +' where table_name = '''+@Table+'''') SET @Column_Name = CURSOR FOR (select column_name from Column_Nam ) OPEN @Column_Name FETCH NEXT FROM @Column_Name INTO @column set @Table=@Database_Selected+'.[dbo].['+@Table+']' WHILE @@FETCH_STATUS = 0 BEGIN set @data_type= (SELECT top 1 data_type FROM Column_Nam Where column_name= @column) if @data_type is not null and (@data_type='varchar' or @data_type='nvarchar' or @data_type='Text') begin set @Result=null if @column like '%-%' begin set @Result =('SELECT ''' + @column +''' FROM ' + @Table +' Where ''' + @column + ''' Like '''+@keytosearch+'''') end else set @Result =('SELECT ' + @column +' FROM ' + @Table +' Where ' + @column + ' Like '''+@keytosearch+'''') insert into #SearchTestResult exec(@Result) set @ID=0 set @ID=(Select ID from #SearchTestResult where ID=Scope_identity()) if @ID is not null begin set @ID_inserted=(select top 1 ID from #SearchResult order by ID desc) if @ID_inserted = @ID begin print '' end else insert into #SearchResult values (@Table,@column) end end FETCH NEXT FROM @Column_Name INTO @column END CLOSE @Column_Name DEALLOCATE @Column_Name --Second cursor closed drop table Column_Nam Fetch Next from @Table_Name into @Table End close @Table_Name Deallocate @Table_Name --First Cursor Closed Select * from #SearchResult
DECLARE @Value AS VARCHAR(20) = 'ABCC' DECLARE @NoOfChars AS INT = LEN(@Value) DECLARE @Permutations TABLE (Value VARCHAR(20)) ; WITH NumTally AS ( --Prepare the Tally Table to separate each character of the Value. SELECT 1 Num UNION ALL SELECT Num + 1 FROM NumTally WHERE Num < @NoOfChars ), Chars AS ( --Separate the Characters SELECT Num, SUBSTRING(@Value, Num, 1) Chr FROM NumTally ) SELECT * FROM Chars
日志错误:
Service Broker needs to access the master key in the database 'BRM_TEST'. Error code:32. The master key has to exist and the service master key encryption is required.
原因:
SqlDependency在清理服务,队列,存储过程时需要一个转换,这个转换需要数据库的master key去存储产生的会话key,虽没多大影响,但会产生错误日志。解决方法是创建一个key给他用。
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '23987hxJ#KL95234nl0zBe';
SELECT db_name(database_id), object_name(object_id), ghost_record_count, version_ghost_record_count FROM sys.dm_db_index_physical_stats(DB_ID(N'db_name'), OBJECT_ID(N'table_name'), NULL, NULL , 'DETAILED');
在创建索引,数据库恢复等耗时操作时,可通过以下方法查看剩余时间
可统计为以下命令完成的工作的百分比:
问题重现:在订阅服务器上执行删出某行,同时在发布服务器上更新此行。发布服务器更新成功后会将命令传到订阅服务器,但因订阅服务器此行已经删除,不能如发布服务器一样更新成功,就会报错。 解决方法:1,找到丢失的行,在订阅上恢复回来。2,跳过命令的执行。 1,查看错误 SELECT DISTINCT xact_seqno FROM dbo.MSrepl_errors WHERE time >='2013-02-24' --查看错误明细,分发库 SELECT mse.time,mse.error_text,CAST(SUBSTRING(command, 7, 8000) AS NVARCHAR(MAX)) command , ms.article,ma.name,ma.publisher_db,mh.comments,mse.xact_seqno,mse.error_code,mh.duration FROM dbo.MSrepl_errors mse JOIN MSdistribution_history mh ON mse.id = mh.error_id JOIN MSdistribution_agents ma ON mh.agent_id = ma.id JOIN MSrepl_commands msc ON mse.xact_seqno = msc.xact_seqno LEFT JOIN MSarticles ms ON msc.article_id = ms.article_id WHERE mse.time>='2013-07-18 14:00' 2,查看错误 EXEC Sp_browsereplcmds @xact_seqno_start = '0x00015F2B00002A26000300000000', @xact_seqno_end = '0x00015F2B00002A26000300000000' 3,解决数据冲突,若不能解决且数据不重要,可以删除命令的执行 DELETE a FROM dbo.MSrepl_transactions a WHERE xact_seqno IN(0x00046FBE00002BFB000400000000) DELETE a FROM dbo.MSrepl_commands a WHERE xact_seqno IN(0x00046FBE00002BFB000400000000) DELETE a FROM dbo.MSrepl_errors a WHERE xact_seqno = 0x00046FBE00002BFB000400000000 4,查看相关命令 SELECT TOP 50 msc.xact_seqno,CAST(SUBSTRING(command, 7, 8000) AS NVARCHAR(MAX)) --INTO # FROM dbo.MSrepl_commands msc(NOLOCK) JOIN MSrepl_transactions a(NOLOCK) ON msc.xact_seqno = a.xact_seqno WHERE entry_time>='2013-04-26' AND CAST(SUBSTRING(command, 7, 8000) AS NVARCHAR(MAX)) LIKE '%sp_MSupd_dboHandlerAllocateOrder%' 5,查询命令相关的表数据 CREATE TABLE #t(xact_seqno VARBINARY(16),originator_srvname VARCHAR(50), originator_db VARCHAR(50),article_id int,type int,partial_command int, hashkey int,originator_publication_id int,originator_db_version int, originator_lsn VARBINARY(16),command VARCHAR(1000),command_id int) SELECT msc.xact_seqno INTO # FROM dbo.MSrepl_commands msc(NOLOCK) JOIN MSrepl_transactions a(NOLOCK) ON msc.xact_seqno = a.xact_seqno WHERE entry_time>='2013-04-26 14:30' AND CAST(SUBSTRING(command, 7, 8000) AS NVARCHAR(MAX)) LIKE '%sp_MSupd_dboHandlerAllocateOrder%' DECLARE PCUR CURSOR FOR SELECT xact_seqno FROM # DECLARE @V1 VARBINARY(22),@V2 VARCHAR(50) OPEN PCUR FETCH NEXT FROM PCUR INTO @V1 WHILE @@FETCH_STATUS = 0 BEGIN SET @v2 = master.dbo.fn_varbintohexstr(@V1) INSERT INTO #T EXEC Sp_browsereplcmds @xact_seqno_start =@v2, @xact_seqno_end =@v2 FETCH NEXT FROM PCUR INTO @V1 END CLOSE PCUR DEALLOCATE PCUR SELECT t.originator_db,ms.article, SUBSTRING(command,CHARINDEX(',{',command)+2,CHARINDEX('},',command,2)-CHARINDEX(',{',command)-2) rowkey, SUBSTRING(command,CHARINDEX(' [',command)+1,CHARINDEX('] ',command,2)+1-CHARINDEX(' [',command)) sp, t.xact_seqno FROM #T t JOIN distribution.dbo.MSarticles ms(NOLOCK) ON t.article_id = ms.article_id
2008开始存错过程可以带表参数,不过限制只读。
CREATE TYPE [dbo].[MyTable] AS TABLE
02.
(
03.
[id] [int],
04.
[value] [nvarchar](128)
05.
)
06.
GO
07.
08.
CREATE PROCEDURE [dbo].[Capitalize]
09.
(
10.
@myTable [dbo].[MyTable] READONLY
11.
)
12.
AS
13.
BEGIN
14.
DECLARE @myOutput [dbo].[MyTable]
15.
16.
INSERT INTO @myOutput([id], [value])
17.
SELECT [id], UPPER([value]) FROM @myTable
18.
19.
SELECT [id], [value] FROM @myOutput
20.
END
21.
GO
22.
23.
DECLARE @myTable [dbo].[MyTable]
24.
DECLARE @myOutput [dbo].[MyTable]
25.
26.
INSERT INTO @myTable([id], [value])
27.
VALUES
28.
(1, 'one'),
29.
(2, 'two'),
30.
(3, 'three')
31.
32.
INSERT INTO @myOutput([id], [value])
33.
EXEC [dbo].[Capitalize] @myTable
34.
35.
SELECT * from @myOutput
If connections exist on a database , and you attempt to detach a database with EXEC master.dbo.sp_detach_db @dbname = N'database_name' the detach will fail. Best practise is to identify the connection sources and communicate with the users, and closing the connection gracefully. Communicating with the users is not always possible . Instead of using the KILL command ,use this command to set the database into SINGLE_USER mode. The SINGLE_USER mode indicates for only one user to access the database. The ROLLBACK IMMEDIATE option terminates ,rolling back all transactions not completed and disconnecting all other connections immediately. USE [master] GO ALTER DATABASE [database_name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE GO USE [master] GO EXEC master.dbo.sp_detach_db @dbname = N'database_name' GO
USE CedarLog GO CREATE TABLE [dbo].[ChangeLog]( [LogId] [INT] IDENTITY(1,1) NOT NULL, [DatabaseName] [VARCHAR](256) NOT NULL, [EventType] [VARCHAR](50) NOT NULL, [ObjectName] [VARCHAR](256) NOT NULL, [ObjectType] [VARCHAR](25) NOT NULL, [SqlCommand] [NVARCHAR](MAX) NOT NULL, [EventDate] [DATETIME] NOT NULL CONSTRAINT [DF_EventsLog_EventDate] DEFAULT (GETDATE()), [LoginName] [VARCHAR](256) NOT NULL, [IP] [VARCHAR](50) NOT NULL, CONSTRAINT [PK_ChangeLog] PRIMARY KEY CLUSTERED ( [LogId] DESC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO SET ANSI_PADDING OFF GO CREATE TRIGGER [backup_objects] ON ALL SERVER FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE, CREATE_TABLE, ALTER_TABLE, DROP_TABLE,CREATE_VIEW, ALTER_VIEW, DROP_VIEW, CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION AS SET NOCOUNT ON DECLARE @data XML SET @data = EVENTDATA() IF @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)') NOT LIKE 'SqlQuery%' BEGIN INSERT INTO CedarLog.dbo.changelog(databasename, eventtype, objectname, objecttype, sqlcommand, loginname,IP) VALUES( @data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(256)'), @data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(50)'), @data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(256)'), @data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(25)'), @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)'), @data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(256)'), CONVERT(VARCHAR(50),CONNECTIONPROPERTY('client_net_address') ) ) END GO ENABLE TRIGGER [backup_objects] ON ALL SERVER GO --查看服务器级别的触发器 SELECT TOP 50 * FROM sys.server_triggers --查看服务器级别的触发器的定义 SELECT * FROM sys.server_sql_modules --查看激发触发器的数据库事件的信息 SELECT TOP 50 * FROM sys.server_trigger_events --删除服务器上的DDL触发器 DROP TRIGGER backup_objects ON ALL SERVER --失效DDL触发器 DISABLE TRIGGER backup_objects ON ALL SERVER --获取有关数据库范围内的触发器的信息 SELECT * FROM sys.triggers --获取有关激发触发器的数据库事件的信息 SELECT * FROM sys.trigger_events --查看数据库范围内的触发器的定义 SELECT * FROM sys.sql_modules --删除当前数据库上的DDL触发器 DROP TRIGGER backup_objects ON DATABASE
SELECT DATABASEPROPERTYEX('mydb','version'),SERVERPROPERTY('Edition') [已安装产品版本], SERVERPROPERTY('ProductLevel') [SQL Server 实例的版本级别],SERVERPROPERTY('ProductVersion') [SQL Server 实例的版本]
QL Server Version
|
Internal Database Version
|
SQL Server 2008 R2
|
665
|
SQL Server 2008
|
661
|
SQL Server 2005 SP2+ with vardecimal enabled
|
612
|
SQL Server 2005
|
611
|
SQL Server 2000
|
539
|
SQL Server 7
|
515
|
DENY VIEW any DATABASE to PUBLIC;
通过触发器来级联删除:
具体的触发器代码如下:
Create TRIGGER [dbo].[DeleteRelatedProducts] ON [dbo].[ProductCategory] AFTER DELETE AS BEGIN SET NOCOUNT ON; delete from [dbo].[product] where categoryId in ( select id from deleted ) END
这种方式比较简单,而且语法也很明了:
具体资料可以参照:http://msdn.microsoft.com/en-us/library/ms191300(SQL.105).aspx
还有一种方式可能并不是很多人知道:
外键的级联删除和更新:
在sql server 中可以通过设置外键的级联删除和更新来实现这个功能。
这里是外键的定义:来自http://baike.baidu.com/view/68073.htm
外键(Foreign Key)
如果公共关键字在一个关系中是主关键字,那么这个公共关键字被称为另一个关系的外键。由此可见,外键表示了两个关系之间的联系。以另一个关系的外键作主关键字的表被称为主表,具有此外键的表被称为主表的从表。外键又称作外关键字。换而言之,如果关系模式R中的某属性集不是R的主键,而是另一个关系R1的主键则该属性集是关系模式R的外键,通常在数据库设计中缩写为FK。
保持数据一致性,完整性,主要目的是控制存储在外键表中的数据。 使两张表形成关联,外键只能引用外表中的列的值或使用空值
我们现在的要求是删除ProductCategory的时候,同时删除该ProductCategory下面的Product。
所以应该在Product 表中建立外键约束,
看到删除规则了吗,指定为层叠的话,那么当删除ProductCategory的时候,就会删除Product了。
select * from ProductCategory; select * from product; Delete from ProductCategory; select * from ProductCategory; select * from product;
结果如下:
引自:关于数据的级联删除和更新
以上多是监控程序错误,但关于漏洞或程序逻辑错误就无法监测,需要采用另一手段,定时的与业务人员举行会议,收集反馈意见,从操作角度去进行优化,这种反馈的问题一般也是使用者最关心的,也最能取得最大效果。
–目标 前50没有基表,md,frm,perm
–预估运行次数
SELECT CONNECTIONPROPERTY('local_net_address') AS 服务端IP地址, CONNECTIONPROPERTY('local_tcp_port') AS 服务端口, CONNECTIONPROPERTY('client_net_address') AS 客户端IP地址 参考:CONNECTIONPROPERTY
读取的日志文件来自安装目录下:MSSQL\Log\ERRORLOG --This will hold the rows CREATE TABLE #ErrorLog (LogDate datetime, ProcessInfo VarChar(10), ErrorMessage VarChar(Max)) -- Dump the errorlog into the table INSERT INTO #ErrorLog EXEC master.dbo.xp_readerrorlog -- Delete everything older than 5 minutes -- ideally you will store the max date when it ran last DELETE #ErrorLog WHERE LogDate < DATEADD(mi,-5,GETDATE()) -- Some stuff you want to check for -- Failed backups...you want to know this SELECT * FROM #ErrorLog WHERE ErrorMessage LIKE'BACKUP failed%' -- Why does it take so looong to grow a file, maybe rethink your settings SELECT * FROM #ErrorLog WHERE ErrorMessage LIKE'Autogrow of file%' -- What is going on any backups or statistic updates running at this time? SELECT * FROM #ErrorLog WHERE ErrorMessage LIKE'SQL Server has encountered %occurrence(s) of I/O requests taking longer than%' -- My mirror might not be up to date SELECT * FROM #ErrorLog WHERE ErrorMessage LIKE'The alert for ''unsent log'' has been raised%' DROP TABLE #ErrorLog 清除日志:Exec sp_cycle_errorlog,执行当前ERRORLOG 重命名为ERRORLOG.1
Exec xp_readerrorlog 2,1,Null,Null,'20130415 08:10','20130415 12:30','Asc' GO parameters: 1.Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc... 2.Log file type: 1 or NULL = error log, 2 = SQL Agent log 3.Search string 1: String one you want to search for 4.Search string 2: String two you want to search for to further refine the results 5.Search the start time 6.Search the end time 7.Sort order for results: N'asc' = ascending, N'desc' = descending