表值函数是否可更新

时间:2013-05-15 14:57:04

标签: sql sql-server-2008

我对更新声明感到有点困惑,但这就是我所拥有的:我有这两位员工及其各自的字母数字代码。

select * from cm.bo.hotlist('08Z')
where State = 'ca'

select * from cm.bo.hotlist('06D')
where State = 'ca'

该表有某些与每个员工相关联的城市,top select语句中有与'08Z'关联的这些城市列表...让我们说。

New York
Chicago

我想将这些城市转移到员工'06D'

我如何更新?

对我来说令人困惑的部分是表是一个表值函数。

非常感谢任何帮助。谢谢。

也许是这样的:

update CITY cm.bo.hotlist('06D')

where CITY in (New York, Chicago)

2 个答案:

答案 0 :(得分:4)

所以你想要的是:

Update cm.bo.hotlist('08Z')
set
<EmployeeID Column> = '06D'
where
city in ('New York', 'Chicago')

对于所有来到这里的人,是的,只要底层数据集是可更新的,就可以更新内联表值函数。代码示例:

IF EXISTS(select * from sys.objects where name = 'test' and schema_id = schema_id('dbo')) BEGIN DROP TABLE dbo.test; END

CREATE TABLE dbo.test(Employee varchar(10), city varchar(10));

CREATE FUNCTION [dbo].[getEmployeeCities] ( @employee varchar(10) RETURNS TABLE  AS
RETURN  (  SELECT * from test where employee = @employee );

insert into dbo.test select 'A', 'Chicago';
insert into dbo.test select 'B', 'New York';

select * from dbo.test;

update dbo.getEmployeeCities('A')
set Employee = 'B'
where city = 'Chicago';

select * from dbo.test;

答案 1 :(得分:0)

更新表名 SET雇员='06D' CITY IN('纽约','芝加哥')