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
297 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
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 84

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

685 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