Solved

How to join an additional table to an existing stored procedure using SQL Server 2008 for an Access 2003 app?

Posted on 2014-12-31
3
301 Views
Last Modified: 2014-12-31
I am developing an Access 2003 application using Access 2003 as the front end and SQL Server 2008 as the back end database.

I have a stored procedure as follows:

If  Exists(SELECT * FROM dbo.SYSOBJECTS WHERE NAME = 'tblFlINTEXCL' AND TYPE = 'U')
DELETE FROM dbo.tblFlINTEXCL
INSERT INTO dbo.tblFlINTEXCL([Account Number], RedFlag, [Property Type], TrillFixField,EmployeeID,SwingInd,MasterCardNumber, ActiveRecord)
SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number],
C.RedFlag, UPPER(P.PropertyType) As [Property Type], C.TrillFixField,C.EmployeeID,C.SwingInd,C.MasterCardNumber, 1
FROM dbo.tblStatesAll  As S INNER JOIN (dbo.tblCustomersNew As C INNER JOIN dbo.tblProductsNew As P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber) ON S.StateFS = C.ResStateCode
WHERE (S.FallCycle= 1) AND
               (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
               (C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31')))
ORDER BY C.ResStateCode, [Account Number],P.PropertyType

How would you join table "tblIntlAccountFallExcl" to this stored procedure to exclude its "Account" values?
This table only contains the field "Account".

I have used the following SQL Statement separately but I want to incorporate it into my existing stored procedure:

SELECT * FROM dbo.tblFlINTEXCL AS FlINT WHERE NOT EXISTS
(SELECT NULL FROM dbo.tblIntlAccountFallExcl as EXCL
where FlINT.[Account Number] = EXCL.Account AND ActiveRecord = '1');
0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40525543
It sounds like you

1 put your first bit of code up to the end of the second-to-last line,

2. Add an AND

3. Put your second code from NOT EXISTS up to, but not including, the semicolon, but change
FINT.[Account Number]
To
Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber

4. Continue with your first bit of code.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40525553
So you don't want to insert records into tblFlINTEXCL if the C.CustomerNumber exists in tblIntlAccountFallExcl.Account?

If so, you might use a LEFT JOIN and exclude all records that return a non-null value in the [Account] field. This would be the select, I believe:

SELECT Right(C.OfficeNumber,3) + ' ' + C.CustomerNumber AS [Account Number],C.RedFlag, UPPER(P.PropertyType) As [Property Type], C.TrillFixField,C.EmployeeID,C.SwingInd,C.MasterCardNumber, 1
FROM dbo.tblStatesAll  As S INNER JOIN
(dbo.tblCustomersNew As C INNER JOIN
dbo.tblProductsNew As P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber) ON S.StateFS = C.ResStateCode
LEFT OUTER JOIN tblFLINTEXCL FL ON FL.[Account Number]=C.CustomerNumber
WHERE (S.FallCycle= 1) AND
               (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0)
                     AND
               (C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31')))
                    AND FL.Account IS NULL

Essentially, if the value in C.CustomerNumber matches a value foundd in tblFLINTEXCL.Account, you ignore that line.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 40525570
Well,
(and I am sure someone else will chime in)
You have your FROM clause
FROM dbo.tblStatesAll  As S INNER JOIN (dbo.tblCustomersNew As C INNER JOIN dbo.tblProductsNew As P ON C.CustomerNumber = P.CustomerNumber AND C.OfficeNumber = P.OfficeNumber) ON S.StateFS = C.ResStateCode

So you are going to need to alter that to get dbo.tblIntlAccountFallExcl in there with the appropriate relationship
I can't really see how that's going to happen, intuitively -- but then it's not my data.  You many look at it and see exactly how.

You have your WHERE clause
WHERE (S.FallCycle= 1) AND
                (P.PropertyType='FIXED INCOME' AND C.DateLost <= CAST((@RptYear - S.IRAFS) AS VARCHAR) + '-06-30' AND LEN(P.IRACode) > 0 AND (P.Quantity > 0 OR P.CashBalance > 0) AND
                (C.DateOfBirth <= CAST(((@RptYear - S.IRAFS)-71) AS VARCHAR) + '-12-31')))


You now want to flange in an additional condition.  Above you flanged in dbo.tblIntlAccountFallExcl.
Now you are looking to exclude records where FlINT.[Account Number]  = dbo.tblIntlAccountFallExcl.Account and  ActiveRecord = '1', right?

S0 probably appending this to your WHERE statement
AND (FlINT.[Account Number]  <> dbo.tblIntlAccountFallExcl.Account and  ActiveRecord <> '1')
Might be the right logic.
You got a WHERE NOT EXISTS and a SELECT NULL.  Those make for a bit of mind-bending logic.

But that's the idea at any rate.
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question