?
Solved

Help with a case statement in a SQL Stored Procedure

Posted on 2016-10-20
8
Medium Priority
?
64 Views
Last Modified: 2016-10-24
My company uses a custom eForm built in VS 2013 to route requests for approval.

The stored procedure that inserts the desired routing into the database expects that the Supervisor of the requester is an employee and I now have a situation where that is not so.

I would like to put a case statement in the stored procedure to take care of it, but am unsuccessful and would appreciate some input.

I am attaching the original and my attempt.
Stored-Procedures--original-and-prop.pdf
0
Comment
Question by:urthrilled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Expert Comment

by:ALEX RODRIGO FERNANDES VIEIRA
ID: 41852690
Hello my Friend,

case @supervisorEmail
when @supervisorName = 'Andy Smith' then 'asmith@smithco.com' else
@supervisorEmail end,

When you declare the main @supervisorEmail you dont need to refer "=" on the clause,
so it's just.

case @supervisorEmail
when @supervisorName  then 'asmith@smithco.com' else
@supervisorEmail end

case @supervisorEmail (let's say it's = 'test')
when @supervisorName (if it has the string 'test' aswell it will match and enter on this case, if not it will go to else) then 'asmith@smithco.com' else
@supervisorEmail end


You may could just remove the main and change to

case when @supervisorName  then 'asmith@smithco.com' else
@supervisorEmail end
0
 
LVL 29

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41852796
try,,,

ALTER PROCEDURE [dbo].[InsertApprovalNew]
(
	@WO numeric,
	@RequestorEmail varchar(50),
	@supervisorName varchar(50),
	@supervisorEmail varchar(50),
	@deptHeadName varchar(50),
	@deptHeadEmail varchar(50),
	@divPresName varchar(50),
	@divPresEmail varchar(50)
)
AS BEGIN
INSERT INTO Approvals
	(WO,
	RequestorEmail,
	RequestorApprove,
	PersonnelApprove,
	--EmployeeApprove,
	SupervisorName,
	SupervisorEmail,
	SupervisorApprove,
	DeptHeadName,
	DeptHeadEmail,
	DeptHeadApprove,
	DivPresName,
	DivPresEmail,
	DivPresApprove)--,
	--CEOApprove)
	VALUES
	(@WO, @RequestorEmail, 1, 0,
	@supervisorName,
	CASE @supervisorEmail 
			WHEN @supervisorName THEN 
				CASE @supervisorName WHEN 'Andy Smith' then 'asmith@smithco.com' else @supervisorEmail end
		
	END,
	0, @deptHeadName, @deptHeadEmail, 0, @divPresName, @divPresEmail, 0)

END

Open in new window

0
 
LVL 4

Assisted Solution

by:urthrilled
urthrilled earned 0 total points
ID: 41852818
The results I want would be if the Supervisor's Name equals Andy Smith then his email is asmith@smithco.com, otherwise find the Supervisor's email from the personnel Full Name and email as usual.

The suggestion doesn't look to me like it would do that?

Thanks for your help and explanation,
Doris
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 2000 total points
ID: 41852820
Hi Author,

I m not getting. Is that the code I provided is working as per your expectations ?

Or do you want me to change it ?
0
 
LVL 4

Author Comment

by:urthrilled
ID: 41852873
Pawan Kumar Khowal - I was able to successfully alter the Stored Procedure with your suggestion and it didn't insert the email ('asmith@smith.com') in the SupervisorEmail field for the eForm.  It looks for the Supervisor Name from the AD list of "Managers" and grabs their email, but since this one is not an Employee it is in AD as a User, but does not have the same profile as our employees.  

Thank you, hope you might have another idea?

Doris
0
 
LVL 4

Author Comment

by:urthrilled
ID: 41852955
Thank you for your help, it's working now.

Doris (urthrilled)
0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 2000 total points
ID: 41853248
Great !!
0
 
LVL 4

Author Comment

by:urthrilled
ID: 41857432
This is SO FRUSTRATING!!!!!!!!!!!!!!!!!!!!!!!!!!!

The new website configuration I cannot figure out how to give the best contributor credit and close the question.   I've marked the proper contributions twice and still the question is open and I cannot find any other action other than deleting the question or requesting attention...

I've read Experts Exchange tutorial and am not getting the prompts they mention....
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

771 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