Skip to main content

Posts

Log database structure changes

In many application the database structure can be changed by the user ex. CRM or Reporting. In such situation we may want to know such changes was maded. In SQL SERVER 2008 we have a possibility to react when DML (Data Manipulation Language) AND DDL (Data Definition Language) execution occured in out database. So try to log it. Firstly we want to Create a single table named 'DatabaseChangesLog': use model; GO CREATE SCHEMA LOGS CREATE TABLE DatabaseChangeLogs (    EventId int Identity Primary Key,    EventDate datetime2  Constraint DF_DefaultLogDate DEFAULT(sysdatetime()),    EventType nvarchar(100) NOT NULL,    UserName nvarchar(1050) NOT NULL,    Command nvarchar(max) NOT NULL    ) GO Note that we creata this table in model databse. This means that each newly created database will has this table after creation. For existing databases You need to run this script manullay. Now when we have appropriate table we can insert record to. We are able to detect each

Executed command preview in Sql Server

SQL Server offers a lot of system views which allow to get information about each instance without interfering in system tables. All those system views are located in hidden system datadabase name 'Resource'. All system views can be divided into at least two groups: Structure views - which storing information about SQLSERVER properties and custom structure, and begins with schema INFORMATIN_SCHEMA.* . (compilant with ISO) Dynamic views - storing information at runtime from last time instance starts; this begins with sys.dm_* When we need to get information about executed connection in the all current opened session we can query to sys.dm_exec_connections. In this view we see two very important columns: session_id: represent the IDs of all current opened session; note that session ID is presented in each query tab in Managment Studio (ex. 52). most_recent_sql_handle: a handle to the most recent executed query for single session ID. Now assume that we want to preview

Connect to SQL by using TCP/IP

In MS SQL Server Managment Studio we can connect to any instance by using TCP/IP protocol. To do this we can use the following syntax: tcp: server_name\instance, port . For example: local instance:   tcp: .\R2,51550 remote instance: tcp: 192.168.11.5\R2,999 local alias: tcp: .\alias,51550

Replacing/removing HTML Entities in database

Sometimes solutiona that we create stores data from external suppliers. These data is stored in database and than presents to end-user. The problem occured when our suppliers user XML basen technology to send data package to us.  The  XML standard do not allow using some special characters in text (node attribute or value) so each occurance of special characters encoded. Each of us should know that and try do decode those entities before wtiring it to database and/or presents to user. But in real many thing may go wrong and in some rows of our product database these signs may appear. When You recognize the problem You can do three things but only two of them are correct. The first idea ( wrong ) is the attempt to create a CLR stored procedure or function with the System.Web.HttpUtility.HtmlDecode function. The problem is that You can`t add reference to System.Web in CLR projects! So this idea can not be executed. Second idea is to create a stand alone console application and impl

Visualize geospatial data from SQL SERVER 2008 R2

With the development of digital maps such as OpenStreetMaps and Google Earth or Google Maps and other GIS systems many of the softwares need to processing geospatial data which are stores in databases. In the new version of the SQL Server 2008 R2 some new features to working with geographic data were introduced. First of all developers are able to work with two new datatypes which where implemented as a .NET common language runtime (CLR) data type: geography  geometry These types support methods and properties that allow for the creation, comparison, analysis, and retrieval of spatial data. The difference between then is the geometry data type (called planar) supported by SQL Server conforms to the Open Geospatial Consortium (OGC) Simple Features for SQL Specification while the geography data type (geodetic) stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates. After short theoretical introduction it`s time for implementation. First of all we need