Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I've learned more from your forums in 3 days than 3 months at school and on the job combined..."

Geography

Where in the world do Tek-Tips members come from?

Table Update Based on Comparison ResultsHelpful Member!(2) 

CousinBrucie (Programmer)
25 Jun 12 21:37
Hello, I'm taking in the string comparisons I need, but now need to just update a column in another table based on this result. Here's a snippet of what I'm trying to accomplish with a CASE WHEN THEN END but it isn't working:

SELECT * FROM TestSubmission t
CASE WHEN t.Region <> @region THEN
OR t.SUB_TYPE <> @subType
OR t.VENDOR_ID <> @vendorId
OR t.DELIVERY_TYPE <> @deliveryType
OR t.DELIVERY_METHOD <> @deliveryMethod
(
UPDATE SubmissionManagement
SET FileLoadStatus = 4
)
ELSE
(
UPDATE SubmissionManagement
SET FileLoadStatus = 1
)
END

Is there any possibility of working with results from something like this?:
SELECT *
FROM TestSubmission t
WHERE t.Region <> @region OR
t.SUB_TYPE <> @subType OR
t.VENDOR_ID <> @vendorID OR
t.DELIVERY_TYPE <> @deliveryType OR
t.DELIVERY_TYPE <> @deliveryMethod

Grateful for any assistance.

Thank you,
Bruce
markros (Programmer)
25 Jun 12 22:43
May be:

CODE

update SubmissionManagement SET FileLoadStatus = case when exists (SELECT 1
FROM TestSubmission t
WHERE t.Region <> @region OR
t.SUB_TYPE <> @subType OR
t.VENDOR_ID <> @vendorID OR
t.DELIVERY_TYPE <> @deliveryType OR
t.DELIVERY_TYPE <> @deliveryMethod
) then 4 else 1 end

PluralSight Learning Library

Helpful Member!  Qik3Coder (Programmer)
26 Jun 12 10:47
CousinBrucie,
You don't have a solid join between TestSubmission and SubmissionManagement, so you're going to get some very loose joining, especially using a wide not equals block like that.
Note, this block should affect ALL rows, because there is no separate filtering criteria, which would need to be applied to the end.

markos,
I don't think that is going to do what you wanted.
You are doing something akin to "set all marbles blue where 1 marble is red".
I'm sure you meant something like:

CODE

update t2
SET FileLoadStatus = case when exists
(SELECT 1
FROM TestSubmission t
WHERE t.Region <> t2.region OR
t.SUB_TYPE <> t2.sub_Type OR
t.VENDOR_ID <> t2.vendor_ID OR
t.DELIVERY_TYPE <> t2.delivery_Type OR
t.DELIVERY_METHOD <> t2.deliveryMethod
) then 4 else 1 end
FROM SubmissionManagement t2
WHERE 1=1
--EXTRA FILTER CRITERIA HERE

Lodlaiden

You've got questions and source code. We want both!
There's a whole lot of Irish in that one.

markros (Programmer)
26 Jun 12 14:04
Yes, most likely this is what the OP wanted.

PluralSight Learning Library

CousinBrucie (Programmer)
26 Jun 12 14:26
Thank you both for your responses.

This stored procedure is going to take in the FileName from an SSIS package, with the prior steps being that both the Submission table will be truncated, so we'll always be sure that the detail records in the Submission table belong to the MAX record in the SubmissionManagement table. Let me try and implement the last code snippet and see what happens. I really appreciate this help.

CB
CousinBrucie (Programmer)
26 Jun 12 15:31
Hello, I've got this which is working without the WHERE clause (or when WHERE 1 = 1) and am trying to get this to work with the MAX(FileLoadDate) in the filter condition. I'm comparing to the variable values which is working fine, but when I try to run this with a WHERE clause with a nested SELECT here is the error:

An expression of non-boolean type specified in a context where a condition is expected, near 'END'.

UPDATE SubmissionManagement
SET FileLoadDate =
CASE WHEN exists
(
SELECT 1
FROM TestSubmission t
WHERE t.Region <> @region OR
t.SUB_TYPE <> @subType OR
t.VENDOR_ID <> @vendorID OR
t.DELIVERY_TYPE <> @deliveryType OR
t.DELIVERY_METHOD <> @deliveryMethod
)
THEN 4
ELSE 1
END
FROM SubmissionManagement
WHERE (SELECT MAX(FileLoadDate))

Thanks,
CB
Helpful Member!  markros (Programmer)
26 Jun 12 16:55
Your WHERE clause makes no sense. Do you only want to update one row with the Max date? If so, try:

CODE

;with cte as (select top (1) FileLoadDate, FileLoadStatus,
CASE WHEN exists
(
SELECT 1
FROM TestSubmission t
WHERE t.Region <> @region OR
t.SUB_TYPE <> @subType OR
t.VENDOR_ID <> @vendorID OR
t.DELIVERY_TYPE <> @deliveryType OR
t.DELIVERY_METHOD <> @deliveryMethod
)
THEN 4
ELSE 1
END as NewStatus
FROM SubmissionManagement
ORDER BY FileLoadDate DESC)

UPDATE cte SET FileLoadStatus = NewStatus

PluralSight Learning Library

CousinBrucie (Programmer)
26 Jun 12 22:30
Wow, thanks Mark. I see I have a lot to learn with this stuff, but what a great example I can learn from as I implement and test with this.
CB

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close