Solved

Help with a case statement in a SQL Stored Procedure

Posted on 2016-10-20
8
35 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 23

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
 
LVL 23

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 23

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

943 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

4 Experts available now in Live!

Get 1:1 Help Now