Jun 22, 2012

Index rebuilding in sql server with query


Rebuild Index with query in Sql Server

DECLARE @TableName varchar(255)DECLARE @TableSchema varchar(100)DECLARE TableCursor CURSOR FOR SELECT TABLE_SCHEMA, TABLE_NAME  FROM information_schema.tablesWHERE table_type = 'base table'OPEN TableCursorFETCH NEXT FROM TableCursor INTO @TableSchema,  @TableNameWHILE @@FETCH_STATUS = 0BEGINSET @TableName=@TableSchema + '.' + @TableNameDBCC DBREINDEX(@TableName, ' ', 100)FETCH NEXT FROM TableCursor INTO @TableSchema,  @TableNameENDCLOSE TableCursorDEALLOCATE TableCursor


But if you want to learn which table needs rebuild, use this query;

DECLARE @dbid intSET @dbid = db_id()SELECT convert(decimal(5,2), avg_fragmentation_in_percent) AS avg_fragmentation_percent, object_name(d.object_id) AS [table], i.name AS [name], d.index_type_desc, alloc_unit_type_desc AS [type], index_depth, index_level, fragment_count, page_countFROM sys.dm_db_index_physical_stats( @dbid, null, -1, null, 'SAMPLED') dINNER JOIN sys.indexes i ON i.object_id=d.object_id AND i.index_id=d.index_idWHERE  avg_fragmentation_in_percent > 30;

May 8, 2012

Write EventLogs with C# class

Here is the orijinal article.  http://www.kad1r.com/article.aspx?articleId=306&Category=ASP.Net&title=Write-EventLog-with-ASP.Net


using System;
using System.Diagnostics;

namespace smsXmlWS
{
    public class LogManager
    {
        // EventLogEntryType.Error
        // EventLogEntryType.FailureAudit
        // EventLogEntryType.Information
        // EventLogEntryType.SuccessAudit
        // EventLogEntryType.Warning
       
        public LogManager()
        { }

        public static void WriteLog(string message, EventLogEntryType eventType)
        {
            if (!System.Diagnostics.EventLog.SourceExists("
LOGNAME"))
            {
                EventSourceCreationData data = new EventSourceCreationData("
LOGNAME", "LOGNAME");
                System.Diagnostics.EventLog.CreateEventSource(data);
            }

            if (eLog == null)
                eLog = new System.Diagnostics.EventLog();

            eLog.Source = "
LOGNAME";
            eLog.WriteEntry(message + " DATE : " + System.DateTime.Now.ToString(), eventType);
        }
        private static System.Diagnostics.EventLog eLog = null;
    }
}


Combine rows into one row in mssql

Combine lines instead: As you know, we obtained data from the database when we receive lines. But sometimes we need them in a row. We do this, view, or just use a temporary table. I use a temporary table in it. Here is an easy transition.

CREATE TABLE #Temp([Numbers] varchar(20))
INSERT INTO #Temp VALUES('asp');
INSERT INTO #Temp VALUES('.net');
INSERT INTO #Temp VALUES('database');
INSERT INTO #Temp VALUES('views');
INSERT INTO #Temp VALUES('temp');
DECLARE @str VARCHAR(200)
SELECT @str = COALESCE(@str + '-', '') + [Numbers]
FROM #Temp
Print @str

Here is the output: asp-.net-database-views-temp