Solved

SQL 2000 DTSLookup Using Functions or Stored Procedure

Posted on 2006-06-22
9
278 Views
Last Modified: 2012-05-05
Hi There

I've got a DTS package which runs in a file (tab delim) and needs to perform three checks on the incoming data before writing the record to a table. What I was hoping to do was to use a DTSLookup query to run either a user defined function or stored proc which would go away and check a)Does the client exist b) has "check 1" passed c)has "check 2" passed.

I've written the function (also tried it with SP!!!) to return an integer value indicating what the outcome of the validation was so that I can then either write the record or write an error (I've tested these through query analyser and they both work fine). An example is below. However whenever I try to run the SP or Function within the DTSLookup Query Designer I just get an error saying Syntax Error or Access Denied.

The question is then ..... Is it possible to do this through the DTSLookup Query .... if so how?? If not can anyone sugeest an alterative ??

Cheers

Rob

Active X Code
----------------

Function Main()

      SELECT CASE DTSLookups("Check_Valid_Client").Execute(DTSSource("AgreementID"))

            CASE 1
                  Fred = DTSLookups("Insert_Error").Execute("1","Client Unknown:" & DTSSource("AgreementID"))            
            CASE 2
                  Fred = DTSLookups("Insert_Error").Execute("1","Check 1 Failed:" & DTSSource("AgreementID"))            
            CASE 3
                  Fred = DTSLookups("Insert_Error").Execute("1","Check 2 Failed:" & DTSSource("AgreementID"))            
            CASE ELSE
                  DTSDestination("TotalAmount") = DTSSource("Amount")
                  DTSDestination("NB_AgreementID") = DTSSource("AgreementID")
                  DTSDestination("NB_StatementDate") = DTSSource("StatementDate")
                  DTSDestination("NB_StatementID") = DTSSource("StatementID")

      END SELECT

      Main = DTSTransformStat_OK
End Function

DTSLookup Query
---------------------
SELECT     [dbo].FCN_DTS_LKUP_VALID_NB_CLIENT(?)
0
Comment
Question by:robert_f
  • 5
  • 4
9 Comments
 

Author Comment

by:robert_f
Comment Utility
Would you believe it !!! Having spend the better part of a day trying to figure this out within 20 mins of posting this I have a flash of inspiration !!

Basically I ran the function via a stored proc which then returned a value back through to the DTSLookup OK.

Cheers

Rob
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
Its way more efficient to

-import the data into a staging table
-run a stored proc on the staging table to identify this stuff internally.

The DTSLookup is extremely inefficient.


But I guess if it aint broke don't fix it!
0
 

Author Comment

by:robert_f
Comment Utility
nmcdermaid

Thanks for the response. By this I assume you mean just pull all the records into a table and then iterate through that table with a stored proc (using cursors?) and do the work there instead? I think I'll give it a go later today. I'm always open to doing things a better way where possible and as I've only just started putting the system together its no great shakes to dump this bit and start again.

Cheers

Rob
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
Actually you should be able to do a direct join to the table which is way faster than using a cursor.

Assuming that AgreementID contains an ID that you are validating, you can import all he data in a staging table then this will show you which ones are invalid (do not exist in another table - I assume thats what you're doing)

SELECT DISTINCT S.AgreementID
FROM
StagingTable S
LEFT OUTER JOIN
AgreementTable A
ON S.AgreementID = A.AgreementID
WHERE A.AgreementID IS NULL


It is a simple matter to convert this to an Update or delete statement to mark or remove invalid entries.




0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:robert_f
Comment Utility
Actually not quite as simple as that .... there are two bit fields in the AgreementTable which indicate if the case has passed two different checks. If they're both true (and if the actual agreement ID also exists) then the case is good to go , otherwise I need to write a record to an error table indicating which of the checks have failed. Having said that this would work, I would have thought, just by "expanding" the where clause to include the bit fields and splitting it out into three different queries

e.g.
Query 1 -- Gets the "good" records
Query 2 -- Gets the ones that have failed Check 1 and writes to error table
Query 3 -- Gets the ones that have failed Check 2 and writes to error table

Any improvements on that would be gratefully receieved :-)

Cheers

Rob
0
 
LVL 30

Expert Comment

by:nmcdermaid
Comment Utility
This should give you that report.

If its suitable I can show you how to turn it into an UPDATE statement.


SELECT DISTINCT
S.AgreementID,
CASE
WHEN Bit1 = 1 AND Bit2 = 1 THEN 'Pass'
WHEN Bit1 = 0 AND Bit2 = 1 THEN 'Fail 1'
WHEN Bit1 = 1 AND Bit2 = 0 THEN 'Fail 2'
ELSE 'Agreement does not exist' END As Status

FROM
StagingTable S
LEFT OUTER JOIN
AgreementTable A
ON S.AgreementID = A.AgreementID
0
 

Author Comment

by:robert_f
Comment Utility
That certainly looks a lot more concise than mine ! Just ran the data in and tried this query against it ... works a treat !!! If you could give me an idea of how the update would work from this it would be helpful.

Cheers

Rob
0
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 500 total points
Comment Utility
The update would be something like this:


UPDATE StagingTable
SET Status = CASE
    WHEN Bit1 = 1 AND Bit2 = 1 THEN 'Pass'
    WHEN Bit1 = 0 AND Bit2 = 1 THEN 'Fail 1'
    WHEN Bit1 = 1 AND Bit2 = 0 THEN 'Fail 2'
    ELSE 'Agreement does not exist' END
FROM
StagingTable S
LEFT OUTER JOIN
AgreementTable A
ON S.AgreementID = A.AgreementID


This may not be the exact syntax, I'm often caught out, but try it anyway.






Alternatively you can run just this:


UPDATE StagingTable
SET Status = CASE
    WHEN A.Bit1 = 1 AND A.Bit2 = 1 THEN 'Pass'
    WHEN A.Bit1 = 0 AND A.Bit2 = 1 THEN 'Fail 1'
    WHEN A.Bit1 = 1 AND A.Bit2 = 0 THEN 'Fail 2' END
FROM
AgreementTable A
WHERE StagingTable.AgreementID = A.AgreementID

Then your status field will be NULL if the agreement doesn't exist. (as it never gets updated by anything)

Alternatively the field could be 'Pass','Fail1' or 'Fail2'


This way is just a little quicker than the first. Not by much though... it only matters if you have a zillion records.
0
 

Author Comment

by:robert_f
Comment Utility
Fantastic .. with a bit of jiggery pokery it worked like a charm ... thanks for all your help. It might not have been broke but it was still worth fixing !! :-)

Thanks again

Rob
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now