Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘sql server’

What is AlwaysOn in SQL Server?

Posted by Paul on March 3, 2014

SQL Server 2012 introduced the new feature called AlwaysOn.

AlwaysOn provides High Availability & Disaster Recovery alternative to Database Mirroring.

What is Database Mirroring?

Database Mirroring is a technique to increase High Availability of database.  The topology consists of three server roles.

  1. Witness Server
  2. Principal Server
  3. Mirror Server 

Transaction Logs will be synchronized from Principal Server to Mirror Server.

image

The Witness Server determines whether to contact Principal or Mirror based on activity, thus increasing the availability.

Advantages of AlwaysOn over Database Mirroring

AlwaysOn provides the following advantages over Database Mirroring:

  1. More effective server utilization
  2. Less configuration hassles
  3. Better manageability
  4. Better failover situation
  5. Works best with multiple-database scenarios of SharePoint

More and More people are switching to AlwaysOn Availability Group technique.

References

AlwaysOn Experiment

Summary

Clustering, Mirroring, Log Shipping, AlwaysOn are different technologies to attain High Availability in database servers.

Advertisements

Posted in SharePoint, SharePoint 2010, SharePoint 2013, SQL Server | Tagged: , | Leave a Comment »

SQL Server–Create User / Domain User using SQL

Posted by Paul on March 23, 2012

CREATE LOGIN user1WITH PASSWORD = ’MyPassword’  USE TheDbName CREATE USER user1FOR LOGIN user1;
GO
CREATE LOGIN [Domain\user1] FROM WINDOWS;
            USE TheDbName CREATE USER user1 FOR LOGIN [Domain\user1]

Posted in C# | Tagged: , | Leave a Comment »

SQL Server–Create User / Domain User using SQL

Posted by Paul on March 23, 2012

CREATE LOGIN user1WITH PASSWORD = ’MyPassword’  USE TheDbName CREATE USER user1FOR LOGIN user1;
GO
CREATE LOGIN [Domainuser1] FROM WINDOWS;
            USE TheDbName CREATE USER user1 FOR LOGIN [Domainuser1]

Posted in C# | Tagged: , | Leave a Comment »

Sql Server – Transact Sql to make a column unique

Posted by Paul on April 23, 2011

ALTER TABLE TableName
ADD CONSTRAINT NewConstraintName
UNIQUE(ColumnName)

Posted in SQL Server | Tagged: , , | Leave a Comment »

Sql Server – Transact Sql to make a column unique

Posted by Paul on April 23, 2011

ALTER TABLE TableName
ADD CONSTRAINT NewConstraintName
UNIQUE(ColumnName)

Posted in SQL Server | Tagged: , , | Leave a Comment »

How to shrink Sql Server Database?

Posted by Paul on April 21, 2011

The Enterprise Management studio provides an option to shrink the database.

 

Step 1: Open Enterprise Management Studio (i am using sql server 2008) and select database, right click and choose Tasks>Shrink

 

image

Step 2: In the appearing dialogue press Ok to start the shrink process.

image

Posted in SQL Server | Tagged: , , | Leave a Comment »

Automatic History using Triggers

Posted by Paul on February 5, 2011

I would like to take a moment to explain the following problem I am facing:

I do have a table named Equity which contains the company name and the current PE Ratio. 

image

The PE Ratio will be updated daily or weekly based on the wish of user through a web application in ASP.NET.  When the process runs for many years, there will be many updating happening to the same record. 

Problem

Our problem is to get the historical PE Ratio values, so that we can find out the maximum and minimum values for decision making.

High Cost Solution

One of the solutions, we can prefer is while updating the record from our web application, take a copy of the record and insert it into another table. 

This involves changing the associated business logic layer in c# or changing the stored procedure.  I would say  this is more time consuming and lot of code we have to write to write to manage the new table if schema changes are involved.

Simple Solution

We can use database triggers to achieve the same.  The solution steps are as following.  Belive me, this will take only 5 minutes to do.

1. Create a new History Table with same structure

2. Create a Trigger for UPDATE operation

As our History table is not involved in any business logic currently, we can use the Trigger to solve the problem and move off.

Pre-requisites

First, we have to create our original Equity table.  The script is given below.  The script was generated from Sql Server 2005.

CREATE TABLE [dbo].[Equity](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [PERatio] [float] NULL,
    [ModifiedOn] [datetime] NULL,
CONSTRAINT [PK_Equity] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Create the History Table

We can create the EquityHistory table using the following script.  Please note that there is an additional column HistoryId which keeps track of the History record as there will be multiple history records for each original table record.

CREATE TABLE [dbo].[EquityHistory](
    [HistoryId] [int] IDENTITY(1,1) NOT NULL,
    [Id] [int],
    [Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [PERatio] [float] NULL,
    [ModifiedOn] [datetime] NULL,
CONSTRAINT [PK_EquityHistory] PRIMARY KEY CLUSTERED
(
    [HistoryId] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Create the Trigger

The following Trigger script will do all the magic involved.  It says that create a trigger on table Equity whenever UPDATE statement is used.  The last line says that get the record from table inserted and insert it into the EquityHistory table

CREATE TRIGGER Equity_Update_Trigger
ON Equity
FOR UPDATE
AS
INSERT INTO EquityHistory SELECT * FROM inserted

The table inserted  is a pre-defined table insided triggers.  It will give the current record structure while using with INSERT/UPDATE triggers.

Testing the Whole

You can try inserting a new record to the original Equity table.  The corresponding record will get automatically inserted in the EquityHistory table.

Now, try changing the PERatio from the first able.  You can see a new record gets added to the history table.

image

Attachment

The attachment file contains queries to create table, insert data, select data.  You can try running the file against an sql server database.

Posted in SQL Server | Tagged: , , , | Leave a Comment »

How to create Linked Server in Sql Server?

Posted by Paul on November 11, 2010

Open the Management Studio and in the Server Objects > Linked Servers, right click select New Linked Server menu.

image

In the appearing screen enter the actual remoteserver alias name as “RS”

image

Enter Product Name=’’, Data source=[YourRemoteServerInstanceName], Catalog=[DbName]

and press Ok button.

In the security tab enter map the sa user like following screen.

Note: If you are using another user to connect to the current server, use that user name for mapping.

 

image

Press Ok to complete the form.

Testing the Server

You can type any query with the prefix REMOTESERVER to access the tables.

SELECT * FROM RS.Database.dbo.Table

If you can see the results,  Congratulations!  You have done with linked servers

In case of error try the following

1. Change the Security Property (previous screen) “Be made using the security context:”

2. Set the Remote login and password in the textboxes.

3. Retry the query

Creating backup of table in remote server to local

SELECT * INTO new_table_name FROM RS.Database.dbo.Table

Posted in C# | Tagged: , | Leave a Comment »

Sql Server :: Cool Sqls

Posted by Paul on November 11, 2010

Some useful sqls I am sharing here.

Description Sql
Get Row Number of any table
SELECT ROW_NUMBER() OVER (ORDER BY Name ASC) AS ROWID, * FROM  TableX
Delete Duplicate Records

WITH CTE (Col1,Col2, DuplicateCount)

AS

(

SELECT Col1,Col2,

ROW_NUMBER() OVER(PARTITION BY Col1,Col2 ORDER BY Col1,Col2) AS DuplicateCount

FROM Table1

)

DELETE

FROM CTE

WHERE DuplicateCount > 1

Create Copy of a table

SELECT *

INTO new_table_name [IN externaldatabase]

FROM old_tablename

Create Linked Server

exec sp_addlinkedserver @server = ‘servername‘, @srvproduct = ”, @provider = ‘sqloledb’,

@datasrc = ‘serverhostname‘, @catalog = ‘dbname

Copy table from linked server to local server SELECT * INTO NewTable FROM RS.Db.dbo.RemoteTable
Transactions

BEGIN TRAN;

    SELECT * FROM Address

    IF (@@ERROR <> 0) GOTO ERR_HANDLER

    COMMIT TRAN;

    RETURN;

ERR_HANDLER:

PRINT ‘Unexpected error occurred!’

ROLLBACK TRAN

Identity Insert Enable SET IDENTITY_INSERT YourTable ON

Some Cool Sql Server Links

Insert Query Generator

http://www.codeproject.com/KB/database/InsertGeneratorPack.aspx

Posted in C# | Tagged: , , | Leave a Comment »

The system tables in Sql Server

Posted by Paul on October 28, 2010

We can use the sys.tables and sys.objects to find metadata of tables and columns.

Display all User Tables

select * from sys.tables – displays all user tables

Display all Columns in a Table

select * from sys.columns where object_id in
(select object_id from sys.tables where name=’MyTable)

Posted in C# | Tagged: , | Leave a Comment »