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

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.

Related

ORA-01841 happens on one environment but not all

I have the following SQL-code in my (SAP IdM) Application:
Select mcmskeyvalue as MKV,v1.searchvalue as STARTDATE, v2.avalue as Running_Changes_flag
from idmv_entry_simple
inner join idmv_value_basic_active v1 on mskey = mcmskey and attrname = 'Start_of_company_change'
and mcentrytype = 'MX_PERSON' and to_date(v1.searchvalue,'YYYY-MM-DD')<= sysdate+3
left join idmv_value_basic v2 on v2.mskey = mcmskey and v2.attrname = 'Running_Changes_flag'
where mcmskey not in (Select mskey from idmv_value_basic_active where attrname = 'Company_change_running_flag')
I already found the solution for the ORA-01841 problem, as it could either be a solution similar to MSSQLs try_to_date as mentioned here: How to handle to_date exceptions in a SELECT statment to ignore those rows?
or a solution where I change the code to something like this, to work soly on strings:
Select mcmskeyvalue as MKV,v1.searchvalue as STARTDATE, v2.avalue as Running_Changes_flag
from idmv_entry_simple
inner join idmv_value_basic_active v1 on mskey = mcmskey and attrname = 'Start_of_company_change'
and mcentrytype = 'MX_PERSON' and v1.searchvalue<= to_char(sysdate+3,'YYYY-MM-DD')
left join idmv_value_basic v2 on v2.mskey = mcmskey and v2.attrname = 'Running_Changes_flag'
where mcmskey not in (Select mskey from idmv_value_basic_active where attrname = 'Company_change_running_flag')
So for the actually problem I have a solution.
But now I came into discussion with my customers and teammates why the error happens at all.
Basically for all entries of idmv_value_basic_activ that comply to the requirement of "attrname = 'Start_of_company_change'" we can be sure that those are dates. In addition, if we execute the query to check all values that would be delivered, all are in a valid format.
I learned in university that the DB-Engine could decide in which order it will run individual segments of a query. So for me the most logical explanation would be that, on the development environment (where we face the problem), the section " to_date(v1.searchvalue,'YYYY-MM-DD')<= sysdate+3” is executed before the section “attrname = 'Start_of_company_change'”
Whereas on the productive environment, where everything works like a charm, the segments are executed in the order that is descripted by the SQL Statement.
Now my Question is:
First: do I remember that right, since the teacher said that only once and at that time I could not really make sense out of it
And Second: Is this assumption of mine correct or is there another reason for the problem?
Borderinformation:
The Tool uses a kind of shifted data structure which is why there can be quite a few different types in the actual “Searchvalue” column of the idmv_value_basic_activ view. The datatype on the database layer is always a varchar one.
"the DB-Engine could decide in which order it will run individual segments of a query"
This is correct. A SQL query is just a description of the data you want and where it's stored. Oracle will calculate an execution plan to retrieve that data as best it can. That plan will vary based on any number of factors, like the number of actual rows in the table and the presence of indexes, so it will vary from environment to environment.
So it sounds like you have an invalid date somewhere in your table, so to_date raises an exception. You can use validate_conversion to find it.

Add Math function to SQL Query

New to SQL and I am trying to run a query that pulls all our item codes, lot number, and qty on hand.
Each lot number has multiple entries due to adjustments. I need a way of running my query and having it add or subtract to get the actual qty on hand for each lot and only show me lots that are in the negatives. I have tried playing with SSRS but I cant get it right. I'm using SQL 2008R2.
SELECT
IMLAYER.ITEM_CODE
,IMMSTR.ITEM_DESC
,IMLAYER.LOT_NO
,IMLAYER.QTY_ON_HAND
FROM
IMLAYER
INNER JOIN
IMMSTR
ON
IMLAYER.ITEM_CODE = IMMSTR.ITEM_CODE
WHERE
(IMLAYER.QTY_ON_HAND < 0);
I believe I understand the requirements correctly, but if not please comment and I can update the query:
SELECT
M.ITEM_CODE
,M.ITEM_DESC
,L.LOT_NO
,'SUM_OF_QTY_ON_HAND' = SUM(L.QTY_ON_HAND)
FROM
IMLAYER L
INNER JOIN
IMMSTR M
ON L.ITEM_CODE = M.ITEM_CODE
GROUP BY
M.ITEM_CODE
,M.ITEM_DESC
,L.LOT_NO
HAVING
SUM(L.QTY_ON_HAND) < 0
HAVING is the trick you are looking for to be able to use an aggregate function for filtering.

Query with multiple Left Joins to ODBC tables fails

I posted this originally without all the ODBC information. After further investigation the problem appears to the ODBC related. Given the change, I wanted to report and see if an ODBC guru might be "listening".
I have the following queries in Access...
Query #1
SELECT aa.DocNum, b.QualityClass
FROM dbo_TransferHistory AS aa LEFT JOIN PCQualityClass AS bb ON aa.DocNum = bb.DocumentNum
WHERE (((aa.DocNum)=[Enter Doc Num]));
Which I want to expand to...
Query #2
SELECT aa.DocNum, bb.QualityClass, cc.BldgCond
FROM (dbo_TransferHistory AS aa LEFT JOIN PCQualityClass AS bb ON aa.DocNum = bb.DocumentNum)
LEFT JOIN PCBldgCond AS cc ON aa.DocNum = cc.DocumentNum
WHERE (((aa.DocNum)=[Enter Doc Num]));
dbo_TransferHistory is an table I access through an ODBC connector.
PCQualityClass and PCBldgCond are two queries that are based off another ODBC table. The SQL for those queries is ...
Query for PCQualityClass
SELECT aa.DocumentNum, aa.Value AS QualityClass, aa.ResourceID, aa.BldgSeqNum
FROM dbo_PCBldgDetail AS aa
WHERE (((aa.DocumentNum)=[Enter Doc Num]) AND ((aa.ResourceID)="QualityClass") AND ((aa.BldgSeqNum)=[Enter Bldg Num]));
Query for PCBldgCond
SELECT aa.DocumentNum, aa.Value AS BldgCond, aa.BldgSeqNum, aa.ResourceID
FROM dbo_PCBldgDetail
WHERE (((aa.DocumentNum)=[Enter Doc Num]) AND ((aa.BldgSeqNum)=[Enter BldgNum]) AND ((aa.ResourceID)="Condition"));
DocNum and DocumentNum are the same type ("Short Text" and a length of 12) and while I would like to make the names the same, I cannot.
When the query is run, an Inputbox pops up and [Enter Doc Num] is replaced with the Document Number I want the data for.
The queries were created in Access using the Create Query tool. Access is willing to create the query and the SQL looks good.
The problem is that while Query #1 will work, Query #2 causes the error "Invalid Operation". As a note, if I take the first join out of query #2 and run the remainder as a single join, the query works.
Changing to inner joins will allow Query #2 to function but will cause issues as there are times that one or both of the two sub queries do not have data but I still need the data from the primary table.
Changing from ODBC tables to local tables solves the problem but is not a viable solution as the ODBC tables cannot be local.
Does anyone have any insight into the inter-workings of Access and ODBC? I have a hard time believing that I am the first person who has tried to build an Access/Odbc query with more than one join. Yet I have not been able to Google a solution to this problem.

Allow mulitple values not working in SSRS

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.

Access query returns empty fields depending on how table is linked

I've got an Access MDB I use for reporting that has linked table views from SQL Server 2005. I built a query that retrieves information off of a PO table and categorizes the line item depending on information from another table. I'm relatively certain the query was fine until approximately a month ago when we shifted from compatibility mode 80 to 90 on the Server as required by our primary application (which creates the data). I can't say this with 100% certainty, but that is the only major change made in the past 90 days. We noticed that suddenly data was not showing up in the query making the reports look odd.
This is a copy of the failing query:
SELECT dbo_porel.jobnum, dbo_joboper.opcode, dbo_porel.jobseqtype,
dbo_opmaster.shortchar01,
dbo_porel.ponum, dbo_porel.poline, dbo_podetail.unitcost
FROM ((dbo_porel
LEFT JOIN dbo_joboper ON (dbo_porel.assemblyseq = dbo_joboper.assemblyseq)
AND (dbo_porel.jobseq = dbo_joboper.oprseq)
AND (dbo_porel.jobnum = dbo_joboper.jobnum))
LEFT JOIN dbo_opmaster ON dbo_joboper.opcode = dbo_opmaster.opcode)
LEFT JOIN dbo_podetail ON (dbo_porel.poline = dbo_podetail.poline)
AND (dbo_porel.ponum = dbo_podetail.ponum)
WHERE (dbo_porel.jobnum="367000003")
It returns the following:
jobnum opcode jobseqtype shortchar01 ponum poline unitcost
367000003 S 6624 2 15
The query normally should have displayed a value for opcode and shortchar01. If I remove the linked table dbo_podetail, it properly displays data for these fields (although I obviously don't have unitcost anymore). At first I thought it might be a data issue, but I found if I nested the query and then linked the table, it worked fine.
For example the following code works perfectly:
SELECT qryTest.*, dbo_podetail.unitcost
FROM (
SELECT dbo_porel.jobnum, dbo_joboper.opcode, dbo_porel.jobseqtype,
dbo_opmaster.shortchar01, dbo_porel.ponum, dbo_porel.poline
FROM (dbo_porel
LEFT JOIN dbo_joboper ON (dbo_porel.jobnum=dbo_joboper.jobnum)
AND (dbo_porel.jobseq=dbo_joboper.oprseq)
AND (dbo_porel.assemblyseq=dbo_joboper.assemblyseq))
LEFT JOIN dbo_opmaster ON dbo_joboper.opcode=dbo_opmaster.opcode
WHERE (dbo_porel.jobnum="367000003")
) As qryTest
LEFT JOIN dbo_podetail ON (qryTest.poline = dbo_podetail.poline)
AND (qryTest.ponum = dbo_podetail.ponum)
I'm at a loss for why it works in the latter case and not in the first case. Worse yet, it seems to work intermittently for some records and not for others (it's consistent about the ones it does and does not work for).
Do any of you experts have any ideas?
You definitely need to use subqueries for multiple left/right joins in Access.
I think it's a limitation of the Jet optimizer that gets confused if you're just chaining left/right joins.
You can see that this is a recurrent problem that surfaces often.
I'm always confused by Access' use of brackets in joins. Try stripping out the extra brackets.
FROM
dbo_porel
LEFT JOIN
dbo_joboper ON (dbo_porel.assemblyseq = dbo_joboper.assemblyseq)
AND (dbo_porel.jobseq = dbo_joboper.oprseq)
AND (dbo_porel.jobnum = dbo_joboper.jobnum)
LEFT JOIN
dbo_opmaster ON (dbo_joboper.opcode = dbo_opmaster.opcode)
LEFT JOIN
dbo_podetail ON (dbo_porel.poline = dbo_podetail.poline)
AND (dbo_porel.ponum = dbo_podetail.ponum)
OK the above doesn't work - Sorry I give up