SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update query using select query result
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Aged Yak Warrior

540 Posts

Posted - 01/02/2013 :  09:50:18  Show Profile  Reply with Quote
Is it possible for an update query to use the select query.

update T_EMPLOYEES set title_flag = (below select query TITLE_FLAG based on T_STG_LW_EMPLOYEE.Employee)

select DISTINCT T_STG_LW_EMPLOYEE.Employee,
 CASE T_STG_LW_AUMXVALUE.matrix_cat
	             WHEN  'DIR' then 'Y'
	             WHEN 'DIR2' then 'Y'
	             WHEN 'VP' then 'Y'
	             ELSE 'N'
              END as TITLE_FLAG
from T_STG_LW_EMPLOYEE
left outer join T_STG_LW_AUMXVALUE
on (ltrim(rtrim(T_STG_LW_EMPLOYEE.first_name)) + ' ' + ltrim(rtrim(T_STG_LW_EMPLOYEE.Last_name))) = 
T_STG_LW_AUMXVALUE.mx_value order by T_STG_LW_EMPLOYEE.employee

Join: T_EMPLOYEES.EMPLOYEE_NUMBER = T_STG_LW_EMPLOYEE.Employee


Thanks a lot for the helpful info.

Ifor
Aged Yak Warrior

590 Posts

Posted - 01/02/2013 :  10:10:41  Show Profile  Reply with Quote

UPDATE T_EMPLOYEES
SET title_flag =
	CASE
		WHEN EXISTS
		(
			SELECT 1
			FROM T_STG_LW_EMPLOYEE E
			WHERE E.Employee = T_EMPLOYEES.EMPLOYEE_NUMBER
				AND EXISTS
				(
					SELECT 1
					FROM T_STG_LW_AUMXVALUE A
					WHERE A.mx_value = LTRIM(RTRIM(E.first_name)) + ' ' + LTRIM(RTRIM(E.Last_name))
						AND A.matrix_cat IN ('DIR', 'DIR2', 'VP')
				)
		)
		THEN 'Y'
		ELSE 'N'
	END;
Go to Top of Page

shan
Yak Posting Veteran

USA
84 Posts

Posted - 01/02/2013 :  10:14:10  Show Profile  Reply with Quote
This will meet what you need.

update b
set title_flag = a.TITLE_FLAG
from T_EMPLOYEES b
inner join
(
select DISTINCT T_STG_LW_EMPLOYEE.Employee,
CASE T_STG_LW_AUMXVALUE.matrix_cat
WHEN 'DIR' then 'Y'
WHEN 'DIR2' then 'Y'
WHEN 'VP' then 'Y'
ELSE 'N'
END as TITLE_FLAG
from T_STG_LW_EMPLOYEE
left outer join T_STG_LW_AUMXVALUE
on (ltrim(rtrim(T_STG_LW_EMPLOYEE.first_name)) + ' ' + ltrim(rtrim(T_STG_LW_EMPLOYEE.Last_name))) =
T_STG_LW_AUMXVALUE.mx_value
) A
on b.EMPLOYEE_NUMBER = a.Employee


-Shan

Edited by - shan on 01/02/2013 10:20:52
Go to Top of Page

cplusplus
Aged Yak Warrior

540 Posts

Posted - 01/02/2013 :  13:04:58  Show Profile  Reply with Quote
Ifor, Shan, It worked.
Thank you very much.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000