Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

What Is An Index?

 


What Is An Index?



A SQL index is used to retrieve data from a database very fast. Indexing a table or view is, without a doubt, one of the best ways to improve the performance of queries and applications.

A SQL index is a quick lookup table for finding records users need to search frequently. An index is small, fast, and optimized for quick lookups. It is very useful for connecting relational tables and searching large tables.

SQL indexes are primarily a performance tool, so they really apply if a database gets large. SQL Server supports several types of indexes but one of the most common types is the clustered index. This type of index is automatically created with a primary key. 

Customer Address book relation for channel Database D365FO


Customer Address book relation for channel Database D365FO



Scenario:

Most of our D365FO Fellow are working with Retail where we sync customer with our channel database, and sometime we need to update the customer from POS as well, Sometime channel DB throw exception when while updating the customer information if AddressBook of the customer  customer is not properly set.

Its happens when we create customer using AX client or import them via DMF and didn't set the address book value.


So no need to worry using below code & query you can update the customers address book in bulk.

Image:




Code:

public void AddrssBookRelation()
    {
        DirAddressBookParty       dirAddressBookParty;
        DirAddressBook                   dirAddressBook;
        CustTable                              custTable;


        select RecId,Party from custTable
            where custTable.AccountNum==this.CustomerAccount;

        if(custTable)
        {
            select RecId from dirAddressBook
            where dirAddressBook.Name==this.CountryCode;

            if(dirAddressBook)
            {
                select RecId from dirAddressBookParty
                    where dirAddressBookParty.Party==custTable.Party && dirAddressBookParty.AddressBook==dirAddressBook.RecId;

                if(dirAddressBookParty.RecId==0)
                {
                    dirAddressBookParty.clear();
                    dirAddressBookParty.Party=custTable.Party;
                    dirAddressBookParty.AddressBook=dirAddressBook.RecId;
                    dirAddressBookParty.insert();
                }

            }
        }

    }



SQL Query:

INSERT INTO DirAddressBookParty (PARTY,ADDRESSBOOK,PARTITION)
select DISTINCT DirPartyLocation.PARTY,DirAddressBook.RECID,5637144576 from LogisticsPostalAddress
join DirPartyLocation on DirPartyLocation.LOCATION=LogisticsPostalAddress.LOCATION
Join LOGISTICSLOCATION on LOGISTICSLOCATION.RECID=LogisticsPostalAddress.LOCATION
join DirAddressBook on DirAddressBook.NAME=LogisticsPostalAddress.COUNTRYREGIONID
where DirPartyLocation.ISPRIMARY=1 and party in (select party from CUSTTABLE)
and LOGISTICSLOCATION.ISPOSTALADDRESS=1 and DirPartyLocation.ISPOSTALADDRESS=1 and DirPartyLocation.POSTALADDRESSROLES='Business'
and not exists (select 1 from DirAddressBookParty where DirAddressBookParty.PARTY=DirPartyLocation.PARTY and DirAddressBookParty.ADDRESSBOOK=DirAddressBook.RECID)
 and LogisticsPostalAddress.VALIDTO >=GETDate()

SQL In Operator in D365FO


SQL In Operator in D365FO


Microsoft Introduce In Operator in X++ Syntax, but it will work with Enums only.

Following are the example how you can use this


SalesTable  salesTable;
container   con = [SalesType::Sales, SalesType::ReturnItem, SalesType::Subscription];
while select SalesId from salesTable
where salesTable.SalesType in con
{
Info(salesTable.SalesId);
}

Generate class from SQL database table


Generate class from SQL database table




declare @TableName sysname = 'BANKACCOUNT'
declare @Result varchar(max) = 'public class ' + UPPER(LEFT(@TableName,1))+LOWER(SUBSTRING(@TableName,2,LEN(@TableName)))  + 'Entity

{'

select @Result = @Result + '
    public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
    select
        replace(col.name, ' ', '_') ColumnName,
        column_id ColumnId,
        case typ.name
            when 'bigint' then 'long'
            when 'binary' then 'byte[]'
            when 'bit' then 'bool'
            when 'char' then 'string'
            when 'date' then 'DateTime'
            when 'datetime' then 'DateTime'
            when 'datetime2' then 'DateTime'
            when 'datetimeoffset' then 'DateTimeOffset'
            when 'decimal' then 'decimal'
            when 'float' then 'float'
            when 'image' then 'byte[]'
            when 'int' then 'int'
            when 'money' then 'decimal'
            when 'nchar' then 'char'
            when 'ntext' then 'string'
            when 'numeric' then 'decimal'
            when 'nvarchar' then 'string'
            when 'real' then 'double'
            when 'smalldatetime' then 'DateTime'
            when 'smallint' then 'short'
            when 'smallmoney' then 'decimal'
            when 'text' then 'string'
            when 'time' then 'TimeSpan'
            when 'timestamp' then 'DateTime'
            when 'tinyint' then 'byte'
            when 'uniqueidentifier' then 'Guid'
            when 'varbinary' then 'byte[]'
            when 'varchar' then 'string'
            else 'UNKNOWN_' + typ.name
        end ColumnType,
        case
            when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
            then '?'
            else ''
        end NullableSign
    from sys.columns col
        join sys.types typ on
            col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
    where object_id = object_id(@TableName)
) t
order by ColumnId

set @Result = @Result  + '
}'

print @Result






public class BankaccountEntity

{
    public int ID { get; set; }

    public string BANK { get; set; }

    public string ROUNTINGNUM { get; set; }

    public string CURRENCY { get; set; }

    public string ACCOUNTNUM { get; set; }

    public int? MAINACCOUNTID { get; set; }

    public string IBAN { get; set; }

    public string SWIFT { get; set; }

    public string BRANCH { get; set; }

    public string CONTACTPERSON { get; set; }

    public string CONTACTPHONE { get; set; }

    public string CONTACTEMAIL { get; set; }

    public bool? STATUS { get; set; }

}


D365FO One Box VM SQL Password


D365FO One Box VM SQL Password


We can open SQL Server Management Studio on you D365FO Local machine Via Windows Authentication and Using SQL server Authentication...


Window Authentication 

For windows Authentication you have to open SQL server as Run as Administrator otherwise you will get authentication Error...


SQL Server Management Authentication

For SQL server Authentication, Use below user name & password

USERNAMEAOSUSER
PASSWORDAOSWebSite@123


The database principal owns a fulltext catalog in the database, and cannot be dropped.




The database principal owns a fulltext catalog in the database, and cannot be dropped.

Problem



Msg 15138, Level 16, State 1, Line 1
    The database principal owns a fulltext catalog in the database, and cannot be dropped.

Resolution 


Find the catalog name and change the owner ship like below

Run the command on your database 
select * from sys.fulltext_catalogs


Now change the ownership with below script

Example
ALTER AUTHORIZATION ON Fulltext Catalog::[CatalogName] TO [dbo];  


Script should be in my case like

ALTER AUTHORIZATION ON Fulltext Catalog::[COMMERCEFULLTEXTCATALOG] TO [dbo];  
ALTER AUTHORIZATION ON Fulltext Catalog::[SqlTempDBForSync_catalog] TO [dbo];  


Virtual Fields Vs Computed Fields

  Virtual Field: A virtual field in D365FO is a field that doesn't have a direct representation in the database. It's a field that y...