使用表别名更新stmt? [英] Update stmt with an Table alias?

查看:59
本文介绍了使用表别名更新stmt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在进行从Oracle工作很长一段时间到MS $ SQL
MS SQL服务器的变化,并对一对夫妇感到沮丧简单的SQL stmt'的。或者

至少他们一直很容易。


SQL非常简单。我正在使用Max

有效日期行标准更新字段。 (PepopleSoft应用程序)


将PS_JOB更新为A set BAS_GROUP_ID =''''

其中EMPL_STATUS位于(''D'',''L'' , '从PS_JOB' R '', '' S '', '' T '')

和EFFDT =(选择最大值(EFFDT)其中EMPLID = A.EMPLID)


这个stmt无效。我在关键字''as''上收到错误。我已经尝试了



更新PS_JOB一套...

从PS_JOB更新PS_JOB一套...


相同的结果,A上的错误或来自的错误。


我也尝试将表别名添加到子查询,

技术上有效,但数据结果错误。


所以我的问题归结为:我如何在表中使用表别名在MS SQL服务器中更新

语句?

我通过创建临时表来解决这个问题。但这并不能满足我的好奇心,也不是理想的解决方案。


非常感谢,


-OK

解决方案

" John" <确定**** @ yahoo.com>在消息新闻中写道:ef ************************** @ posting.google.c om ...

大家好,

我正在改变从在Oracle工作了很长时间到MS SQL服务器,并对一些简单的SQL stmt感到沮丧。或者至少他们一直很容易。

SQL非常简单。我正在使用Max
有效日期行标准更新字段。 (PepopleSoft应用程序)

将PS_JOB更新为一组BAS_GROUP_ID =''''
其中EMPL_STATUS位于(''D'',''L'',''R'',' 'S'',''T'')
和EFFDT =(从PS_JOB中选择max(EFFDT),其中EMPLID = A.EMPLID)


别名表名子查询并引用要明确更新的表。

UPDATE中的表名不能在T-SQL或标准SQL中别名。


更新PS_JOB

组BAS_GROUP_ID = '' ''

其中EMPL_STATUS在( '' d '', '' L '', '' R '', ''S'',''T'')

和EFFDT =(从PS_JOB中选择最大值(EFFDT)AS J

其中PS_JOB.EMPLID = J.EMPLID)


-

JAG

此stmt不起作用。我在关键字''as''上收到错误。我试过了:

更新PS_JOB一套...
从PS_JOB更新PS_JOB一套......

同样结果,错误就'' ''''或''来自''的错误。

我还尝试将表别名添加到子查询中,这在技术上有效,但数据结果错误。

所以我的问题归结为:如何在MS SQL服务器的更新
语句中使用表别名?

我通过创建临时表来解决这个问题。但这并不能满足我的好奇心,也不是理想的解决方案。

非常感谢,

-OK



[发布和邮寄,请在新闻中回复]


John(ok****@yahoo.com)写道:

SQL非常简单。我正在使用Max
有效日期行标准更新字段。 (PepopleSoft应用程序)

将PS_JOB更新为一组BAS_GROUP_ID =''''
其中EMPL_STATUS位于(''D'',''L'',''R'',' 'S'',''T'')
和EFFDT =(从PS_JOB中选择最大值(EFFDT),其中EMPLID = A.EMPLID)

此stmt不起作用。我在关键字''as''上收到错误。我试过了:

更新PS_JOB一套...
从PS_JOB更新PS_JOB一套......

同样结果,错误就'' ''''或''来自''的错误。
...
所以我的问题归结为:如何在MS SQL服务器的更新
语句中使用表别名?




这很简单。你只需要一个SELECT语句,撕掉SELECT部分​​

,点击UPDATE-SET,然后你就去了:


UPDATE PS_JOB

SET BAS_GROUP_ID =''''

来自PS_JOB A

WHERE A.EMPL_STATUS IN(''D'',''L'', ''R'','''',''T'')

和A.EFFDT =(从PS_JOB B中选择最大值(EFFDT)



其中B.EMPLID = A.EMPLID)


当他说你不能在MS SQL Server中执行此操作时,JAG错了,但他是

在ANSI SQL上是正确的。事实上,大多数RDBMS在UPDATE中支持

基表上的别名,这是一个明显的事实,即ANSI只是将它全部错误地给了b $ b,而你得到的是不同的语法在不同的地方。

-

Erland Sommarskog,SQL Server MVP,所以** **@algonet.se


SQL Server SP3的联机书籍
http://www.microsoft.com/sql/techinf...2000/books.asp


>>大多数RDBMS在

UPDATE中支持基表上的别名这一事实是一个明显的事实,即ANSI只是把它全部弄错了,你得到的是b $ b得到的是不同的语法在不同的地方。 <


实际上,ANSI说得对,在UPDATE中允许

别名的各种产品给出了不同的答案,因为它们

违反了标准。


不允许UPDATE子句中的相关名称可以避免可能发生的一些自我引用问题。但它也遵循标准SQL中的

数据模型。当您为表表达式提供一个

相关名时,就好像在数据库中创建了具有该

相关名的物化表。然后那张表是在声明结尾处掉落的
。如果您在UPDATE子句中允许相关名称

,那么您将更新实体化表格,

然后会消失并保持基本表不受影响。


你认为INSERT INTO也应该允许别名吗?


Hi all,

I am doing the change from having worked in Oracle for a long time to
MS SQL server and am frustrated with a couple of simple SQL stmt''s. Or
at least they have always been easy.

The SQL is pretty straightforward. I am updating a field with a Max
effective dated row criteria. (PepopleSoft app)

update PS_JOB as A set BAS_GROUP_ID = '' ''
where EMPL_STATUS in (''D'', ''L'', ''R'', ''S'', ''T'')
and EFFDT = (select max(EFFDT) from PS_JOB where EMPLID = A.EMPLID)

This stmt is not working. I am getting an error on the keyword ''as''. I
have tried:

update PS_JOB A set...
update PS_JOB from PS_JOB A set...

Same result, error on ''A'' or error on ''from''.

I also tried to add the table alias to the sub query, which
technically worked, but with wrong data result.

So my question comes down to: How do I use a table alias in an update
statement in MS SQL server?

I worked around this by creating a temp table. But that does not
fulfill my curiosity, nor is it an ideal solution.

Thanks a lot,

-OK

解决方案

"John" <ok****@yahoo.com> wrote in message news:ef**************************@posting.google.c om...

Hi all,

I am doing the change from having worked in Oracle for a long time to
MS SQL server and am frustrated with a couple of simple SQL stmt''s. Or
at least they have always been easy.

The SQL is pretty straightforward. I am updating a field with a Max
effective dated row criteria. (PepopleSoft app)

update PS_JOB as A set BAS_GROUP_ID = '' ''
where EMPL_STATUS in (''D'', ''L'', ''R'', ''S'', ''T'')
and EFFDT = (select max(EFFDT) from PS_JOB where EMPLID = A.EMPLID)
Alias the table name in the subquery and refer to the table to be updated explicitly.
The table name in the UPDATE can''t be aliased in T-SQL or in Standard SQL.

update PS_JOB
set BAS_GROUP_ID = '' ''
where EMPL_STATUS in (''D'', ''L'', ''R'', ''S'', ''T'')
and EFFDT = (select max(EFFDT) from PS_JOB AS J
where PS_JOB.EMPLID = J.EMPLID)

--
JAG
This stmt is not working. I am getting an error on the keyword ''as''. I
have tried:

update PS_JOB A set...
update PS_JOB from PS_JOB A set...

Same result, error on ''A'' or error on ''from''.

I also tried to add the table alias to the sub query, which
technically worked, but with wrong data result.

So my question comes down to: How do I use a table alias in an update
statement in MS SQL server?

I worked around this by creating a temp table. But that does not
fulfill my curiosity, nor is it an ideal solution.

Thanks a lot,

-OK



[posted and mailed, please reply in news]

John (ok****@yahoo.com) writes:

The SQL is pretty straightforward. I am updating a field with a Max
effective dated row criteria. (PepopleSoft app)

update PS_JOB as A set BAS_GROUP_ID = '' ''
where EMPL_STATUS in (''D'', ''L'', ''R'', ''S'', ''T'')
and EFFDT = (select max(EFFDT) from PS_JOB where EMPLID = A.EMPLID)

This stmt is not working. I am getting an error on the keyword ''as''. I
have tried:

update PS_JOB A set...
update PS_JOB from PS_JOB A set...

Same result, error on ''A'' or error on ''from''.
...
So my question comes down to: How do I use a table alias in an update
statement in MS SQL server?



It''s very simple. You just take a SELECT statment, rip the SELECT part
out of it, tack on UPDATE-SET, and here you go:

UPDATE PS_JOB
SET BAS_GROUP_ID = '' ''
FROM PS_JOB A
WHERE A.EMPL_STATUS IN (''D'', ''L'', ''R'', ''S'', ''T'')
and A.EFFDT = (select max(EFFDT)
from PS_JOB B
where B.EMPLID = A.EMPLID)

JAG was wrong when he said you cannot do this in MS SQL Server, but he
is right on ANSI SQL. The fact that most RDBMSs support alias on the
base table in an UPDATE, is a clear fact that ANSI just has it all
wrong, and what you get is different syntax in different places.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


>> The fact that most RDBMSs support alias on the base table in an
UPDATE, is a clear fact that ANSI just has it all wrong, and what you
get is different syntax in different places. <<

Actually, ANSI got it right and the various products that allows an
alias in the UPDATE give slightly different answers because they
violated standards.

Not allowing correlation names in the UPDATE clause avoids some
self-referencing problems that could occur. But it also follows the
data model in Standard SQL. When you give a table expression a
correlation name, it is to act as if a materialized table with that
correlation name has been created in the database. That table then is
dropped at the end of the statement. If you allowed correlation names
in the UPDATE clause, you would be updating the materialized table,
which would then disappear and leave the base table untouched.

Do you think that INSERT INTO ought to allow an alias, too?


这篇关于使用表别名更新stmt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆