How to run SQL code in the same transaction as the Nav

rmv_RU
Member Posts: 119
Sometimes we have to use SQL in Nav for different actions including data modification. For example, we have to call a stored procedure to update a web-order status after release a sales order. Unfortunately, there are no way to run the stored procedure in a same transaction as a sales order released by using ADO. Different transactions may cause data integrity problems.
The solution is - Write a SQL statetment into special table from NAV and use a sql trigger on the table to run the statement
1. Create a view in SQL and then link the view in Nav
The main disadvantage of this solution - dynamic sql may cause security problems.
The solution is - Write a SQL statetment into special table from NAV and use a sql trigger on the table to run the statement
1. Create a view in SQL and then link the view in Nav
create view [dbo].[sys_ConnectionProperties] as select cast(serverproperty('servername') as varchar(100)) as server_name, DB_NAME() as db_name, @@SPID as spid , system_USER as [system_user]2. Create a table sql_statement in Nav
spid int sql text(250)3. Write a SQL trigger
alter trigger run_sql on sql_statement for insert, update as begin if not app_name() IN ('Microsoft Business Solutions-Navision client', 'Microsoft Dynamics NAV client', 'Microsoft Business Solutions-Navision', 'Microsoft Dynamics NAV','C/ODBC','C/FRONT','Navision Application Server', 'Microsoft Dynamics NAV Application Server','Navision VIP Application Server') raiserror ('forbidden', 16, 1) declare @spid int declare @sql varchar(250) select @sql=[sql] from inserted where spid=@@SPID --set @sql='raiserror (''zuzu'', 16, 1)' exec (@sql) end4. Write a test codeunit
SysProperties.FIND('-'); SQL.spid:=SysProperties.spid; //make sure that sql update works SQL.sql:='update [your_company_name$item] set description=''changes made by sql'' where No_=''your_item_no'''; IF NOT SQL.INSERT THEN SQL.MODIFY; commit; //make sure that sql update rollback when Nav error occurs SQL.sql:='update [your_company_name$item] set description=''changes made by sql'' where No_=''your_item_no2'''; IF NOT SQL.INSERT THEN SQL.MODIFY; ERROR('error');
The main disadvantage of this solution - dynamic sql may cause security problems.
Looking for part-time work.
Nav, T-SQL.
Nav, T-SQL.
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions