Solved

SQL 2000 DTSLookup Using Functions or Stored Procedure

Posted on 2006-06-22
9
281 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

856 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