How we can perform CRUD and Join operations using C/AL in NAV 2016? - dynamics-nav

Currently I am working on NAV 2016. I came across some C/AL functions to Insert, Delete, Modify data from the table after retrieving them statically.
I want to know is there any use of SQL statement in C/AL to perform CRUD and join operations or we have to rely on C/AL inbuilt functions only?

Only c/al.
No join.
Fo read operations you can use Query object. It’s highly limited version of the SQL’s query. But it can do join.

Related

How to use SQL Server apply in typeorm

For a project I'm assigned to I have to alter a typeorm querybuilder to incorporate an "outer apply".
I could not find any support in typeorm for this.
I've looked for ways to do this by a join and a sub-query in the "on" of the join but the amount off data in the DB generates a timeout using multiple sub-queries.
Furthermore, I looked into inserting a RAW query into the builder but could not find the mechanism for this.
Is there any way to use "apply" with typeorm?
I personally prefer to write all my queries by hand so if you come up with reasons NOT to use typeorm, I agree!! But this is an existing project and no way I'm going to rewrite it!

Very slow queries in MS Access with joined MS SQL table via ODBC

What is the best solution when I would like to use an Access front-end application with some linked table (via ODBC) from MSSQL Server?
The difficulty of this for me is that I have to use complex queries with many multiple joins (and functions called from queries).
It is very-very slow because of the joins between the two DB (and there is a lot of data in some tables, the 2 GB Access mdb limit is the reason of the MSSQL DB upgrade).
Pass-through query doesn't help because of the joined Access tables.
With OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0'... it is still slow in SQL Server too. I tried ODBC linked view with WHERE clause from MSSQL, but it
seems as slow as the full table.
I have to move all of joined Access tables to the MSSQL DB and convert all queries to Pass-Through? Is there any other solution?
I have to move all of joined Access tables to the MSSQL DB
Yes, definitely.
and convert all queries to Pass-Through?
Not necessarily, only those that are still slow.
"Normal" INNER JOIN queries, using only linked tables from one server database, are handled by Access and the ODBC driver in a way that everything is processed on the server. They should be (more or less) as fast as when run on the server (or as Pass-Through query).
Only "complex" queries, especially involving multiple INNER and OUTER JOINs, won't work like that. You'll notice that they are still very slow when running on linked tables. These need to be changed to Pass-Through queries.
Edit: I just noticed
functions called from queries
You can't call VBA functions from PT queries, and they will again kill performance when called from Access queries running on linked MSSQL tables (because they have to be processed locally).
You'll need to learn to create views in MSSQL, probably also user defined functions and/or stored procedures.
In the long run, you'll find that views are actually easier to manage than PT queries.

Is there a way to automatically join many tables in SQL Server?

I just imported ~50 tables; each table has 2 common foreign keys (making each record unique). My goal is to setup a query that joins all these tables; I obviously don't want to have to this manually and was thinking about setting up a procedure that loops through all tables to dynamically build this query ... is this the best way or is there an obvious solution I'm not seeing? Thanks you
No there is not. You have to manually write the query to JOIN the tables.
You can also check Automatically Generate a Set of Join Filters Between Merge Articles (SQL Server Management Studio) but I am not sure if that is going to help.

What is the reasoning for using OPENQUERY within a tsql stored procedure?

I am currently reviewing some jobs that run stored procedures on a database. All of these stored procedures are connecting to a linked server(s). I am not too familiar with this functionality. I am at the moment attempting to determine why these were used versus just a normal query as the queries I am running seem to be pulling in the data.
I read this, which is MSDNs explanation of openquery. :
http://technet.microsoft.com/en-us/library/ms188427.aspx
I also read this, which is a stackoverflow link talking about why not to use it on local server. :
Why is using OPENQUERY on a local server bad?
My question is do you basically just use this when the stored procedure requires the embedded credentials of the linked server? Or are there more reasons for using OpenQuery that I am not aware of?
Two advantages I can think of using openquery. It can reduce the amount of data you'd need to transfer by doing the necessary filtering on the remote server. It can allow the query optimizer on the remote server to choose the optimal execution plan when joining tables.
The other alternative is using REMOTE JOIN
I've had some luck using it but Aaron Bertrand has a nice write up about it here.. http://www.mssqltips.com/sqlservertip/2765/revisit-your-use-of-the-sql-server-remote-join-hint/
Here is the MS documentation
REMOTE
Specifies that the join operation is performed on the site of the right table. This is useful when the left table is a local table and the right table is a remote table. REMOTE should be used only when the left table has fewer rows than the right table.
If the right table is local, the join is performed locally. If both tables are remote but from different data sources, REMOTE causes the join to be performed on the site of the right table. If both tables are remote tables from the same data source, REMOTE is not required.
REMOTE cannot be used when one of the values being compared in the join predicate is cast to a different collation using the COLLATE clause.
REMOTE can be used only for INNER JOIN operations.

How to execute dynamic .net code in clr stored procedure in SQL Server

Is there a way to change the code of CLR procedure in SQL Server dynamically?
Suppose you have an assembly with your business logic deployed in MS SQL Server 2008 R2. This assembly (or assemblies) is being used constantly (for example calling some functions for each row of a table in multiple concurrent queries). So you cannot just drop assembly. Is there a way to change my business logic dynamicly or some way to execute external changable code?
I've already explored these approaches, but none worked:
Reflection.Emit
Mono.Cecil
Loading external assembly in the assembly deployed in SQL Server
UPDATE:
The question was not about release process: I want to be able to set some security rules dynamically via GUI.
For instance some users should be able to see only clients without their addresses or the transactions within the last year and so on.
The rules are not complicated but they may change almost every day and we cannot put them in the code. The rest of the business logic is implemented in TSQL. CLR was chosen because of the performance issue (dynamic SQL is too slow).
There was another option: generate clustered views (with rules in WHERE section) but it was not quick enough.
Some more details:
Suppose we have some code selecting a part of big table dbo.Transactions
select *
from dbo.Transactions
where ... --filters from your business logic
If we want to filter the result to show allowed rows we could generate some indexed view and join it with the result set like this:
select *
from dbo.Transactions t
inner join dbo.vw_Transactions v
on t.id = v.id
where ... --filters from your business logic
But if we check the execution plan in most cases the query analyzer decides not to filter dbo.Transaction and then join with vw_Transactions, but to join first and filter later (which is absolutely not desirable). Hints like FORCE ORDER doesn't help.
I'm not a CLR assembly expert, but the obvious options are:
ALTER ASSEMBLY
Drop and re-create the assembly inside a transaction
Define a maintenance window and deploy it then
gbn's point about release processes is a good one. If your procedures (and therefore your business operations) are really constantly running 24x7 then presumably you already have some form of system redundancy and established maintenance procedures for patching and upgrading applications? If so, just deploy your new code in your usual maintenance window.
There's a good library for dynamic evaluating arithmetic expression (with parameters) - Flee
In my case I didn't have to execute any .Net code - just the expressions like "Date > '20100101' Or Status = 2", so Flee satisfies almost completely. The only issue is that its logical operators don't work with SqlBoolean type (which is used in sql expressions) but it's not a big deal to add this feature.
But in general case it's seems to be impossible to execute dynamic .Net code inside Sql Server host.

Resources