Solved

SQL 2000 DTSLookup Using Functions or Stored Procedure

Posted on 2006-06-22
9
282 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
ID: 16962006
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
ID: 16965127
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
ID: 16966877
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 30

Expert Comment

by:nmcdermaid
ID: 16966987
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
 

Author Comment

by:robert_f
ID: 16967137
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
ID: 16967206
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
ID: 16967257
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
ID: 16967986
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
ID: 16968783
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

733 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