| Author |
Topic |
|
lsulindy
Starting Member
9 Posts |
Posted - 2008-10-30 : 17:23:37
|
| I have the following stored procedure:ALTER PROCEDURE [dbo].[training_Results] @ssn varchar(10), @baid uniqueidentifierASUPDATE EResults SET ba_id = @baidWHERE test_emp_id = right(@ssn, 6)I have another table which holds all of my employees. I only want to update the EResults if there is only 1 employee in the Employee table with those last 6 ssn digits. If there is more than one employee with that ssn, then I do not want to update the EResults. How would I do that?Thanks! |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-30 : 17:30:57
|
| UPDATE EResults SET ba_id = @baidfrom EResults r inner join Employee eon r.test_emp_id = e.emp_idwhere r.test_emp_id = right(@ssn,6)having count(e.emp_id) = 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-31 : 00:09:21
|
| [code]UPDATE r SET r.ba_id = @baidFROM EResults r INNER JOIN (SELECT COUNT(emp_id) AS EmpCount FROM Employee e WHERE emp_id=r.test_emp_id AND emp_id = right(@ssn,6))eWHERE e.EmpCount = 1[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-31 : 00:09:54
|
quote: Originally posted by hanbingl UPDATE EResults SET ba_id = @baidfrom EResults r inner join Employee eon r.test_emp_id = e.emp_idwhere r.test_emp_id = right(@ssn,6)having count(e.emp_id) = 1
wont work. you're taking count without grouping by anything. |
 |
|
|
lsulindy
Starting Member
9 Posts |
Posted - 2008-10-31 : 15:19:51
|
quote: Originally posted by visakh16
UPDATE r SET r.ba_id = @baidFROM EResults r INNER JOIN (SELECT COUNT(emp_id) AS EmpCount FROM Employee e WHERE emp_id=r.test_emp_id AND emp_id = right(@ssn,6))eWHERE e.EmpCount = 1
Thanks! I get the error "Incorrect syntax near the keyword WHERE" on the last line. Do I need and "ON" for the join? Here's a bit of background. We are transmitted test results for our employees. The only identifier we are sent is the last 6 digits of the employee's ssn. The ba_id is our unique employee number. When the case comes up that 2 of our employees have the same last 6 ssn digits, I do not assign a ba_id to the test result. Someone assigns those manually when that case comes up. So, if more than 1 employee exists in the Employee table with the same 6 digit ssn ending, then I do not want to assign a ba_id to the EResult record. This sp is used as part of a "fix" employee records button. It finds all of the EResults with the same last 6 digits as the employee's ssn and assigns them to the employee's ba_id. If there is more than one employee in our system with this employee's last 6 ssn digits, I do not want to assign all of the EResults to this employee's ba_id b/c then he/she will acquire all of the other employee's test results.UPDATE r SET r.ba_id = @ba_idFROM EResults AS r INNER JOIN (SELECT COUNT(right(ssn,6)) AS EmpCount FROM Employee e WHERE right(ssn,6) = right(@ssn,6)) as cWHERE (c.EmpCount = 1 AND r.emp_test_id = right(@ssn,6)) |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-10-31 : 15:58:35
|
[code]UPDATE aSET a.ba_id = @baidfromEResults a WHERE a.test_emp_id = right(@ssn, 6)and 1 = (Select Count(*) from Employee e where right(e.ssn,6) = right(@ssn,6)) [/code] Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
lsulindy
Starting Member
9 Posts |
Posted - 2008-10-31 : 16:21:37
|
quote: Originally posted by Vinnie881
UPDATE aSET a.ba_id = @baidfromEResults a WHERE a.test_emp_id = right(@ssn, 6)and 1 = (Select Count(*) from Employee e where right(e.ssn,6) = right(@ssn,6)) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881
THANKS!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-01 : 02:22:22
|
quote: Originally posted by lsulindy
quote: Originally posted by visakh16
UPDATE r SET r.ba_id = @baidFROM EResults r CROSS APPLY INNER JOIN (SELECT COUNT(emp_id) AS EmpCount FROM Employee e WHERE emp_id=r.test_emp_id AND emp_id = right(@ssn,6))eWHERE e.EmpCount = 1
Thanks! I get the error "Incorrect syntax near the keyword WHERE" on the last line. Do I need and "ON" for the join? Here's a bit of background. We are transmitted test results for our employees. The only identifier we are sent is the last 6 digits of the employee's ssn. The ba_id is our unique employee number. When the case comes up that 2 of our employees have the same last 6 ssn digits, I do not assign a ba_id to the test result. Someone assigns those manually when that case comes up. So, if more than 1 employee exists in the Employee table with the same 6 digit ssn ending, then I do not want to assign a ba_id to the EResult record. This sp is used as part of a "fix" employee records button. It finds all of the EResults with the same last 6 digits as the employee's ssn and assigns them to the employee's ba_id. If there is more than one employee in our system with this employee's last 6 ssn digits, I do not want to assign all of the EResults to this employee's ba_id b/c then he/she will acquire all of the other employee's test results.UPDATE r SET r.ba_id = @ba_idFROM EResults AS r INNER JOIN (SELECT COUNT(right(ssn,6)) AS EmpCount FROM Employee e WHERE right(ssn,6) = right(@ssn,6)) as cWHERE (c.EmpCount = 1 AND r.emp_test_id = right(@ssn,6))
sorry it should be CROSS APPLY |
 |
|
|
lsulindy
Starting Member
9 Posts |
Posted - 2008-11-13 : 15:29:12
|
quote: Originally posted by Vinnie881
UPDATE aSET a.ba_id = @baidfromEResults a WHERE a.test_emp_id = right(@ssn, 6)and 1 = (Select Count(*) from Employee e where right(e.ssn,6) = right(@ssn,6)) Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881
That works great. Now I want it to count across two tables. I have an Applicant table and an Employee table. They both have ssn fields. They are not joined in any way. One is a table of applicants, one of hired employees. I want the count of applicants + employees where right(ssn,6) = right(@ssn,6) |
 |
|
|
|