Solved

Help with a case statement in a SQL Stored Procedure

Posted on 2016-10-20
8
30 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
  • 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 17

Accepted Solution

by:
Pawan Kumar Khowal 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
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal 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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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 17

Assisted Solution

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now