SSRS and SQL and NAV Passed Company Variable as Paramter

pettal
Member Posts: 20
Hi All,
I have created a SQL Function which basically translates the NAV Flowfields into the correct SQL and returns the decimal value. All good, but I tried to paramaterise the Company Name and pass this through the function and it doesn't like it and I can't figure out why.
Currently it works like this and this does work:
Within SSRS I have a SQL Text Query and within that a call to my function:
(Select dbo.ReturnNAVFlowFields('Purchases Cost',CA.[Client No_] ,CA.[Asset Code],@StartDate,@EndDate))
The function looks like this:
IF @FlowField LIKE 'Purchases Cost' BEGIN
SET @Amount = (SELECT ISNULL(SUM([Amount (LCY)]), 0)
FROM [CompanyName$Asset Ledger Entry]
WHERE [Client No_] LIKE @ClientNo
AND [Asset Code] LIKE @AssetCode
AND [Transaction Type] = 0
AND [Posting Date] IS NOT NULL
AND [Posting Date] > @StartDate
AND [Posting Date] < @EndDate)
END
Which works lovely if I only have 1 company... I dont, I have 4 and I don't want to have to repeat code, sql or functions to do it. ](*,)
Any pointers to paramaterise the company name would be hugely appreciated!
I have created a SQL Function which basically translates the NAV Flowfields into the correct SQL and returns the decimal value. All good, but I tried to paramaterise the Company Name and pass this through the function and it doesn't like it and I can't figure out why.
Currently it works like this and this does work:
Within SSRS I have a SQL Text Query and within that a call to my function:
(Select dbo.ReturnNAVFlowFields('Purchases Cost',CA.[Client No_] ,CA.[Asset Code],@StartDate,@EndDate))
The function looks like this:
IF @FlowField LIKE 'Purchases Cost' BEGIN
SET @Amount = (SELECT ISNULL(SUM([Amount (LCY)]), 0)
FROM [CompanyName$Asset Ledger Entry]
WHERE [Client No_] LIKE @ClientNo
AND [Asset Code] LIKE @AssetCode
AND [Transaction Type] = 0
AND [Posting Date] IS NOT NULL
AND [Posting Date] > @StartDate
AND [Posting Date] < @EndDate)
END
Which works lovely if I only have 1 company... I dont, I have 4 and I don't want to have to repeat code, sql or functions to do it. ](*,)
Any pointers to paramaterise the company name would be hugely appreciated!
0
Comments
-
You can use
SET @sql = 'SELECT * FROM ' + @tablename EXEC(@sql) -- execure the query
The other option as you mentioned is to have a case statement and list all the companies queries repeated.0 -
Thanks for the response.
I did look at the first option you gave of using a dynamic SQL query within the function or even re-writing as a stored proc but had difficulty calling the dynamic query from within the report. The error it gave intimated that I would be better using a SP from within the report at which point I figured I might as well write the whole thing as an SP in the first place!
Ok, thanks for your help I will go back to the drawing board. I thought there might have been a simpler solution that I was just missing but it appears I may as well go back and start again. :roll:
Thanks for your help!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