AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 01.04.2011, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,643 / 848 (80) +++++++
Регистрация: 28.10.2006
sumitsaxfactor: Handling RecIds in SQL Server
Источник: http://sumitsaxfactor.wordpress.com/...in-sql-server/
==============

I have already written one article on handling RecIds in AX (click here to read it).

There I have mentioned a method to handle RecIds using SQL. I am writing this article after actually implementing it .

The need arose because we had some recalculations and data inserts required for one of our clients (almost more than 2 million records).

Last time when we had run the script to perform this task in AX, it took us 4 days to complete the whole task. So we decided to try and perform these tasks using SQL Scripts for better performance.

I am not writing that script here but I will take an example and explain you how we can handle RecIds through SQL Server.

Let us assume that we need to backup current customer master to a new table. For this purpose I created a new table named SGXCustTable with following fields:
  • AccountNo
  • Name
  • CreatedTime
  • ModifiedTime
  • CreatedDate
  • ModifiedDate
  • CreatedDateTime (Assuming CustTable has this property enabled)
  • ModifiedDateTime(Assuming CustTable has this property enabled)
  • CreatedTransactionId
  • ModifiedTransactionId
  • CreatedBy
  • ModifiedBy
  • DataAreaId
  • RecVersion
  • RecId
Now run the following SQL Script and you will see that the data is getting inserted properly and quickly. That is the power of SQL. You will find comments inline that will help you understand each step in SQL query.

X++:
DECLARE @NEXTVAL BIGINT, @ROWCOUNT BIGINT, @NEXTVALTRANS BIGINT

 

DROP TABLE #TEMP

 

First insert all the required data into a temporary table with RecId as an identity field

Note: *CreatedTime and ModifiedTime are stored as integers hence convert them to corresponding integer

      *RecId field is made as an identity field that will be used later for calculating Record Ids

          *GETDATE() when used with date field automatically converts to date and with datetime field automatically gets datetime

SELECT [ACCOUNTNUM], [NAME], [MODIFIEDTIME]=(DATEPART(hour, GETDATE()) * 3600)+(DATEPART(MINUTE, GETDATE()) * 60)+DATEPART(SECOND, GETDATE()),

         [CREATEDTIME]=(DATEPART(hour, GETDATE()) * 3600)+(DATEPART(MINUTE, GETDATE()) * 60)+DATEPART(SECOND, GETDATE()),

         [CREATEDDATE]=GETDATE(),[MODIFIEDDATE]=GETDATE(), [CREATEDDATETIME],[MODIFIEDDATETIME],

         [CREATEDTRANSACTIONID]=0,[MODIFIEDTRANSACTIONID]=0,[CREATEDBY]=,[MODIFIEDBY]=, [DATAAREAID], [RECVERSION],

         [RECID] = IDENTITY(BIGINT,0,1)

INTO #TEMP

FROM CUSTTABLE WHERE CUSTTABLE.DATAAREAID = 

 

The Next RecId value is stored in SystemSequences Table

Get the Next RecId and store it in a variable

Note the TABID will be the table id into which records are being inserted

SELECT @NEXTVAL=NEXTVAL

FROM SYSTEMSEQUENCES 

WITH(UPDLOCK, HOLDLOCK) WHERE ID = -1 AND TABID = 50051

 

Note that this is required for ModifiedTransactionId and CreatedTransactionId

The sequence for this is stored per company with TABID as 0 (Independent of tables) and ID = -2



SELECT @NEXTVALTRANS=NEXTVAL

FROM SYSTEMSEQUENCES 

WITH(UPDLOCK, HOLDLOCK) WHERE TABID = 0 AND ID = -2

 

Now read all the records from temporary table and insert into the required table

Note that RecId, CreatedTransactionId and ModifiedTransactionId are made using the NextVal 

fetched from SystemSequences and Identity column

INSERT INTO SGXCUSTTABLE (ACCOUNTNO, NAME, MODIFIEDTIME, CREATEDTIME, CREATEDDATE, MODIFIEDDATE,

                                    CREATEDDATETIME, MODIFIEDDATETIME, CREATEDTRANSACTIONID, MODIFIEDTRANSACTIONID,

                                    CREATEDBY, MODIFIEDBY, DATAAREAID, RECVERSION, RECID)

SELECT [ACCOUNTNUM], [NAME], [MODIFIEDTIME],[CREATEDTIME],[CREATEDDATE],[MODIFIEDDATE],[CREATEDDATETIME],

         [MODIFIEDDATETIME],[CREATEDTRANSACTIONID]=@NEXTVALTRANS+[RECID],[MODIFIEDTRANSACTIONID]=@NEXTVALTRANS+[RECID],

         [CREATEDBY],[MODIFIEDBY], [DATAAREAID], [RECVERSION],[RECID] = @NEXTVAL + [RECID]

FROM #TEMP

 

Select the number of records inserted

SELECT @ROWCOUNT = COUNT(*) FROM #TEMP

 

Update the used number sequences back to SystemSequences table

UPDATE SYSTEMSEQUENCES

SET NEXTVAL=NEXTVAL + @ROWCOUNT 

WHERE ID = -1 AND TABID = 50051

 

UPDATE SYSTEMSEQUENCES

SET NEXTVAL=NEXTVAL + @ROWCOUNT 

WHERE TABID = 0 AND ID = -2 AND DATAAREAID = 

 

SELECT * FROM SGXCUSTTABLE


Источник: http://sumitsaxfactor.wordpress.com/...in-sql-server/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
CRM DE LA CREME! CRM 4.0 Disaster Recovery Blog bot Dynamics CRM: Blogs 2 26.02.2016 08:23
Connection к другому SQL Server Poleax DAX: Программирование 5 19.10.2010 10:49
Microsoft Dynamics CRM Team Blog: Building a Self-Contained Virtual CRM Development Server Blog bot Dynamics CRM: Blogs 0 05.05.2009 10:05
Dynamics AX: SQL Server, Heart of Dynamics AX Blog bot DAX Blogs 0 13.07.2007 18:00
aEremenko: Диагностика проблем при установке Microsoft Dynamics Ax 4.0 на Microsoft SQL Server 2005 Blog bot DAX Blogs 0 28.10.2006 16:01
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 21:00.