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
298 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

734 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