Tag Archives: Microsoft

Convert MS SQL Query to MySQL Automatically

With a recent project, I’ve had to reproduce a table from a Microsoft SQL Database into a MySQL Database.

The table had lots of fields and would be very time consuming to write the SQL to create the table, but luckily I came across this tool to convert MSSQL to MySQL

MS SQL:

CREATE TABLE [dbo].[CRONUS UK Ltd_$Customer](
	[timestamp] [timestamp] NOT NULL,
	[No_] [varchar](20) NOT NULL,
	[Name] [varchar](50) NOT NULL,
	[Search Name] [varchar](50) NOT NULL,
	[Name 2] [varchar](50) NOT NULL,
	[Address] [varchar](50) NOT NULL,
	[Address 2] [varchar](50) NOT NULL,
	[City] [varchar](30) NOT NULL,
	[Contact] [varchar](50) NOT NULL,
	[Phone No_] [varchar](30) NOT NULL,
	[Telex No_] [varchar](20) NOT NULL,
	[Our Account No_] [varchar](20) NOT NULL,
	[Territory Code] [varchar](10) NOT NULL,
	[Global Dimension 1 Code] [varchar](20) NOT NULL,
	[Global Dimension 2 Code] [varchar](20) NOT NULL,
	[Chain Name] [varchar](10) NOT NULL,
	[Budgeted Amount] [decimal](38, 20) NOT NULL,
	[Credit Limit (LCY)] [decimal](38, 20) NOT NULL,
	[Customer Posting Group] [varchar](10) NOT NULL,
	[Currency Code] [varchar](10) NOT NULL,
	[Customer Price Group] [varchar](10) NOT NULL,
	[Language Code] [varchar](10) NOT NULL,
	[Statistics Group] [int] NOT NULL,
	[Payment Terms Code] [varchar](10) NOT NULL,
	[Fin_ Charge Terms Code] [varchar](10) NOT NULL,
	[Salesperson Code] [varchar](10) NOT NULL,
	[Shipment Method Code] [varchar](10) NOT NULL,
	[Shipping Agent Code] [varchar](10) NOT NULL,
	[Place of Export] [varchar](20) NOT NULL,
	[Invoice Disc_ Code] [varchar](20) NOT NULL,
	[Customer Disc_ Group] [varchar](10) NOT NULL,
	[Country_Region Code] [varchar](10) NOT NULL,
	[Collection Method] [varchar](20) NOT NULL,
	[Amount] [decimal](38, 20) NOT NULL,
	[Blocked] [int] NOT NULL,
	[Invoice Copies] [int] NOT NULL,
	[Last Statement No_] [int] NOT NULL,
	[Print Statements] [tinyint] NOT NULL,
	[Bill-to Customer No_] [varchar](20) NOT NULL,
	[Priority] [int] NOT NULL,
	[Payment Method Code] [varchar](10) NOT NULL,
	[Last Date Modified] [datetime] NOT NULL,
	[Application Method] [int] NOT NULL,
	[Prices Including VAT] [tinyint] NOT NULL,
	[Location Code] [varchar](10) NOT NULL,
	[Fax No_] [varchar](30) NOT NULL,
	[Telex Answer Back] [varchar](20) NOT NULL,
	[VAT Registration No_] [varchar](20) NOT NULL,
	[Combine Shipments] [tinyint] NOT NULL,
	[Gen_ Bus_ Posting Group] [varchar](10) NOT NULL,
	[Picture] [image] NULL,
	[Post Code] [varchar](20) NOT NULL,
	[County] [varchar](30) NOT NULL,
	[E-Mail] [varchar](80) NOT NULL,
	[Home Page] [varchar](80) NOT NULL,
	[Reminder Terms Code] [varchar](10) NOT NULL,
	[No_ Series] [varchar](10) NOT NULL,
	[Tax Area Code] [varchar](20) NOT NULL,
	[Tax Liable] [tinyint] NOT NULL,
	[VAT Bus_ Posting Group] [varchar](10) NOT NULL,
	[Reserve] [int] NOT NULL,
	[Block Payment Tolerance] [tinyint] NOT NULL,
	[IC Partner Code] [varchar](20) NOT NULL,
	[Prepayment %] [decimal](38, 20) NOT NULL,
	[Primary Contact No_] [varchar](20) NOT NULL,
	[Responsibility Center] [varchar](10) NOT NULL,
	[Shipping Advice] [int] NOT NULL,
	[Shipping Time] [varchar](32) NOT NULL,
	[Shipping Agent Service Code] [varchar](10) NOT NULL,
	[Service Zone Code] [varchar](10) NOT NULL,
	[Allow Line Disc_] [tinyint] NOT NULL,
	[Base Calendar Code] [varchar](10) NOT NULL,
	[Copy Sell-to Addr_ to Qte From] [int] NOT NULL,
	[Type of Supply Code] [varchar](10) NOT NULL,
	[Liq_ Payment Terms Code] [varchar](10) NOT NULL,
 CONSTRAINT [CRONUS UK Ltd_$Customer$0] PRIMARY KEY CLUSTERED 
(
	[No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

MySQL:

CREATE TABLE `CRONUS UK Ltd_$Customer`(
	`timestamp` `timestamp` NOT NULL,
	`No_` varchar(20) NOT NULL,
	`Name` varchar(50) NOT NULL,
	`Search Name` varchar(50) NOT NULL,
	`Name 2` varchar(50) NOT NULL,
	`Address` varchar(50) NOT NULL,
	`Address 2` varchar(50) NOT NULL,
	`City` varchar(30) NOT NULL,
	`Contact` varchar(50) NOT NULL,
	`Phone No_` varchar(30) NOT NULL,
	`Telex No_` varchar(20) NOT NULL,
	`Our Account No_` varchar(20) NOT NULL,
	`Territory Code` varchar(10) NOT NULL,
	`Global Dimension 1 Code` varchar(20) NOT NULL,
	`Global Dimension 2 Code` varchar(20) NOT NULL,
	`Chain Name` varchar(10) NOT NULL,
	`Budgeted Amount` decimal(38, 20) NOT NULL,
	`Credit Limit (LCY)` decimal(38, 20) NOT NULL,
	`Customer Posting Group` varchar(10) NOT NULL,
	`Currency Code` varchar(10) NOT NULL,
	`Customer Price Group` varchar(10) NOT NULL,
	`Language Code` varchar(10) NOT NULL,
	`Statistics Group` int NOT NULL,
	`Payment Terms Code` varchar(10) NOT NULL,
	`Fin_ Charge Terms Code` varchar(10) NOT NULL,
	`Salesperson Code` varchar(10) NOT NULL,
	`Shipment Method Code` varchar(10) NOT NULL,
	`Shipping Agent Code` varchar(10) NOT NULL,
	`Place of Export` varchar(20) NOT NULL,
	`Invoice Disc_ Code` varchar(20) NOT NULL,
	`Customer Disc_ Group` varchar(10) NOT NULL,
	`Country_Region Code` varchar(10) NOT NULL,
	`Collection Method` varchar(20) NOT NULL,
	`Amount` decimal(38, 20) NOT NULL,
	`Blocked` int NOT NULL,
	`Invoice Copies` int NOT NULL,
	`Last Statement No_` int NOT NULL,
	`Print Statements` `tinyint` NOT NULL,
	`Bill-to Customer No_` varchar(20) NOT NULL,
	`Priority` int NOT NULL,
	`Payment Method Code` varchar(10) NOT NULL,
	`Last Date Modified` datetime NOT NULL,
	`Application Method` int NOT NULL,
	`Prices Including VAT` `tinyint` NOT NULL,
	`Location Code` varchar(10) NOT NULL,
	`Fax No_` varchar(30) NOT NULL,
	`Telex Answer Back` varchar(20) NOT NULL,
	`VAT Registration No_` varchar(20) NOT NULL,
	`Combine Shipments` `tinyint` NOT NULL,
	`Gen_ Bus_ Posting Group` varchar(10) NOT NULL,
	`Picture` blob NULL,
	`Post Code` varchar(20) NOT NULL,
	`County` varchar(30) NOT NULL,
	`E-Mail` varchar(80) NOT NULL,
	`Home Page` varchar(80) NOT NULL,
	`Reminder Terms Code` varchar(10) NOT NULL,
	`No_ Series` varchar(10) NOT NULL,
	`Tax Area Code` varchar(20) NOT NULL,
	`Tax Liable` `tinyint` NOT NULL,
	`VAT Bus_ Posting Group` varchar(10) NOT NULL,
	`Reserve` int NOT NULL,
	`Block Payment Tolerance` `tinyint` NOT NULL,
	`IC Partner Code` varchar(20) NOT NULL,
	`Prepayment %` decimal(38, 20) NOT NULL,
	`Primary Contact No_` varchar(20) NOT NULL,
	`Responsibility Center` varchar(10) NOT NULL,
	`Shipping Advice` int NOT NULL,
	`Shipping Time` varchar(32) NOT NULL,
	`Shipping Agent Service Code` varchar(10) NOT NULL,
	`Service Zone Code` varchar(10) NOT NULL,
	`Allow Line Disc_` `tinyint` NOT NULL,
	`Base Calendar Code` varchar(10) NOT NULL,
	`Copy Sell-to Addr_ to Qte From` int NOT NULL,
	`Type of Supply Code` varchar(10) NOT NULL,
	`Liq_ Payment Terms Code` varchar(10) NOT NULL,
 CONSTRAINT `CRONUS UK Ltd_$Customer$0` PRIMARY KEY 
(
	`No_` ASC
)
)

;

A simple find and replace in the new SQL to correct tinyint, and then the table can be easily created in the MySQL database.

SQL Server – Find Last Backup Date/Time

With SQL server and 3rd party backup software, it can sometimes be difficult to tell when the last SQL backup has been taken and if the backup software has actually performed the backup or not.

There are 3 main backup types in SQL Server:

  • Full
  • Incremental
  • Transaction Log

The following SQL query will show vital statistics, including when the last backup took place for each backup type. Handy for debugging and checking backup strategies..!

This post is not going to go into the details of how each one works, or recommendations for strategies etc. There are lots of details on guides on the internet, such as this one – Microsoft Technet – Understanding SQL Backups.

 SELECT  name ,
            recovery_model_desc ,
            state_desc ,
            d AS 'Last Full Backup' ,
            i AS 'Last Differential Backup' ,
            l AS 'Last log Backup'
    FROM    ( SELECT    db.name ,
                        db.state_desc ,
                        db.recovery_model_desc ,
                        type ,
                        backup_finish_date
              FROM      master.sys.databases db
                        LEFT OUTER JOIN msdb.dbo.backupset a ON a.database_name = db.name
            ) AS Sourcetable 
        PIVOT 
            ( MAX(backup_finish_date) FOR type IN ( D, I, L ) ) AS MostRecentBackup

For example:

SQL Last Backup

Microsoft Dynamics Nav – Either the caller does not have the required permission or the specified path is read-only – Error

Today on Microsoft Dynamics Nav 2013R2, I kept getting the following error, which stumped me for a bit…

Either the caller does not have the required permission or the specified path is read-only.

After much investigation, I found a solution to the problem, delete the following folder and restart the service.

C:ProgramDataMicrosoftMicrosoft Dynamics NAV71Server{Nav Instance Name}usersdefault

Upon restarting the service, I was able to connect back to Nav without receiving the error.

Automatic T-SQL Formatting with Microsoft SQL Server Management Studio 2014

Within MySql Workbench, there’s a feature called “Clean Up SQL” which automatically tidies up your SQL and the indentation automatically.

This is a very useful feature, as your messy SQL then becomes nice and neat. Unfortunately with Microsoft SQL Server Management Studio (SSMS), this feature is not standard.

Poor Mans T-SQL Formatter - Format T-SQL CodeLuckily I came across the following open source T-SQL formatter library, complete with an add-in for SSMS which adds this functionality – Poor Mans T-SQL Formatter. This plugin automatically formats your SQL, adding tabs and linebreaks etc in order to make it easy to read.

After installing the product, the following folder needs renaming in order to work with the 2014 version of SSMS:

%SystemDrive%\ProgramData\Microsoft\SQL Server Management Studio\11.0

To:

%SystemDrive%\ProgramData\Microsoft\SQL Server Management Studio\12.0

This is due to the installer. The installer puts the files in the incorrect place for the 2014 version of SQL Management Studio. Once you have renamed the folder and restarted SSMS, it will automatically add the options to the Tools menu.
Poor Mans T-SQL Formatter File Changes

Not only does this plugin work for SQL Management Studio, there are also versions for:

If you don’t wish to install the software, there’s an online version available.

Dynamics NAV RTC – Page Slow to Load – Flowfields

With Microsoft Dynamics Nav, it is possible to add a “FlowField” to a table. This flow field is then calculates an aggregate of an underlying table.

Typical uses are to perform the following calculations:

  • Sum
  • Average
  • Min
  • Max
  • Count

Additionally, the following non-aggregate functions are available:

  • Lookup
  • Exists

Typical values which can be calculated via flow fields include:

  • Inventory
  • Quantity on Sales Order
  • Quantity on Purchase Order

With some versions of Microsoft Dynamics Nav, opening a page can be extremely slow. If you compare it to an earlier version, say 2009 or 2013, the speed of the earlier version can be a lot quicker.

This is due to the FlowFields on the page. With some versions of Nav, the FlowFields are calculated when you open the page, which can be quite time consuming, especially where there are lots of records. Even if the FlowField is not visible on the page! On earlier versions of Nav, if you hide the field, the FlowField is not calculated, so the issue is not present.

Therefore if you have a page with a flowfield which is not used, instead of hiding it from the page, delete it completely.

Further information of this issue is available here on the Mibuso forum.

Windows 10 – Blurry Fonts at High Resolutions Due to DPI Scaling

With high resolutions in Windows 10, windows will automatically adjust the DPI of the fonts in order to allow you to see these on the screen.

In most cases, this works, however in some specific applications, it can make the fonts all “blurry” and give you a headache when using the machine.

For example, running the Microsoft Dynamics Nav 2013 R2 Development Environment on a high resolution screen, with DPI scaling set at 250% looks as follows. (Click the image to view full size)

Windows 10 - Display Scaling Enabled

A fix for this is to enable the option “Disable display scaling on high DPI settings” within compatibility mode. This is accessed by right clicking on the shortcut and choosing “Properties”:

Windows 10 - Compatibility Settings

Once ticked and you run the application again, the scaling issue for the text is fixed. Unfortunately in this case, it makes some icons smaller (as you can tell by comparing the screenshots), however it is more usable and does not give you a headache!

Windows 10 - Display Scaling Disabled

Access Microsoft Dynamics Nav 2013 R2 From Windows 10, Not on Domain

Edit: The following updated blog post has a better method for achieving this: Run Programs as a Domain user from a none Domain account.

With Microsoft Dynamics Nav 2013R2, Microsoft recommend using a “Pro” version of windows which is registered on an Active Directory domain.

If you are using a “Home” version of windows, when you try to access Nav via the RTC (Role Tailored Client), you get the following error:

You do not have access to Microsoft Dynamics NAV.
Verify that you have been set up as a valid user in Microsoft Dynamics NAV.

Unfortunately “Home” versions of Windows cannot be registered on a domain (Only Pro and above can be). This error occurs because Nav is trying to use the local credentials from the machine across the domain, which will obviously fail.

In order to solve this issue, edit the following file:

%appdata%\Microsoft\Microsoft Dynamics NAV\71\ClientUserSettings.config

Amend the following line:

<add key="ClientServicesCredentialType" value="Windows" />

To the following:

<add key="ClientServicesCredentialType" value="UserName" />

This then will prompt the following credential prompt – once the correct credentials have been provided, you will be able to access Nav from your “Home” edition of windows.

Prompting for Credentials

In order to access Nav via the Development Environment, the answer is quite simple… Just use the Database Server Authentication:

Development Environment Authentication

Estimate Completion Time For Long Running SQL Query

With Microsoft SQL Server, sometimes there can be long running SQL Queries which you would like to get an estimated ETA from.

Tim Ford has wrote a brilliant article which will provide an ETA for the following types of queries:

  • Database Backup
    • BACKUP
  • Database Restore
    • RESTORE
  • Index Reorganisation
    • DBREINDEX
  • DBCC Operations, such as:
    • SHRINKFILE
    • SHRINKDATABASE
    • CHECKDB
    • CHECKTABLE
  • Rollback Operations
    • KILLED/ROLLBACK

Running the following SQL query will give the following information for all transactions which are awaiting completion:

  • Session ID
  • Percentage Complete
  • Time Elapsed in Seconds
  • Wait Type
  • Wait Time
  • Last Wait Type
  • Estimated Completion Time
  • SQL Transaction Text
  • SQL Statement Currently Executing
SELECT R.session_id, 
R.percent_complete, 
R.total_elapsed_time/1000 AS elapsed_secs, 
R.wait_type,
R.wait_time,
R.last_wait_type,
DATEADD(s,100/((R.percent_complete)/ (R.total_elapsed_time/1000)), R.start_time) estim_completion_time,
ST.text, 
SUBSTRING(ST.text, R.statement_start_offset / 2, 
 (
 CASE WHEN R.statement_end_offset = -1 THEN DATALENGTH(ST.text)
 ELSE R.statement_end_offset
 END - R.statement_start_offset 
 ) / 2
) AS statement_executing
FROM sys.dm_exec_requests R
CROSS APPLY sys.dm_exec_sql_text(R.sql_handle) ST
WHERE R.percent_complete > 0
AND R.session_id <> @@spid
OPTION(RECOMPILE);

Azure AD Connect – Force Sync Via Command Line

Azure AD connect is a new tool which replaces DirSync for syncing Active Directory information to Azure from the local active directory.

Synchronisation automatically works in the background, but sometimes you want to push through a synchronisation for some reason or another (rather than waiting for the next time it runs). This can be done by running the following 2 commands from the C:Program FilesMicrosoft Azure AD Syncbin directory:

DirectorySyncClientCmd.exe initial
DirectorySyncClientCmd.exe delta
  • initial – this forces a full sync
  • delta – this forces a delta sync

Example:
Azure AD Connect Command Line

Status can be checked by running “Synchronisation Service” and checking the log. In the example below, 452 entries where unchanged and 1 was deleted as part of the forced sync.
Azure AD Connect Synronisation Service Log

Drop All Triggers from MS SQL Database

Recently I’ve had the scenario where I’ve had to drop all SQL Triggers from a Microsoft SQL database.

This could be done manually through the SQL Management Studio, but with a database with lots of triggers can be time consuming to do this.

The following SQL when ran against the database will drop all triggers from the said database automatically:

 USE [{database Name}]
DECLARE @SQLCmd nvarchar(1000)
DECLARE @Trig sysname
DECLARE @owner sysname
DECLARE @uid int

DECLARE TGCursor CURSOR FOR
SELECT name, uid FROM sysobjects WHERE type = 'TR'
OPEN TGCursor
FETCH NEXT FROM TGCursor INTO @Trig, @uid
WHILE @@FETCH_STATUS = 0
BEGIN

SET @SQLCmd = N'DROP TRIGGER [' + user_name(@uid) + '].[' + @Trig + ']'
EXEC sp_executesql @SQLCmd
PRINT @SQLCmd

FETCH next FROM TGCursor INTO @Trig, @uid
END

CLOSE TGCursor
DEALLOCATE TGCursor