![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
If I have two tables with the following schema: CREATE TABLE TEST1(iID int primary key, sChar char(1)) CREATE TABLE TEST2(iID int primary key, sChar char(1)) And fill them with data as follows: insert TEST1(iID,sChar) VALUES (1,'a') insert TEST1(iID,sChar) VALUES (2,'b') insert TEST2 (iID,sChar) VALUES (1,'c') insert TEST2 (iID,sChar) VALUES (2,'d') What would be the portable way to update TEST2 such that TEST2.sChar = TEST1.sChar where TEST2.iID = TEST1.iID. To clarify, the end result of the update statment should be table 2 with the following recordsets: iID sChar 1 'a' 2 'b' I've done my due diligence in searching for an answer on this one but every answer I have seen involves UPDATE..FROM which is proprietary. The only solution I can come with on my own would be to either use a cursor or a while loop. Cheers, Tyler |
#3
| |||
| |||
|
|
"Tyler Hudson" <TylerH (AT) Spam (DOT) MeNOTallpax.com> wrote If I have two tables with the following schema: CREATE TABLE TEST1(iID int primary key, sChar char(1)) CREATE TABLE TEST2(iID int primary key, sChar char(1)) And fill them with data as follows: insert TEST1(iID,sChar) VALUES (1,'a') insert TEST1(iID,sChar) VALUES (2,'b') insert TEST2 (iID,sChar) VALUES (1,'c') insert TEST2 (iID,sChar) VALUES (2,'d') What would be the portable way to update TEST2 such that TEST2.sChar = TEST1.sChar where TEST2.iID = TEST1.iID. To clarify, the end result of the update statment should be table 2 with the following recordsets: iID sChar 1 'a' 2 'b' I've done my due diligence in searching for an answer on this one but every answer I have seen involves UPDATE..FROM which is proprietary. The only solution I can come with on my own would be to either use a cursor or a while loop. Cheers, Tyler Use a correlated subquery: Update Test2 set sChar = (Select test1.sChar from test1 where test1.iID = test2.iID ) ; -- Paul Horan Sr. Architect VCI Springfield, Mass |
#4
| |||
| |||
|
#5
| ||||
| ||||
|
|
The standard syntax is INSERT INTO, not INSERT. |
|
There is no FROM clause in a Standard SQL UPDATE statement; it would make no sense. |
|
Other products (SQL Server, Sybase and Ingres) also use the UPDATE .. FROM syntax, but with different semantics. So it does not port, or even worse, when you do move it, it trashes your database. Other programmers cannot read it and maintaining it is harder. And when Microsoft decides to change it, you will have to do a re-write. Remember the deprecated "*=" versus "LEFT OUTER JOIN" conversions? |
|
The correct syntax for a searched update statement is update statement> ::= UPDATE <table name SET <set clause list [WHERE <search condition>] |
#6
| |||||
| |||||
|
|
Just another example of useless noise words in the SQL standard. |
|
On the contrary it's [FROM clause] one of the few useful extensions of T-SQL. |
|
Don't pretend that the standard doesn't change, it's a vendor driven |
|
In what version of the standard? In which vendor's dialect? |
|
The correct syntax for an update statement depends on the DBMS you are using, not some unrealistic approximation designed by a committee. |
#7
| ||||||||||
| ||||||||||
|
|
Are you a big fan of APL or LISP? They are much, much more compact. And much, much less readable, too. You must hate the fact that about 78% of the commercial programs in the world are still in COBOL. Have you looked at any of the research on how much a few noise words help in reading (and therefore in maintaining) code? |
|
Many years ago, there was an article in CACM on translating COBOL into Arabic at the grammar level. That is not just replacing English words with Arabic, but arranging the word order in a COBOL compiler to match the word order of Arabic. |
|
On the contrary it's [FROM clause] one of the few useful extensions of T-SQL. Which version of this syntax do you find useful? |
|
In older versions of Sybase/SQL Server, if a base table row is represented more than once in the embedded query, then that row is operated on multiple times instead of just once. A total violation of relational principles. <example |
|
Standard SQL is very consistent and very clear about aliases, views and derived tables, as well as a highly orthogonal language. |
|
If the UPDATE clause could take an alias, according to the Standard SQL model, you would create a copy of the contents of that base table under the alias name, then update that copy, and delete it when the statement was over -- in effect doing nothing to the base table. If the UPDATE clause could take a FROM clause, according to the Standard SQL model, you would create a nameless copy of the result set of the table expression, then update that copy, and delete it when the statement was over -- in effect doing nothing to the base tables. If you gave the result set of the table expression a name, the same rules would apply and you would still be doing nothing to the base tables. |
|
Since this syntax is so proprietary, inconsistent with the standard model and ambiguous, why does it exist? In the original Sybase product, the *physical* model made this "extension" relatively easy to implement and there were no standards or even a good understanding of the relational model. This is how a sequential file system would do an update based on a tape merge -- **shudder**. |
|
Don't pretend that the standard doesn't change, it's a vendor driven pile of you know what. And when the SQL standards committee decides to change it, you will have to do a re-write. The standards DO change, but they change one and only one way. Vendor extension change many different ways. Which is why the original poster is having problems. |
|
In what version of the standard? In which vendor's dialect? SQL-92 and above; everyone that I can think of has it at their core, even if they added extensions. The reason they agree has to do with FIPS-127 conformance testing (do you want a federal contract?) and the original System-R SQL language. |
|
Wrong. A standard is very specific and not at all approximate. And wasn't that evil committee made up of those same vendors? The real shame is that nobody seems to be working on ROW() constructors for their UPDATE statements. |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
snip lengthy discussion I will assume from your silence on these issues that you agree with me and have conceeded the point that from clause updates are a useful extension to the SQL language. |
#10
| |||
| |||
|
|
I will assume from your silence on these issues that you agree with me and have conceeded the point that from clause updates are a useful |
![]() |
| Thread Tools | |
| Display Modes | |
| |