Maintainable SQL queries on a NAV db
 
            
                
                    Miklos_Hollender                
                
                    Member Posts: 1,598                
            
                        
            
                    Taking some further steps towards discovering Reporting Services and T-SQL, the second bump I found on the road is that it's better not to hardcode the database and company name in the query - it means we have to use EXEC and string concatenation, but that generally looks like line noise, a total mess, so we need to find a more readable, maintanable approach.
After much ](*,) and :-k and :-# what I managed to find is the following.
Can someone perhaps suggest a simpler approach? (Yes, that stuff about putting the db and the company into a setup table I already tried and I think it's better to have them as a report parameter.)
(This query takes a year and a month as a parameter and shows the total sales from Cust. Ledger Entry to the end of that month in that year and in the previous year.)
                After much ](*,) and :-k and :-# what I managed to find is the following.
Can someone perhaps suggest a simpler approach? (Yes, that stuff about putting the db and the company into a setup table I already tried and I think it's better to have them as a report parameter.)
(This query takes a year and a month as a parameter and shows the total sales from Cust. Ledger Entry to the end of that month in that year and in the previous year.)
CREATE FUNCTION NaviTable 
(
    @db varchar(50),
    @company varchar(50),
    @table varchar(50),
    @alias varchar(50)
)
RETURNS varchar(200)
AS
BEGIN
	DECLARE @retval varchar(200)
        SET @retval = ' [' + @db+ '].[dbo].['+@company +'$' + @table +'] ' + @alias + ' '
        RETURN @retval
END
CREATE PROCEDURE SalesByYearMonth
   @year integer,
   @month integer,
   @company varchar(50),
   @database varchar(50)
as 
declare @prevyear integer
declare @cleThisYear varchar(200)
declare @cleLastYear varchar(200)
set @prevyear = @year-1
set @cleThisYear = dbo.NaviTable(@database, @company, 'Cust_ Ledger Entry', 'cleThisYear')
set @cleLastYear = dbo.NaviTable(@database, @company, 'Cust_ Ledger Entry', 'cleLastYear')
exec( '
select
  (select
     sum(cleThisYear.[Sales (LCY)])
   from' 
     + @cleThisYear  + '
  where
   
     datepart(yyyy, cleThisYear.[Posting Date])= ' + @year +'
     and datepart(month,cleThisYear.[Posting Date])<=' + @month + ' ) as SalesThisYear,
  (select
     sum(cleLastYear.[Sales (LCY)])
  from' 
     + @cleLastYear  + '
  where 
  
     datepart(yyyy, cleLastYear.[Posting Date])=' + @prevyear +'
     and datepart(month,cleLastYear.[Posting Date])<=' + @month+ ') as SalesLastYear
') -- end of exec
                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
- 323 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