Allow mulitple values not working in SSRS - sql

I have a report that uses a multi value parameter when ran in SQL pulls data without issue. When applied in a Stored Procedure and processed through SSRS with "Allow Multi Values" enabled when selecting multiple values the report returns no data. Like other users if I select a single value the report will return data.
I have done some homework and tried increasing my parameter value but this did not help either.
Please see below for my query. The parameter in question is the #material parameter.
With Zone as
(Select
name,
id
From datex_Footprint.LocationContainersView
Where
typename IN ('Zone', 'Area')
)
SELECT inv.projectId,
inv.projectName,
inv.materialLookupCode,
inv.materialDescription,
CASE
WHEN sn.id IS NULL
THEN inv.totalPackagedAmount
ELSE 1
END AS totalPackagedAmount,
inv.licensePlateLookupCode,
z.name WarehouseZone,
inv.locationName,
sn.id serialNumberId,
sn.lookupCode,
udf.SerialRef1,
udf.SerialRef2,
udf.SerialRef3,
udf.SerialRef4,
udf.SerialRef5
FROM datex_footprint.InventoryDetailedViewByLicensePlateLot inv
LEFT OUTER JOIN datex_footprint.SerialNumbers sn ON sn.lotId = inv.lotId
AND sn.licensePlateId = inv.licensePlateId
AND sn.archived = 'False'
LEFT OUTER JOIN datex_footprint.SerialNumbersUdfs udf ON udf.id = sn.id
INNER JOIN datex_footprint.LocationContainersView lcv ON lcv.id = inv.locationId
INNER JOIN datex_footprint.locationcontainers LC ON LC.id = lcv.id
INNER JOIN Zone z ON z.id = LC.parentid
WHERE inv.projectName IN(#projectName)
AND inv.materialLookupCode IN(#material)
AND (inv.locationName IN(#locationName)
OR (#locationName IS NULL));

If you don't have to use a stored procedure then the easiest way of solving this would be to put this code directly into your dataset. It will then work as you expect, SSRS will convert you multi-value parameter into a comma separated list and inject it into the SQL statement.
If you have to use the stored proc then you will need to do all the conversion yourself including splitting the values up inside your stored proc. There are plenty of examples out there on how to do this such as http://www.codeulike.com/2012/03/ssrs-multi-value-parameters-with-less.html
Personally, I always put dataset code directly in the dataset query, it's more convenient, means less objects on the SQL server but could be less secure.

Related

Should I use an SQL full outer join for this?

Consider the following tables:
Table A:
DOC_NUM
DOC_TYPE
RELATED_DOC_NUM
NEXT_STATUS
...
Table B:
DOC_NUM
DOC_TYPE
RELATED_DOC_NUM
NEXT_STATUS
...
The DOC_TYPE and NEXT_STATUS columns have different meanings between the two tables, although a NEXT_STATUS = 999 means "closed" in both. Also, under certain conditions, there will be a record in each table, with a reference to a corresponding entry in the other table (i.e. the RELATED_DOC_NUM columns).
I am trying to create a query that will get data from both tables that meet the following conditions:
A.RELATED_DOC_NUM = B.DOC_NUM
A.DOC_TYPE = "ST"
B.DOC_TYPE = "OT"
A.NEXT_STATUS < 999 OR B.NEXT_STATUS < 999
A.DOC_TYPE = "ST" represents a transfer order to transfer inventory from one plant to another. B.DOC_TYPE = "OT" represents a corresponding receipt of the transferred inventory at the receiving plant.
We want to get records from either table where there is an ST/OT pair where either or both entries are not closed (i.e. NEXT_STATUS < 999).
I am assuming that I need to use a FULL OUTER join to accomplish this. If this is the wrong assumption, please let me know what I should be doing instead.
UPDATE (11/30/2021):
I believe that #Caius Jard is correct in that this does not need to be an outer join. There should always be an ST/OT pair.
With that I have written my query as follows:
SELECT <columns>
FROM A LEFT JOIN B
ON
A.RELATED_DOC_NUM = B.DOC_NUM
WHERE
A.DOC_TYPE IN ('ST') AND
B.DOC_TYPE IN ('OT') AND
(A.NEXT_STATUS < 999 OR B.NEXT_STATUS < 999)
Does this make sense?
UPDATE 2 (11/30/2021):
The reality is that these are DB2 database tables being used by the JD Edwards ERP application. The only way I know of to see the table definitions is by using the web site http://www.jdetables.com/, entering the table ID and hitting return to run the search. It comes back with a ton of information about the table and its columns.
Table A is really F4211 and table B is really F4311.
Right now, I've simplified the query to keep it simple and keep variables to a minimum. This is what I have currently:
SELECT CAST(F4211.SDDOCO AS VARCHAR(8)) AS SO_NUM,
F4211.SDRORN AS RELATED_PO,
F4211.SDDCTO AS SO_DOC_TYPE,
F4211.SDNXTR AS SO_NEXT_STATUS,
CAST(F4311.PDDOCO AS VARCHAR(8)) AS PO_NUM,
F4311.PDRORN AS RELATED_SO,
F4311.PDDCTO AS PO_DOC_TYPE,
F4311.PDNXTR AS PO_NEXT_STATUS
FROM PROD2DTA.F4211 AS F4211
INNER JOIN PROD2DTA.F4311 AS F4311
ON F4211.SDRORN = CAST(F4311.PDDOCO AS VARCHAR(8))
WHERE F4211.SDDCTO IN ( 'ST' )
AND F4311.PDDCTO IN ( 'OT' )
The other part of the story is that I'm using a reporting package that allows you to define "virtual" views of the data. Virtual views allow the report developer to specify the SQL to use. This is the application where I am using the SQL. When I set up the SQL, there is a validation step that must be performed. It will return a limited set of results if the SQL is validated.
When I enter the query above and validate it, it says that there are no results, which makes no sense. I'm guessing the data casting is causing the issue, but not sure.
UPDATE 3 (11/30/2021):
One more twist to the story. The related doc number is not only defined as a string value, but it contains leading zeros. This is true in both tables. The main doc number (in both tables) is defined as a numeric value and therefore has no leading zeros. I have no idea why those who developed JDE would have done this, but that is what is there.
So, there are matching records between the two tables that meet the criteria, but I think I'm getting no results because when I convert the numeric to a string, it does not match, because one value is, say "12345", while the other is "00012345".
Can I pad the numeric -> string value with zeros before doing the equals check?
UPDATE 4 (12/2/2021):
Was able to finally get the query to work by converting the numeric doc num to a left zero padded string.
SELECT <columns>
FROM PROD2DTA.F4211 AS F4211
INNER JOIN PROD2DTA.F4311 AS F4311
ON F4211.SDRORN = RIGHT(CONCAT('00000000', CAST(F4311.PDDOCO AS VARCHAR(8))), 8)
WHERE F4211.SDDCTO IN ( 'ST' )
AND F4311.PDDCTO IN ( 'OT' )
AND ( F4211.SDNXTR < 999
OR F4311.PDNXTR < 999 )
You should write your query as follows:
SELECT <columns>
FROM A INNER JOIN B
ON
A.RELATED_DOC_NUM = B.DOC_NUM
WHERE
A.DOC_TYPE IN ('ST') AND
B.DOC_TYPE IN ('OT') AND
(A.NEXT_STATUS < 999 OR B.NEXT_STATUS < 999)
LEFT join is a type of OUTER join; LEFT JOIN is typically a contraction of LEFT OUTER JOIN). OUTER means "one side might have nulls in every column because there was no match". Most critically, the code as posted in the question (with a LEFT JOIN, but then has WHERE some_column_from_the_right_table = some_value) runs as an INNER join, because any NULLs inserted by the LEFT OUTER process, are then quashed by the WHERE clause
See Update 4 for details of how I resolved the "data conversion or mapping" error.

Using Microsoft Query in Excel to Add Date Parameters to SQL Query

I often use the function (now legacy) in Excel to get data from SQL Server where I paste an actual SQL statement into the Excel sheet and it works fine. I have been researching how to do this with queries I have that have date parameters that need to be changed each time a report is ran and at first it seemed like using Microsoft Query in Excel would be the best option. This would use the '?' instead of the dates themselves and allow for adding parameters. Whenever I try to do this with the below query I get the error "Parameters are not allowed in queries that can't be displayed graphically." I honestly have no idea what that means but would value any input. My Query is below. Thanks
SELECT E.TEAM_MEMBER_NAME AS 'PURCHASER',
M.DEPARTMENT,
M.BUSINESS_SEGMENT_CODE,
KB.BUSINESS_SEGMENT_DESC,
KG.GENDER_DESC,
MR.PLANT_CODE [PLANT],
MR.STOCK_CATEGORY,
M.MATERIAL,
M.[DESCRIPTION],
M.COLOR_1,
M.COLOR_2,
MR.SIZE_LITERAL,
MR.QUANTITY,
M.STANDARD_COST,
M.DEALER_PRICE,
M.CURRENT_SEASON,
MR.STOCK_NUMBER AS 'AFS PO #',
H.PO_CREATED_BY,
H.PO_TYPE,
MR.MRP_INDICATOR,
MR.STOCK_TYPE,
H.PO_ISSUE_DATE
FROM PDX_SAP_USER..VW_MRP_ALLOCATION MR
JOIN PDX_SAP_USER..VW_MM_MATERIAL M ON MR.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..VW_KD_BUSINESS_SEGMENT KB ON M.BUSINESS_SEGMENT_CODE = KB.BUSINESS_SEGMENT_CODE
JOIN PDX_SAP_USER..VW_KD_GENDER KG ON M.GENDER_CODE = KG.GENDER_CODE
JOIN PDX_SAP_USER..VW_PO_HEADER H ON MR.STOCK_NUMBER = H.PO_NUMBER
JOIN ADI_USER_MAINTAINED..SCM_PO_EMPLOYEE_NAME E ON MR.STOCK_NUMBER = E.PO_NUMBER
WHERE M.BUSINESS_SEGMENT_CODE NOT IN ('420','421','422','424')
AND MR.STOCK_CATEGORY NOT LIKE 'A60383%'
AND MR.STOCK_CATEGORY NOT IN ('A60382001','A60380070')
AND M.MATERIAL NOT IN ('AY1480','CD4683')
AND H.PO_TYPE NOT IN ('02','06','10','UB','DB')
AND MR.MRP_INDICATOR IN ('A','N')
AND MR.STOCK_TYPE = 'B'
AND MR.QUANTITY >= 50
AND H.PO_ISSUE_DATE BETWEEN '09/26/2018' AND '10/10/2018'
ORDER BY MR.QUANTITY DESC
I got it to work...a bit of a work around. I had my DBA create a view on our server and selected all from that view. I then used MS Query to bring in the data and replaced the dates with ?. From there in the data source within Excel you can assign those question marks to cells in which you enter your dates. Works like a charm. And not taking credit - all credit goes to:
https://www.youtube.com/watch?v=xPalEw4xw1w
Short answer is that I've researched it at the time as well, trying my best to pass parameters (let's say, 'Sheet1!A1' cell) and couldn't. There is no real way to do it UNLESS you're using the Power Query as well as using an SQL stored procedure.
Do you think you can ask your DBA (or whomever is responsible for the database) to create a stored procedure for you in which you'd pass the date parameters? That's basically your only way to create a parameterised query.

Multiple results into one SQL

I have a database and when I grab some results I want to work out how to return in a different format to what I am getting now for example
select
reports.host_ip, reportitems.mskb
from
reportitems
inner join
reports on reports.report_id = reportitems.report_id
where
reportitems.mskb IS NOT NULL
order by
reportitems.mskb;
Output:
10.63.128.115|2251481
10.63.128.89|2269637
10.63.128.100|2269637
10.63.128.16|2269637
10.63.128.115|2269637
10.63.128.115|2669970
10.63.128.89|2871997
10.63.128.100|2871997
10.63.128.16|2871997
10.63.128.115|2871997
10.63.128.194|3000483
10.63.128.198|3000483
10.63.128.89|3000483
I would like the output to be for example all of the ips in the left column to be grouped so for 2269637 it would look like
10.63.128.89
10.63.128.100 2269637
10.63.128.16
10.63.128.115
can I do that with the SQL statement or will i need to process that afterwards?
Thanks
Generally this would be left up to the UI displaying the data, but if that isn't an option, or it's just difficult to implement, you could use the GROUP_CONCAT() function and shove a carriage return in between values:
SELECT GROUP_CONCAT(CONCAT(reports.host_ip, '\n')) as IP_Addresses,
reportitems.mskb
FROM reportitems
INNER JOIN reports
ON reports.report_id = reportitems.report_id
WHERE reportitems.mskb IS NOT NULL
GROUP BY reportitems.mskb
ORDER BY reportitems.mskb

Long Text Field over 255 Characters gets truncated

Not sure why my field in my query is getting truncated upon the return of the result. The value is being stored in the field, but gets truncated by access to help with "performance". I have reviewed multiple forums and SO posts to no avail.
Problems listed at link do not apply, Aggregation, Uniqueness, Union, Format Property, Row Source
What is wrong with my query? Instructions field in the Customer table is the one that is getting truncated.
Here is the raw query generated by access:
SELECT Task.ID, Task.TaskID, Task.TaskName, Task.TypeID, TaskType.TaskTypeName, Task.CustomerID, Customer.CustomerName, Customer.OnHold, Customer.Blacklisted, Customer.CustomerEngagementRecieved, Customer.AutoEmail, Customer.SpecialInstructions, Customer.Instructions, Task.QuoteRequired, Task.PriorityID, Priority.Priority, Task.Min, Task.Max, Task.Projected, Task.DeadlineDate, Task.ResourceID, Resource.ResourceName, Resource.Email, Resource.Extension, Task.Description, Task.StatusID, Status.Status, Task.DeveloperLog, Task.TaskPOCID, POC.Phone, POC.Email, Task.OtherPOC, Task.OtherPOCPhone, Task.OtherPOCEmail, Task.FolderPath, Task.StopBilling, Task.Premium, Task.EntryDate, Task.CompleteDate, Task.AssignedBy, Task.SettingsID, Settings.AutoEmail
FROM TaskType
INNER JOIN (Status
INNER JOIN (Settings
INNER JOIN (Resource
INNER JOIN (Priority
INNER JOIN (Customer
INNER JOIN (Task
INNER JOIN POC ON Task.TaskPOCID = POC.POCID)
ON Customer.CustID = Task.CustomerID)
ON Priority.PriorityID = Task.PriorityID)
ON Resource.ResourceID = Task.ResourceID)
ON Settings.SettingsID = Task.SettingsID)
ON Status.StatusID = Task.StatusID)
ON TaskType.TTID = Task.TypeID;
`
Have a close read of this - http://allenbrowne.com/ser-63.html something in your set up will causing the truncation.
If it's when you cut and paste the query results that can also be mis-leading. When you say a Long Text are these linked tables?
I'd also rename your Min and Max fields as they are reserved words and may cause access to think you are aggregating your data.
So from the sounds of it, Access just sometimes will ALWAYS truncate the field no matter what the settings. There is a way to force access to show the entire field though, by using the DLOOKUP() function instead of using a Control Source.
Here is the Answer to my current Issue for reference,
=DLOOKUP("Instructions", "Customer", "CustID=" & [CustomerID])

SQL Server returns empty fields

I have a select script that runs 3 times a day in a 2005 SQL Server installation, a few times the return table has contained expected number of rows but without any values. The integer and date fields have zeros and the others are simply blank.
Since the problem occurs very rarely there is no way to supervise the database when the script runs and haven't been able to replicate the issue. My thought is that some other update in the database is causing the problem. Does anyone know about this problem?
Here is the script
SELECT DISTINCT
URL.Line,
RTrim(URL.DescriptionNote) AS [DescriptionNote],
SA1.Name AS [Name1],
SP1.Designation AS [Designation1],
SA2.Name [Name2],
SP2.Designation AS [Designation2],
RL.DistanceMeters,
dbo.RouteLinkTransportModeDesc(URL.TransportModeTypeNumber) AS TransportMode,
URL.THM,
URL.FirstWorkedDate,
URL.LastWorkedDate
FROM #RequiredRouteLink URL
INNER JOIN StopPoint AS SP1
ON SP1.JourneyPatternPointGid = URL.StartsAtPointGid
INNER JOIN StopArea AS SA1
ON SP1.IsPartOfStopAreaVersionId = SA1.VersionId
INNER JOIN StopPoint AS SP2
ON SP2.JourneyPatternPointGid = URL.EndsAtPointGid
INNER JOIN StopArea AS SA2
ON SP2.IsPartOfStopAreaVersionId = SA2.VersionId
LEFT JOIN verRouteLink AS RL
ON RL.StartsAtPointGid = URL.StartsAtPointGid
AND RL.EndsAtPointGid = URL.EndsAtPointGid
AND URL.TransportModeTypeNumber = RL.TransportModeTypeNumber
WHERE URL.StartsAtPointGid <> URL.EndsAtPointGid
AND (RL.EndsAtPointGid IS NULL OR RL.DistanceMeters = 0 OR RL.DistanceMeters IS NULL)
AND RL.[DeletedDateTime] IS NULL
ORDER BY FirstWorkedDate, THM, TransportMode, Line, DescriptionNote
I'm sure it is a data issue. The reason you are getting blank fields is most likely the LEFT JOIN.
You have 4 INNER JOINs, any one of which could cause you to have an empty result set. If one or more of those don't find a match, you have no results.
However, your LEFT JOIN means you will at least get the list of URL.Line, which is why you had the expected number of rows most likely.
Can you run a low level SQL trace - filtering it as much as possible to reduce the load of the trace [e.g., by application, etc.]