Solved

Help with a case statement in a SQL Stored Procedure

Posted on 2016-10-20
8
39 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 28

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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 28

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 28

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBScript Write Column Headers 3 38
Help Required 3 97
VB.NET 2008 - SQL Timeout 9 24
SQL - Update field defined as Text 6 17
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

809 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