Wednesday, June 20, 2018

SQL server check the status of the shrink

I have shrink database using SQL server Management Studio visual editor.

Right click database --> tasks --> Shrink --> database

Window comes and shows shrink is running. But this has been there for more than one day. So i need to check the status of the shrinking.



with below script you will get list of the commands and its status, completed percentage etc. look for command DbccFilesCompact , you will get all the details.

--------------------------------------------------------------------------------------------------

SELECT percent_complete, start_time, status, command, estimated_completion_time, cpu_time, total_elapsed_time
FROM sys.dm_exec_requests

----------------------------------------------------------------------------------------------------

As above  may give a little too large list, you can put a cross apply and get a better output as below script


----------------------------------------------------------------------------------------------------

select T.text, R.Status, R.Command, DatabaseName = db_name(R.database_id)
       , R.cpu_time, R.total_elapsed_time, R.percent_complete
from   sys.dm_exec_requests R
       cross apply sys.dm_exec_sql_text(R.sql_handle) T
order by Command

----------------------------------------------------------------------------------------------------



When i query, status was shown as "suspended".
I tried to cancel job by clicking window close button, but did not work.
So i restarted the db server and shrink the log file using the script.


----------------------------------------------------------------------------------------------------

USE [myDb]
GO
DBCC SHRINKFILE (N'myDB_log' , 0, TRUNCATEONLY)
GO

----------------------------------------------------------------------------------------------------


'myDB_log' is the logical name for the log file for the database 'myDB'

Monday, June 18, 2018

Stop Transaction Logs from Growing on a SQL Server

Problem: In a SQL server, you may have seen most of the time space becomes zero due to growth of the log.

Solution: In SQL Server, database transaction log keeps growing until you take a backup of the database. Once taken backup, log files will be truncate. It is not logical to delete transaction log files until you take a backup of the database ( mostly for production class database servers, for test environment transaction logs may not important as production).

Why ?

In a situation, database corruption or loss, you can recreate the database with restoring last database backup and restoring transaction logs on top of that with no loss.

(This is why database files and log files should be in different disks)

But in a test environment ( Other than production ) it is preferred to be automatically truncate the log files which will help to reduce the space used by the database. SQL server let us do this by changing the recovery model.

Set the recovery model = Simple

How to do 

Open SQL server Management studio --> select database --> Right click and select Properties --> Go to Option --> Change "recovery model" to Simple

you can do it in script using below

USE [master]
GO
ALTER DATABASE [myDB] SET RECOVERY SIMPLE WITH NO_WAIT
GO

You can check model with below script

SELECT name, recovery_model_desc 
   FROM sys.databases 
      WHERE name = 'myDB' ;


If Log files has grown, better to shrink after changing the recovery type.


Monday, March 26, 2018

Select = date without timestamp with sql

you may have a data for date column with timestamp
eg: 2018-03-26 13:40:22.470

let's say you need to check the date eg: 2018-03-26

Then you need to take part of the date string. you have various options, here i use convert()

 select count(*)
  -- convert(varchar(10),LastErrorOccurredDate,120)
 from Mytable (nolock)
 where convert(varchar(10),LastErrorOccurredDate,120) ='2018-03-26'

Thursday, March 22, 2018

Extracting list of windows services and their status to a text file

Below will extract details of windows services to a text file

Get-Service | Export-Csv -path "C:\Services\services.csv"

With below you can retrieve the running windows services list

Get-Service | where {$_.Status -eq "Running"} | Export-Csv -path "C:\Service\services.csv"

Friday, January 26, 2018

Error in creating Microsoft SQL server connection

Error:
Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library

This is the basic error and this will have a full stack trace.
When creating database connection, you must have enabled the "integratedSecurity=true".
This will enable LDAP users to access the database.

Solution :



1.goto: https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url#Connectingintegrated
2. Download the JDBC file and extract to your preferred location
3.open the auth folder matching your OS x64 or x86
4. copy sqljdbc_auth.dll file and paste in: C:\Program Files\Java\jdk_version\bin
restart either eclipse or netbeans

In a case when you do not have permission to copy the files to folders.
set thi as a VM argument
In eclipse

1) Create a folder 'sqlauth' in your C: drive, and copy the dll file sqljdbc_auth.dll to the folder
2) Go to Run> Run Configurations
3) Choose the 'Arguments' tab for your class
4) Add the below code in VM arguments:
         -Djava.library.path="C:\\sqlauth"
5) Hit 'Apply' and click 'Run'

https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url#Connectingintegrated
https://blogs.msdn.microsoft.com/jdbcteam/2017/03/31/jdbc-download-center-pages-and-repackaging/



Tuesday, January 23, 2018

Clean Boot

Issue     : the data collector set or one of t its dependencies is already in use
Solution :

Clean boot

Let’s disable all startup items and third party services when booting. This method will help us determine if this issue is caused by a loading program or service. Please perform the following steps:

1. Click the Start Button type "msconfig" (without quotation marks) in the Start Search box, and then press Enter.

Note: If prompted, please click Continue on the User Account Control (UAC) window.

2. Click the "Services" tab, check the "Hide All Microsoft Services" box and click "Disable All" (if it is not gray).

3. Click the "Startup" tab, click "Disable All" and click "OK".

Then, restart the computer. When the "System Configuration Utility" window appears, please check the "Don't show this message or launch the System Configuration Utility when Windows starts" box and click OK.

https://social.technet.microsoft.com/Forums/office/en-US/6c95b8cc-d7f5-46ed-8858-06e4f16204d3/start-and-stop-error-for-performance-counter-in-perfmon-of-windows-server-2008?forum=winserverManagement

Get the queries run by a users

USE master
go
SELECT sdest.DatabaseName
    ,sdes.session_id
    ,sdes.[host_name]
    ,sdes.[program_name]
    ,sdes.client_interface_name
    ,sdes.login_name
    ,sdes.login_time
    ,sdes.nt_domain
    ,sdes.nt_user_name
    ,sdec.client_net_address
    ,sdec.local_net_address
    ,sdest.ObjName
    ,sdest.Query
FROM sys.dm_exec_sessions AS sdes
INNER JOIN sys.dm_exec_connections AS sdec ON sdec.session_id = sdes.session_id
CROSS APPLY (
    SELECT db_name(dbid) AS DatabaseName
        ,object_id(objectid) AS ObjName
        ,ISNULL((
                SELECT TEXT AS [processing-instruction(definition)]
                FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle)
                FOR XML PATH('')
                    ,TYPE
                ), '') AS Query

    FROM sys.dm_exec_sql_text(sdec.most_recent_sql_handle)
    ) sdest
where sdes.session_id <> @@SPID
--and sdes.nt_user_name = '' -- Put the username here !
--ANd login_name ='user'
ORDER BY sdec.session_id