How to pull a ms access report data to a query? - sql-server

I am trying to pull data from a MS Access report (repInvoices and form frmInvoice) and keep it in MS Access query(qryQuote). I know how it can be done from form but not from a report. I did research in stack exchange and google but I didn't find anything that is helpful. Can any of you help me out? Following is the query that I had been trying to work on. I kept form on following example:
SELECT MAX([oi].[Quote])
FROM ORDER_Shipment AS os
INNER JOIN ORDER_Items AS oi
ON os.Id = oi.Id
AND oi.MaterialId = [Reports]![repInvoices]![MaterialId]
WHERE os.InvoiceNumber= CAST('& Me.InvoiceID' AS VARCHAR)
Two last lines in above query is the place where I need to get data from the report.Also, I know both of above are not correct right now.
[Reports]![repInvoices]![MaterialId] It gives me an error JOIN expression not supported.
Also, I have to use report InvoiceID here CAST('& Me.InvoiceID' AS VARCHAR)

Related

CROSS APPLY function in Presto/AWS

I'm trying to convert 4 date columns titled Created, Approved, Processed & Realized to a single column with all 4 dates AND a second column with the status of each of those dates.
The image at the end shows the data issue visually (apologies I'm still figuring out how to attach tables in textual form on stackoverflow)
To solve this, I successfully executed the CROSS APPLY
function in SQL server (see below), but I now need to do the same in AWS Simba
Athena or the Presto language. Can someone please guide me on what is the AWS/Presto equivalent of a CROSS APPLY function? Thank you in advance
SELECT
V.Date,
V.Status
From Table C
CROSS APPLY
(VALUES
(C.Created, 'Opened'),
(C.Approved, 'Approved'),
(C.Processed, 'Processed'),
(C.Realized, 'Realized')
) AS V([Date], Status)
I want to convert the following table:
You should be able to use UNNEST for this:
SELECT v.date, v.status
FROM m_table
CROSS JOIN UNNEST(ARRAY[
ROW(C.Created, 'Opened'),
ROW(C.Approved, 'Approved'),
ROW(C.Processed, 'Processed'),
ROW(C.Realized, 'Realized')
]) AS v(date, status);
This works in the latest Presto version, 337.
In Athena you probably still cannot UNNEST array or ROW
in ANSI SQL manner, so you may need some modifications.

SSRS Report Manager: Linked report behaving strangely but SQL queries work fine

I have created a report (call it Primary) and a drillthrough (call it Secondary) in Report Builder. Each of these has a SQL statement.
When executed in SQL Server Management Studio, SQL statements work as expected.
However, when the Primary.rdl and Secondary.rdl are uploaded to Report Manager (the web interface in Internet Explorer), they do not generate the correct data when run.
Because of this, I think the problem is not the SQL statements. I think it's something to do with the Report Manager.
Primary SQL statement:
This statement grabs a bunch of user data from multiple tables and checks if their passwords are acceptable. It populates a list of users whose passwords failed the check.
This is pseudocode so pardon inconsistencies in var names
with details as (
select u.userid
, u.password
, u.firstname
, u.lastname
, u.userdescription
, u.status
, u.lastlog
, dbo.IsPassswordAcceptable(u.userid, u.password) as passStatus
from masterListOfUsers as u
)
select d.*, p.datavalue
from details as d
left join passwordDetailList as p
on p.keyvalue = d.passStatus
and p.datatype = 'ERRORMESSAGE'
where d.passStatus <> 1
and d.passStatus <> -5
and d.status = (#USERSTATUS) -- only user ids in use
;
Secondary SQL statement:
This statement is a drillthrough. The person running the report can click on a userID in the above list. A drillthrough is performed where the contact information for that userID is populated.
This is pseudocode so pardon inconsistencies in var names
SELECT
m.userid
, c.address
, c.city
, c.state
, c.zip
, c.cphone
FROM userMasterList AS m
left join userDetailList AS d
ON d.userid = m.userid
left join anotherList as e on d.fullkey = e.fullkey
left join yetAnotherList AS c
WHERE m.userid = #USERID;
Expected result:
When the user runs the Primary, a list of users with bad passwords is populated. Each user's userID can be clicked on, which triggers the Secondary to populate the location/contact info associated with that userID.
Actual result:
On userID click, the Secondary fails to populate any location/contact info associated with the userID. This occurs only sometimes. Other times, it works fine.
I made a list of these "empty" userIDs and ran the Secondary's SQL statement in Management Studio, and it populates all the expected location/contact info.
Solutions I've tried:
I'm absolutely stumped. I've triple-checked the SQL statements and tested them in Management Studio. I've re-uploaded both .rdl files to Report Manager. I've reassigned the Secondary to the Primary via the "Create Linked Report" option in Report Manager AND ALSO in Report Builder's Action > Go To Report option.
What else can I do?
This is not really an answer as such, but a list of things I would work thru in the same situation.
Run SQL Profiler to trace your report session and make sure the query being executed is what you expect. Depending on how parameters are passed to the SQL statements, SSRS will not always do things quite the way you expected.
Check if you can repeat the issue by just running the drill thru report on it's own (not via the primary report)
Determine if the issue is consistent with specific userids? i.e. does user A always fail and User B always work? If the issue is consistent, the issue is most likely to be data related. Check for special characters in the fields that appear to be blank such as chr(13)/chr(10), they may just be forcing the 'real' content onto a new line inside the textbox.
Add some debug info to your report to help identify the issue such as:
a. Edit the dataset query to add some more info from dataset itself SELECT .... , c.addrees, len(c.address) as AddresLen from .... You can add this to a copy of your report
b. Add another textbox that does the same thing but directly in SSRS (e.g. expression would be something like =LEN(Fields!address.Value)). You then have two numbers to compare against what you can see. If the LEN textbox says 20 but the address field appears blank, then special characters could be the issue.
After hours of tinkering, the problem ended up being that the userID was being trimmed of all leading and trailing whitespace by some of the query tools but not by the SQL statements themselves. So when the final report is run in Report Manager, the data is queried with superfluous whitespace, resulting in no data being found.
This issue is resolved when the data points are trimmed.
The fixed Secondary SQL statement:
This is pseudocode so pardon inconsistencies in var names
SELECT
rtrim(m.userid) as userid
, rtrim(c.address) as address
, rtrim(c.city) as city
, rtrim(c.state) as state
, rtrim(c.zip) as zip
, rtrim(c.phone) as phone
FROM userMasterList AS m
left join userDetailList AS d
ON d.userid = m.userid
left join anotherList as e on d.fullkey = e.fullkey
left join yetAnotherList AS c
WHERE ltrim(rtrim(m.userid)) = ltrim(rtrim(#USERID));

SSRS 2008 subscriptions have incorrect data

I have written, and rewritten, a hospital census query for which I would like to create a daily Windows File Share subscription. The query seems fine; it runs properly in SSMS, BIDS, and on the SSRS Report Server itself. However, when I create any kind of subscription, sometimes the report will have old and incorrect data on it. There is no real pattern to when the report generates incorrectly, as far as I can tell, but the data that gets sent incorrectly is consistently the same, and is from back in January. I had initially written the query on a test server with the same data, just a little bit older, and had changed the dates that it would run for. I also had to re-deploy the report a few times after making changes. I had initially deployed it from the test system directly to the live server, so I tried completely redoing it in the live system, in addition to making some changes improve performance, but I am still getting the same incorrect data.
Any ideas would be greatly appreciated at this point, because I am at a loss and I am really just getting started with SQL.
Thank you for any assistance.
Edits: adding the queries that are returning the inconsistent data; removing my comments that have poorly-formatted code.
Also, to be a bit more specific about the issue: The subscription sometimes sends the census data for the day before as expected, and sometimes it sends the census data from January 15, and this only happens in the subscription.
Query 1:
SELECT
V.Name AS PatientName
,V.AccountNumber AS AccountNumber
,V.FinancialClassName AS FinancialClass
,V.ServiceDateTime
,P.EmergencyID AS Provider
,X.ErDateTime AS Discharge
,X.ErDispositionID AS Disposition
FROM dbo.AdmVisits AS V
INNER JOIN dbo.AdmProviders AS P
ON P.VisitID=V.VisitID
INNER JOIN dbo.AdmDischarge AS X
ON V.VisitID=X.VisitID
WHERE CAST(V.ServiceDateTime AS DATE)=CAST(DATEADD(day,-1,GETDATE()) AS DATE)
OR CAST(X.ErDateTime AS DATE)=CAST(DATEADD(day,-1,GETDATE()) AS DATE)
ORDER BY V.Name
Query 2:
SELECT
V.Name
,V.AccountNumber
,V.FinancialClassName
,V.InpatientServiceID
,V.RoomID
,D.AdmitDateTime
,P.AdmitID
,P.AttendID
,X.DischargeDateTime
,X.DispositionName
FROM dbo.AdmVisits AS V
INNER JOIN dbo.AdmittingData AS D
ON V.VisitID=D.VisitID
INNER JOIN dbo.AdmProviders AS P
ON P.VisitID=D.VisitID
LEFT OUTER JOIN dbo.AdmDischarge AS X
ON V.VisitID=X.VisitID
WHERE V.InpatientOrOutpatient='I'
AND NOT V.InpatientServiceID='INP LTC'
AND (CAST(D.AdmitDateTime AS DATE)<CAST(GETDATE()AS DATE))
AND (V.Status='ADM IN' OR CAST(X.DischargeDateTime AS DATE)=CAST(DATEADD(DAY,-1,GETDATE()) AS DATE))
ORDER BY V.Name
Query 3:
SELECT
V.Name
,V.AccountNumber
,V.FinancialClassName
,V.InpatientServiceID
,V.RoomID
,D.AdmitDateTime
,P.AdmitID
,P.AttendID
,X.DischargeDateTime
,X.DispositionName
,CASE
WHEN V.LoaStatus IN('F','L') THEN CAST(V.LoaEffectiveDateTime AS varchar)
Else 'No'
END AS 'LeaveOfAbsence'
FROM dbo.AdmVisits AS V
INNER JOIN dbo.AdmittingData AS D
ON V.VisitID=D.VisitID
INNER JOIN dbo.AdmProviders AS P
ON P.VisitID=D.VisitID
LEFT OUTER JOIN dbo.AdmDischarge AS X
ON V.VisitID=X.VisitID
WHERE V.InpatientServiceID='INP LTC'
AND (CAST(D.AdmitDateTime AS DATE)<CAST(GETDATE()AS DATE))
AND (V.Status='ADM IN' OR CAST(X.DischargeDateTime AS DATE)=CAST(DATEADD(DAY,-1,GETDATE()) AS DATE))
ORDER BY V.Name
So it turns out that the issue was the data source's connection string to the server. I was using 'localhost' instead of the server's actual name, and since the test and live servers are basically clones of one another, I think it wasn't consistently connecting to the right server. Not sure how exactly that works, but the report is working now.

ADODB.Recordset returns with incorrect data?

I have an ADO.Recordset object that is able to successfully connect to the database and pull data. However, the data that is in the database vs what is being retrieved by the object is incorrect.
For example, if I take the exact query that is in the command parameter in the Recordset and execute it in Sql Server Management Studio, the RANK in SSMS is 170 but the data returned in code from the object says it is 140.
Has anyone run across a situation like this before? I can't figure out why this is happening.
This involves a search box where the user is trying to search for a product and the script is trying to find the product that the user most likely wants to find.
SELECT tbl.RANK as rk, a.*, CA.* FROM FREETEXTTABLE
([ab_fts],([cpn],[cd],[mas],[ac],[ttr],[nemo],[man]),'tester',1000) tbl
INNER JOIN [qws] a on tbl.[key] = a.wid
INNER JOIN catad CA ON (CA.cgid = a.cgid)
ORDER BY rk DESC

Is it possible to connect an Access form to a SQL Server view

I'm migrating an Access DB to SQL Server and everything is slowly coming along but Im not sure how to connect the Access forms to the SQL Server views.
So far I have all the tables linked to SQL Server and Im working on migrating the Access queries into views, but Ive got this error, A2SS0069: External variable cannot be converted
which references a form in my Access file:
SELECT TOP 9223372036854775807 WITH TIES
[AcuteHospitals].[NHSN_ID],
[AcuteHospitals].[HospitalName],
[Location_LOV].[Description] AS Location,
Sum([RateTable_CLABData].[clabcount]) AS [Number of CLABSI],
Sum([RateTable_CLABData].[numcldays]) AS [Central Line Days],
[RateTable_CLABData].[CLAB_Mean] AS [National Average]
FROM
(([AcuteHospitals]
LEFT JOIN [RateTable_CLABData]
ON [AcuteHospitals].[NHSN_ID] = [RateTable_CLABData].[orgID])
LEFT JOIN [Location_LOV]
ON [RateTable_CLABData].[loccdc] = [Location_LOV].[CDCLoc])
LEFT JOIN [SummaryYQ_LOV]
ON [RateTable_CLABData].[summaryYQ] = [SummaryYQ_LOV].[StartDate]
WHERE ((([SummaryYQ_LOV].[SummaryYQ]) = forms!YQ_Location.text5 ))
GROUP BY
[AcuteHospitals].[NHSN_ID],
[AcuteHospitals].[HospitalName],
[Location_LOV].[Description],
[RateTable_CLABData].[CLAB_Mean],
[RateTable_CLABData].[loccdc]
HAVING ((([RateTable_CLABData].[loccdc]) NOT LIKE '%ped%'))
ORDER BY [AcuteHospitals].[HospitalName], [RateTable_CLABData].[loccdc]
Its the line WHERE ((([SummaryYQ_LOV].[SummaryYQ]) = forms!YQ_Location.text5 ))
So I need to know if it's possible and how to get this new view to connect with the Access form.
The problem is here
WHERE ((([SummaryYQ_LOV].[SummaryYQ]) = forms!YQ_Location.text5 ))
You cannot convert such Access query into a SQL View, but you can use Stored Procedure instead and pass value from the field forms!YQ_Location.text5 as parameter.
Also, you don't need this TOP 9223372036854775807 WITH TIES it is redundant.
You can't reference the Access form in a SQL View directly. You will need to rethink the logic in this. You could either create a number of Views with the appropriate values hard-coded (inadvisable) or convert the View to a Stored Procedure and pass the value in as a parameter.
For example (assuming the parameter is a string):
create proc s_MyStoredProc
#Location varchar(50)
AS
BEGIN
SELECT
[AcuteHospitals].[NHSN_ID],
[AcuteHospitals].[HospitalName],
[Location_LOV].[Description] AS Location,
Sum([RateTable_CLABData].[clabcount]) AS [Number of CLABSI],
Sum([RateTable_CLABData].[numcldays]) AS [Central Line Days],
[RateTable_CLABData].[CLAB_Mean] AS [National Average]
FROM
(([AcuteHospitals]
LEFT JOIN [RateTable_CLABData]
ON [AcuteHospitals].[NHSN_ID] = [RateTable_CLABData].[orgID])
LEFT JOIN [Location_LOV]
ON [RateTable_CLABData].[loccdc] = [Location_LOV].[CDCLoc])
LEFT JOIN [SummaryYQ_LOV]
ON [RateTable_CLABData].[summaryYQ] = [SummaryYQ_LOV].[StartDate]
WHERE ((([SummaryYQ_LOV].[SummaryYQ]) = #Location ))
GROUP BY
[AcuteHospitals].[NHSN_ID],
[AcuteHospitals].[HospitalName],
[Location_LOV].[Description],
[RateTable_CLABData].[CLAB_Mean],
[RateTable_CLABData].[loccdc]
HAVING ((([RateTable_CLABData].[loccdc]) NOT LIKE '%ped%'))
ORDER BY [AcuteHospitals].[HospitalName], [RateTable_CLABData].[loccdc]
END
Just create the SQL server view, and then from the Access font end link to that view. It is easy, not much work.
As for any parameters? Just remove them from the query and views. You then just open up the report using a where clause from the Access client.
In fact using a Access form or report that is bound to a linked table (or in this case view) is easy, and Access will ONLY pull down the reocrds you specifiy in the "where" clause of the open form or open report command.
SQL Server has an excellent (and free) MS-Access to MS-SQL migration tool. It does a very good job of converting MS-Access queries. I haven't tried converting queries with form parameters, but it is certainly worth a look and you may learn some things as well, especially if you plan to convert other queries. http://www.microsoft.com/sqlserver/en/us/product-info/migration-tool.aspx#oracle.

Resources