Advertisement

10.06.2008 at 10:18AM PDT, ID: 23791147
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Using IF statement in a stored procedure - Or CASE whichever would work best

Asked by GLIanimal in MS SQL Server, PL / SQL

Tags: ,

Ok. I am creating a stored procedure to update a few tables. See code. What i have is marked off in the code, I need to be able to find if my contact and customer are indeed the same, then I need to run the one update query, whereas if my contact and customer do not equal then I have to run the other. I am currently getting an invalid syntax message due to referencing the wi table in my if statement. Any help to solve this issue would be appreciated.
Side note, using SQL 2000.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
CREATE PROCEDURE dbo.UpdateuserID 
 
AS
 
-- Need to first update contact to inactivate MAX id value
 
UPDATE t1
	SET
		  t1.status = 'i'
		, t1.modified_on = GETDATE()
FROM CONTACT t1
INNER JOIN
(
	SELECT
		  name
		, MIN(id) AS id
	FROM CONTACT
	WHERE status = 'a'
	GROUP BY name
	HAVING COUNT(*) > 1
) t2
	ON t1.name = t2.name
	AND t1.id <> t2.id
WHERE t1.status = 'a'
/*
IF ((SELECT wi2.customer_id 
	FROM workitem wi2
	WHERE wi2.number=wi.number)
	=
	(SELECT wi3.contact_id
	FROM workitem wi3
	WHERE wi3.number=wi.number))
BEGIN
*/
UPDATE wi
SET 
wi.contact_id = c2.id,
wi.customer_id=c2.id
From workitem wi
inner join
(
	Select id, name
	from contact
	where status = 'i'
	group by name, id
)c
on c.id = wi.contact_id
Inner join
(
	select name, id
	from contact 
	where status = 'a'
	group by name, id
)c2
on c.name = c2.name	
where c.id<>c2.id
/*
END
ELSE
BEGIN*/
--Then update the workitem tables with the active id value
UPDATE wi
 
SET 
wi.contact_id = c2.id
From workitem wi
inner join
(
	Select id, name
	from contact
	where status = 'i'
	group by name, id
)c
on c.id = wi.contact_id
Inner join
(
	select name, id
	from contact 
	where status = 'a'
	group by name, id
)c2
on c.name = c2.name	
where c.id<>c2.id
--END
 
-- Then delete the inactive id value from the contact table 
delete 
from contact
where status = 'i'
GO
[+][-]10.06.2008 at 10:35AM PDT, ID: 22652391

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 10:38AM PDT, ID: 22652423

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 10:58AM PDT, ID: 22652619

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 11:06AM PDT, ID: 22652694

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 11:12AM PDT, ID: 22652752

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 12:44PM PDT, ID: 22653672

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2008 at 01:15PM PDT, ID: 22654012

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, PL / SQL
Tags: Microsoft, SQL 2000
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628