Search

Wednesday, June 22, 2011

How to Move TempDB

Determine the current location of the MDF and LDF files.
Run the ALTER DATABASE command to move the files


USE master;
GO
ALTER DATABASE tempdb 
MODIFY FILE (NAME = tempdev, FILENAME = 'E:\SQLData\tempdb.mdf');
GO
ALTER DATABASE tempdb 
MODIFY FILE (NAME = templog, FILENAME = 'F:\SQLLog\templog.ldf');
GO


Stop and restart the SQL Server instance
Delete the old MDF and LDF files

Tuesday, June 21, 2011

How to take backup of data if the system got corrupted?


You may require emergency data backup in following situations
  1. The OS is corrupt or system is not booting
  2. The OS is running but FusionRetail 6 is not starting up
  3. Microsoft SQL Server Management Studio Express is not present.
  4. You do not know how to use SQL Server Management Studio Express to take backup.

Please use the following steps to take emergency backup of data in case of emergency

First we have to stop the SQL Server if that is running. For stopping the SQL use the following step.
    • Go to Start > Run > Type net stop mssql$

    • Copy the data directory of the database. (Usually C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data)
    • Make sure that the data files with extension .MDF and .LDF is present
      ** Once You have copied the above files to another computer or any safe place…
      • After Installing SQL Server, Use "Attach Database" option in Management Studio to attach this MDF file.

      Monday, June 20, 2011

      Update a column in a Large Table in SQL Server

      Consider a table called TestTable which has more than 6 millions rows. Suppose you want to update a column with the value 100 if that column contains negative value. Let us also assume that there are over 1 million row in that column that has a negative value.

      The simple way to do this is :

      UPDATE TestTable SET Column = 100 WHERE Column < 0

      This query is fine but it will take lot of time to finish and also it will lock the table while update running.

      We can improve its performance if we run update in small packet (some rows at a time).

      SET RowCount 10000
      UPDATE TestTable SET Column = 100 WHERE Column < 0
      WHILE @@RowCount > 0
      BEGIN
      SET RowCount 10000

      UPDATE TestTable SET Column = 100 WHERE Column < 0
      END
      SET RowCount 0

      The above code will update 10000 rows at a time and it will continue till @@RowCount > 0. Table is also not locked for a long time.


      Saturday, June 18, 2011

      Setup Black Box in SQL Server

      Check out the below script to trace. This will capture all the SQL Server statements and activities. Be extra cautious as this will take significant space which depends on SQL Server activities. This trace will be created in default sql server Data Directory, this case c:\Program files, as we install SQL Serever 


      --  Create a Trace file, which will call all SQL Server events and Activities */
      declare @rc int, @traceid int
      exec @rc = sp_trace_create @traceid output, 8
      select traceid = @traceid, error = @rc
      exec sp_trace_setstatus @traceid, 1
      -- -- Select the TraceId, which is initiated by SQL Server, this number will be used to stop the trace
      select * from ::fn_trace_getinfo(@traceid)


      -- We are using 2, as a traceID which was started in last statement, 
      sp_trace_setstatus 2, 0
      sp_trace_setstatus 2, 2





      Friday, June 17, 2011

      How to communicate to SQL Server through a Firewall?

      Firewall systems prevent unauthorized access to computer resources. To access an instance of the Microsoft SQL Server Database Engine through a firewall, you must configure the firewall on the computer running SQL Server to allow access.

      There are many firewall systems available. For information specific to your system, see the firewall documentation.

      Important:
      Opening ports in your firewall can leave your server exposed to malicious attacks.
      Make sure that you understand firewall systems before you open ports.
      Some of the TCP Ports needed for communication to SQL Server through a Firewall. You need to add these ports and services to the firewall exception list.

      By default, Microsoft Windows XP Service Pack 2 enables the Windows Firewall, which closes port 1433 to prevent Internet computers from connecting to a default instance of SQL Server on your computer. Connections to the default instance using TCP/IP are not possible unless you reopen port 1433. The basic steps to configure the Windows XP firewall are provided in the following procedures. For more information, see the Windows documentation.

      To open a port in the Windows firewall for TCP access
      1.   In Control Panel, open Network Connections, right-click the active connection, and then click Properties.
      2.   Click the Advanced tab, and then click Windows Firewall Settings.
      3.   In the Windows Firewall dialog box, click the Exceptions tab, and then click Add Port.
      4.   In the Add a Port dialog box, in the Name text box, type SQL Server <instance name>.
      5.   In the Port number text box, type the port number of the instance of the Database Engine, such as 1433 for the default instance.
      6.   Verify that TCP is selected, and then click OK.
      7.   To open the port to expose the SQL Server Browser service, click Add Port, type SQL Server Browser in the Name text box, type1434 in the Port Number text box, select UDP, and then click OK.
      Note:To allow named pipes access through the firewall, you must also enable File
      and Printer Sharing through the firewall. 
      8.   Close the Windows Firewall and the Properties dialog boxes.

      Click Add Program in the Windows Firewall dialog box for additional options, such as granting access to specific programs and restricting access to certain IP addresses or network subnets.

      As an alternative to configuring SQL Server to listen on a fixed port and opening the port, you can list the SQL Server executable (Sqlservr.exe) as an exception to the blocked programs. Use this method when you want to continue to use dynamic ports. Only one instance of SQL Server can be accessed in this way.

      To access a program through the Windows firewall
      1.  In the Windows Firewall dialog box, on the Exceptions tab, click Add Program.
      2.  Click Browse, and navigate to the instance of SQL Server that you wish to access through the firewall,and then click Open. By default SQL Server is at C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\Sqlservr.exe.
      3.   Click OK twice to close the Windows firewall program.

      Error Message 8: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (Provider: TCP Provider, error: 0-A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
      Cause: Due to connection blocked by Windows firewall.
      Troubleshoot: To resolve this, take follow steps:
      1.  Enable SqlBrowser
      2.  Add SqlSERVR.exe into Firewall exception list
      3.  Add SqlBROWSER.exe into Firewall exception list
      4.  Add Tcp port to Firewall exception list. 
           (eg, Name-1433:TCP, Value-1433:TCP:*:Enabled:Tcp 1433).
      5.  If you are using some third party firewall then contact the vendor for more detail.
      6.  For more detail please see the related links.

      Related links: