Categories
Microsoft SQL Server

Script to rebuild or reorganize indexes in SQL databases

This link contains useful information and scripts to be used to rebuild or reorganize indexes in SQL databases.

Personally I found really useful the script at point D

https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-db-index-physical-stats-transact-sql?view=sql-server-2017

You can eventually comment adjust the following lines to prepare the scripts without run them

        --EXEC (@command);  
        --PRINT N'Executed: ' + @command;
        PRINT @command; 
        PRINT N'GO';

Did my post help you? Leave a reply.

Categories
Dynamics NAV HOW TO Microsoft SQL Server

HOW TO run SQL query for multiple companies in NAV database

In Micrososoft Dynamics NAV, tables with property DataPerCompany = No are stored in SQL in multiple tables having the company name as prefix, than $ and the name.

Example, the Customer table in Cronus Company:

CRONUS UK Ltd_$Customer

The following T-SQL Script retrieves the list of the companies, loop through them and use the Company Name as prefix (@pref) to run a query (@sql_ext).

DECLARE @pref nvarchar(30)
DECLARE @sql_ext nvarchar(max)

DECLARE company_cursor CURSOR FOR
SELECT replace(Name,'.','_') FROM Company

OPEN company_cursor
FETCH NEXT FROM company_cursor INTO @pref

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @sql_ext = N'SELECT ''' + @pref + ''' as Company,* FROM [dbo].[' + @pref + '$Inquiry Note] WHERE DATALENGTH([Note]) < 4'
EXEC sp_executesql @sql_ext
FETCH NEXT FROM company_cursor INTO @pref
END

CLOSE company_cursor
DEALLOCATE company_cursor

Here find another reading suggestion, this time in C/AL: HOW TO change data for all Companies in NAV

Did my HOW TO help you? Leave a reply.