Showing posts with label MSSQL Administration. Show all posts
Showing posts with label MSSQL Administration. Show all posts

Tuesday, August 8, 2017

SQLServerAgent could not be started (reason: Unable to connect to server '(local)'; SQLServerAgent cannot start).


If SQL Server agent not started and if you find below error in event viewer

Error Details

 Log Name             :         Application
Source                  :         SQLSERVERAGENT
Date                      :         8/8/2017 3:49:43 PM
Event ID               :         103
Task Category      :         Service Control
Level                    :         Error
Keywords             :      Classic
User                      :          N/A
Computer             :      VmSQLServer
Description           :
SQLServerAgent could not be started (reason: Unable to connect to server '(local)'; SQLServerAgent cannot start).















Solution 

Provide instance in  in "ServerHost" under

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\SQLServerAgent

Goto run
type regedit
expand HKEY_LOCAL_MACHINE --> SOFTWARE --> Microsoft --> Microsoft SQL Server
click on SQLServerAgent in Microsoft SQL Server
In right side pane double click on ServerHost and provide instance name under  value data

Monday, November 23, 2015

Change compatibility level



Using below statements you can change comparability level of  all user databases  with a single click. Comparability level is decided bases on sql server product level.

SQL Server
Comparability Level
SQL Version
SQL 2016
130

SQL 2014
120
12
SQL 2012
110
11
SQL 2008
100
10
SQL 2005
90
9
SQL 2000
80
8


use master;
go

DECLARE @SQLVer varchar(10);
-- select the compatibility level based in sql server version
select @SQLVer = CASE  SUBSTRING(convert(varchar,SERVERPROPERTY('productversion')),1,2)
when 12 then '120'
when 11 then '110'
when 10 then '100'
when 9. then '90'
when 8. then '80'
end


DECLARE UserDatabases_CTE_Cursor Cursor
FOR

-- filer user database names from sysdatabases
select name as DatabaseName from sys.sysdatabases where ([dbid] > 4)

OPEN UserDatabases_CTE_Cursor
DECLARE @dbName varchar(100);
DECLARE @compatQuery varchar(500);

Fetch NEXT FROM UserDatabases_CTE_Cursor INTO @dbName
While (@@FETCH_STATUS <> -1)

BEGIN

-- set database compatibility level
set @compatQuery =  'ALTER DATABASE [' + @dbName + '] SET COMPATIBILITY_LEVEL = ' + @SQLVer



-- Execute compatability script
EXEC (@compatQuery)

-- Get next database
Fetch NEXT FROM UserDatabases_CTE_Cursor INTO @dbName
END

CLOSE UserDatabases_CTE_Cursor
DEALLOCATE UserDatabases_CTE_Cursor

GO

Tuesday, November 17, 2015

xp_cmdshell help; The configuration option 'xp_cmdshell' does not exist, or it may be an advanced option.


Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

Execute below statements to enable "xp_cmdshell" and 

EXEC sp_configure 'xp_cmdshell', 1  
GO  
RECONFIGURE  
GO


After executing above statements if you are facing below error

Msg 15123, Level 16, State 1, Procedure sp_configure, Line 51
The configuration option 'xp_cmdshell' does not exist, or it may be an advanced option.

Try below statements to enable advanced options  

EXEC sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
EXEC sp_configure 'xp_cmdshell', 1;  
GO  
RECONFIGURE;  
GO

Sunday, June 14, 2015

NetFx3 error while installing SQL Server 2012 on windows 2012 server



Problem description

TITLE: Microsoft SQL Server 2012 Service Pack 1 Setup

The following error has occurred:

Error while enabling Windows feature : NetFx3, Error Code : -2146498298 , Please try enabling Windows feature : NetFx3 from Windows management tools and then run setup again. For more information on how to enable Windows features , see http://go.microsoft.com/fwlink/?linkid=227143







Solution 

Goto server manager
 --> Click on manager right side corner of window
--> Click Add Roles and Features



Blow Add Roles and features wizard window appears 


click on next


in the next screen click on Role-based or feature-based installation

and click on the next


in the Server Selection Screen select the first radio button i.e. Select a Server from the Server Pool 

In the server pool window you can see the server name 


On Server Roles page simply click on next

in Features page wizard clicked on the check box next to ".NET Framework 3.5 Features", and then clicked on the Next as shown in below image


and in final wizard click on install to add features



and installation progress as below 





Thanks & Regards
Venkat Sangu

Sunday, May 3, 2015

How to connect PDW


You can login pdw using MS Visual Studio 2012/SQL Data Tools  or Nexus Query Chameleon


Lets try using Visual Studio 2012 

Goto Visual Studio 2012 icon and click 


Click on Run the program without getting help

you can see 


Click on View and select SQL Server Object Explorer 


now you can see below screen right click on the SQL Server 


Select Add SQL Server 


Provide the PDW server and Login & password details ( you can login using SQL Authentication or using Windows Authentication) 

and click on connect 

Friday, April 10, 2015

Table Space used and row count



Using below query you can find out the table space in a database and rows count of the table. 

Please change/add your tables list in WHERE condition  


SELECT T.NAME AS TableName, P.rows AS RowCounts, SUM(a.total_pages) * 8 AS TotalSpaceKB,  SUM(a.used_pages) * 8 AS UsedSpaceKB, (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB 
 FROM  sys.tables T
 INNER JOIN      
    sys.indexes I ON t.OBJECT_ID = i.object_id
 INNER JOIN 
    sys.partitions P ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
 INNER JOIN 
    sys.allocation_units A ON p.partition_id = a.container_id
 LEFT OUTER JOIN 
    sys.schemas S ON t.schema_id = s.schema_id
 WHERE 
    T.NAME in ('Table1','Table2','Table3','Table4') 
    AND T.is_ms_shipped = 0
    AND I.OBJECT_ID > 255 
 GROUP BY 
    T.Name, S.Name, P.Rows
 ORDER BY 
    T.Name

 you can find using SSMS as shown in below. Right click on the database and click on the Reports --> select Standard Reports --> and select Disk Usage by Table. 






and results shown like below

Monday, March 23, 2015

Script to disable/enable all triggers (Database Level) in a database & Enable or Disable Server level triggers (SERVER LEVEL)

Use below script to disable or enable all triggers in MyDatabase  (Database level) 

To disable all triggers, you can use following statement
use MyDatabase
sp_msforeachtable 'ALTER TABLE ? DISABLE TRIGGER all'

To enable all triggers, you can use following statement
use MyDatabase
sp_msforeachtable 'ALTER TABLE ? ENABLE TRIGGER all'


The following example disables all DDL triggers that were created at the server scope. (Server Level Triggers) 
USE master
GO
DISABLE Trigger ALL ON ALL SERVER;
GO

USE master
GO
ENABLE Trigger ALL ON ALL SERVER;
GO




Thursday, October 30, 2014

How to copy non-clustered indexes to subscriber in snapshot replication



In snapshot replication by default Non-cluster indexes are not copied to subscriber below is the work around for copying Non-cluster indexes to subscriber in Snapshot replication.

Step 1:
  - Login into the Publisher server
  -  Expand the Replication folder and Local Publications
  -  Right click on the publisher and select properties





Step 2:
                In Publication Properties window select Articles Tab à Select the Article Properties
               

Step 3:
  -  In Popup select Set Properties of All Table articles to copy all objects Non-Cluster indexes
  -  Select Set Properties of Highlighted table article to change the option to copy Non-cluster indexes of specific table


Step 4: 

                In Properties for All Tables Articles window change the below highlighted property from False to True.
               

In next run of snapshot will copy all noncluster indexes to subscriber.

Saturday, October 25, 2014

Validate all views in the database


There is an sp that validates the metadata and the   same sp we can use to validate

set nocount on
select ' print ''' +name +'''
exec sp_refreshview ['+name+']' + CHAR(10)+ '
go '
from sys.views


Execute  the above script it will give script to run sp_refreshview on all views. Run the scripts generated using above sql statement I will give error link below screen


Tuesday, October 21, 2014

SQL Server and Build Numbers


Build chart lists all of the known Service Packs (SP)

RTM
SP1
SP2
SP3
SP4
SQL Server 2014
12.0.2000.8 12.00.2000.8




SQL Server 2012
11.0.2100.60 11.00.2100.60
11.0.3000.0
or 11.1.3000.0
11.0.5058.0
or 11.2.5058.0


SQL Server 2008 R2
10.50.1600.1
10.50.2500.0/
10.51.2500.0
10.50.4000.0/ 10.52.4000.0
10.50.6000.34/ 10.53.6000.34

SQL Server 2008
10.0.1600.22 10.00.1600.22
10.0.2531.0 10.00.2531.0
or 10.1.2531.0
10.0.4000.0 10.00.4000.0/
10.2.4000.0
10.0.5500.0 10.00.5500.0/ 10.3.5500.0
10.0.6000.29 10.00.6000.29/ 10.4.6000.29

SQL Server 2005
9.0.1399.06 9.00.1399.06
9.0.2047/9.00.2047
9.0.3042/ 9.00.3042
9.0.4035/ 9.00.4035
9.0.5000/ 9.00.5000
SQL Server 2000
8.0.194 8.00.194
8.0.384 8.00.384
8.0.532 8.00.532
8.0.760/ 8.00.760
8.0.2039 8.00.2039
SQL Server 7.0
7.0.623 7.00.623
7.0.699 7.00.699
7.0.842 7.00.842
7.0.961 7.00.961

Thursday, October 16, 2014

DBCC CHECKIDENT




-- Create Table Script to test CHECKIDENT

CREATE TABLE DBCC_ReSeed
( ID INT IDENTITY(1,1) NOT NULL,
DateAdded DATETIME NOT NULL )

-- Inserting values to DBCC_ReSeed table
INSERT INTO DBCC_ReSeed(DateAdded) VALUES(GETDATE())
INSERT INTO DBCC_ReSeed(DateAdded) VALUES(GETDATE())
INSERT INTO DBCC_ReSeed(DateAdded) VALUES(GETDATE())
INSERT INTO DBCC_ReSeed(DateAdded) VALUES(GETDATE())
INSERT INTO DBCC_ReSeed(DateAdded) VALUES(GETDATE())

SELECT * FROM DBCC_ReSeed

--- Check the current value which produces the below output… 
DBCC CHECKIDENT ('DBCC_ReSeed', NORESEED)

---  Output like :  Checking identity information: current identity value '5', current column value '5'.
--- Now reset the identity value to 20 so that the next time when we insert data into DBCC_RdSeed table , the value will be 21…

DBCC CHECKIDENT('DBCC_ReSeed', RESEED, 20)

----- Output like :   Checking identity information: current identity value '5', current column value '20'


--  Add another row and check identity value. The row inserted will have a value of 21… 

INSERT INTO DBCC_ReSeed (DateAdded)  VALUES(GETDATE());

SELECT * FROM DBCC_ReSeed



-- Delete data in table and reset to start from 1

DELETE FROM DBCC_ReSeed

DBCC CHECKIDENT('DBCC_ReSeed')
-- After deleting data identity value didn’t reset and the value will be remain same  
---  Output like :  Checking identity information: current identity value '20', current column value '20'.


DBCC CHECKIDENT('DBCC_ReSeed', RESEED, -1)

---  Output like :  Checking identity information: current identity value '21', current column value '-1'.


INSERT INTO DBCC_ReSeed (DateAdded)  VALUES(GETDATE());
INSERT INTO DBCC_ReSeed (DateAdded)  VALUES(GETDATE());

SELECT * FROM DBCC_ReSeed


Monday, August 11, 2014

change server level collation for a SQL Server Instance

Collations specify the rules for how strings of character data are sorted and compared, based on the norms of particular languages and locales


The server collation acts as the default collation for all system databases that are installed with the instance of SQL Server, and also any newly created user databases. The server collation is specified during SQL Server installation.  It is not mandatory that we change the default server level collation, because you can specify a different collation level when you create users databases, but you need to remember to specify this when creating user databases.

To change the default SQL Server collation you can simply rebuild the system databases. When you rebuild the master, the model, msdb and tempdb system database are actually dropped and recreated in their original location. If a new collation is specified in the rebuild statement the system databases are rebuilt using that collation setting. Any user modifications to these databases will be lost, so it is important to backup any of this information you wish to retain

Step 1

First check the existing SQL Server collation setting of your instance. Run the command below to get the collation value of your SQL Server instance.
SELECT SERVERPROPERTY('Collation')
Step 2
Make sure to record all server level settings before rebuilding the system databases to ensure that you can restore the system databases to their current settings.  


Step 3

Take backup of all user and server databases.

Take the backup of all jobs, maintenance plans, logins and their access levels
To take all jobs backup in OBJECT EXPLORER window select job folder & in right side OBJECT EXPLORE DETAIL window it displays all jobs, select all jobs and right click on your selection then choose the SCRIPT AS option to create the script for all jobs.

Next is to secure your logins, passwords and their access levels. You can use sp_help_revlogin stored procedure to create a script for all logins so they can be recreated easily. And take server level permissions using script

Step 4

Detach all user databases before rebuilding your system databases. If you leave databases attached they will be detached and will be found in the database folder.

Step 5

Now its time to rebuild your system databases. This operation will recreate your master database and all existing settings will be reset. Run the below command from a Windows command prompt. Make sure to run this command from the directory where you have placed your SQL Server setup files. Once you press enter, a separate window will appear to show you the progress bar. Once the rebuild is done, that window will disappear.

Setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=MANVENDRA /SQLSYSADMINACCOUNTS=domain\adminUser /SAPWD= SaPWD@SqlRebuild /SQLCOLLATION=SQL_Latin1_General_CP1_CI_AI

* SQL_Latin1_General_CP1_CI_AI –this is the new collation need to change

Once the rebuild operation is complete, check the server collation to verify whether this change is successful or not. As we can see in the screenshot below, the server collation has changed to SQL_Latin1_General_CP1_CI_AI. At this point we cannot restore any of the system databases, because doing so will revert back to the previous collation setting. So we will need to use the scripts that were created to recreate logins, jobs

Step 6

Attach all user databases which were detached in Step 4.

Step 7

Now change the collation settings of all user databases. It's not necessary to change the collation settings for the user databases, it totally depends on your requirement.

Run the commands below to change the collation settings of your user databases.

ALTER DATABASE CollationChangeDBName collate SQL_Latin1_General_CP1_CI_AI
Sometimes the command fails to execute and you get this error:

Msg 5075, Level 16, State 1, Line 1 The object 'PK_xxxx' is dependent on database collation. The database collation cannot be changed if a schema-bound object depends on it. Remove the dependencies on the database collation and then retry the operation.
In that case you may need to export all data and recreate the database with the new collation settings.

Step 8

Now run all of the scripts which were created in Step 3 to restore jobs, alerts, logins, operators, etc...  Also don't forget to change the server level configuration settings which were captured in Step 2.

Now your instance is ready to use the new server level collation.

Cannot access the specified path or file on the server. Verify that you have the necessary security privileges and that the path or file exists

If you received below error while attaching a .mdf file in cluster environment please follow below steps to resolve the issue ERROR Ca...