Solved

Help with a case statement in a SQL Stored Procedure

Posted on 2016-10-20
8
60 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 500 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 500 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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