Wednesday, March 28, 2012

MULTI-Table Update Queries

I'm new to adp w/ sql server but I have to use it on a project i'm
doing...
One of the MUSTS for this project is the ability to update a 00 - 09
text value with the appropriate text description from another table...
Easy as pie in .mdb. Of course In the stored procedure it barks at me
and tells me that an update query can only have one table.. ouch that
hurts...

I'm currently reading on the subject but this group has been very
helpful in the past....

I found this link...
http://www.sqlservercentral.com/col...stheeasyway.asp

Unfortunetly I'm using MSDE not Enterprise so I don't think I can use
the query analyser.. But I tryed it in my Access ADP anyway
it barked at me..

I tried to go from this...

SELECT dbo.LU_SEX.SEX_CODE, dbo.TEST.DEFECTS_DP1
FROM dbo.TEST INNER JOIN
dbo.LU_SEX ON dbo.TEST.SEX_DP1 =
dbo.LU_SEX.SEX_DEC

To this...

UPDATE dbo.TEST.SEX_DP1
SET dbo.TEST.SEX_DP1 = dbo.LU_SEX.SEX_CODE
FROM dbo.LU_SEX INNER JOIN
dbo.TEST ON dbo.LU_SEX.SEX_DEC =
dbo.TEST.SEX_DP1

Maybe I need a good book on this?

Thanks,
CharlesIt looks like you wanted:

UPDATE dbo.TEST
SET dbo.TEST.SEX_DP1 = dbo.LU_SEX.SEX_CODE
FROM dbo.LU_SEX
JOIN dbo.TEST
ON dbo.LU_SEX.SEX_DEC = dbo.TEST.SEX_DP1

The table name follows the UPDATE keyword, not the column name.

This is the MS proprietary syntax for an UPDATE statement. There is a
potential problem with that method if you try to update using a join
that doesn't return a unique value for each row. No error is returned
but the result is undefined and may be unreliable. This is one reason I
prefer the ANSI Standard UPDATE syntax, which SQL Server also supports:

UPDATE Test
SET sex_dp1 =
(SELECT sex_code
FROM LU_Sex
WHERE sex_dec = Test.sex_dp1)

I'm guessing this will be the equivalent of what you posted. It will be
so long as sex_dec is unique in LU_Sex and all the values of sex_dp1
also exist in that table.

Presumably you ultimately intend to use one set of codes througout the
database. There is in fact an International standard for gender codes
(0=not known 1=male 2=female 9=not specified). You could also use views
to convert the codes if you need to do so for display for example.

--
David Portas
SQL Server MVP
--|||(meyvn77@.yahoo.com) writes:
> I tried to go from this...
> SELECT dbo.LU_SEX.SEX_CODE, dbo.TEST.DEFECTS_DP1
> FROM dbo.TEST INNER JOIN
> dbo.LU_SEX ON dbo.TEST.SEX_DP1 =
> dbo.LU_SEX.SEX_DEC
> To this...
> UPDATE dbo.TEST.SEX_DP1

You have a database dbo with a schmea TEST and table SEX_DP1?
Drop SEX_DP1 to start with.

> SET dbo.TEST.SEX_DP1 = dbo.LU_SEX.SEX_CODE

And here drop dbo.TEST on the left-hand side. You can uses prefixes on
the left-hand side, but I don't recall under which circumstances. In any
case, since you can only update one table at a time, it's redundant.

> FROM dbo.LU_SEX INNER JOIN
> dbo.TEST ON dbo.LU_SEX.SEX_DEC =
> dbo.TEST.SEX_DP1

This should be fine, although I recommend you to use aliases to make
your queries less verbose.

> Maybe I need a good book on this?

Books Online, see signature.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

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

No comments:

Post a Comment