English 中文(简体)
SQL Update from a Select
原标题:

I want to update two fields of table (Store) from a select of others tables, but i don´t know how i can do it. The SQL system is in AS/400, so doesn´t have SQL Server or Oracle tricks :(

Here is the SELECT, i want the price and amount of an order (article table join for select only existing articles)

SELECT OrderDetails.Price, OrderDetails.Amount
FROM (OrderHeader JOIN OrderDetails ON OrderHeader.Number = OrderDetails.Number)
JOIN Articles ON Article.Reference = OrderDetails.Article
WHERE OrderHeader.Store =  074 
AND   OrderHeader.Date =  2009-12-04 
AND   OrderHeader.Number = 26485

And here is the UPDATE, i want update price and amount of each article from last SELECT

UPDATE Store
SET Store.Price = *****OrderDetails.Price*****
, Store.Amount = Store.Amount + *****OrderDetails.Amount*****
... ????

Thanks for the help, and excuse my Tarzan s english ;)

问题回答

If you have the drivers, you can perform this update via a Linked Server Query, i.e. SQL Server can add the AS/400 as a linked server and perform the update on the file, we have an AS400 with DB2, we routinely do update via SQL Server Stored Procedures, but you have to do a Select First and then run your update (this is vendor specific - IBM AS/400 w/DB2 and SQL 05)

Declare @tmpSql nvarchar(1000);
Declare @baseSql nvarchar(1000);

-- Select 
Set @tmpsql =    Select * From MyAs400Library.file1 where Field1=      + @somevariable +       and Field2= + @someothervariable +     

Set @baseSql =  Update OpenQuery(LINKEDSERVERNAME,  + @tmpSql +  ) 

Set @baseSql = @baseSql +   SET Field3=   + @somevariable +     where Field1=    + @somevariable +     and Field2= + @someothervariable +   

exec sp_executesql @baseSql

So basically you re doing a SELECT and then an update...

Don t know if a Linked server is an option for you but this is one way.

I believe this should work:

UPDATE Store as ST (Price, Amount) = (SELECT OD.Price, ST.Amount + OD.Amount
                                      FROM OrderHeader as OH
                                      JOIN OrderDetails as OD
                                      ON OH.Number = OD.Number
                                      JOIN Articles as A
                                      ON A.Reference = OD.Article
                                      WHERE OH.Store = ST.Store
                                      AND OH.Date =  2009-12-04 
                                      AND OH.Number = 26485)
WHERE ST.Store =  074 
      AND EXISTS (SELECT  1 
                  FROM OrderHeader as OH
                  JOIN OrderDetails as OD
                  ON OH.Number = OD.Number
                  JOIN Articles as A
                  ON A.Reference = OD.Article
                  WHERE OH.Store = ST.Store
                  AND OH.Date =  2009-12-04 
                  AND OH.Number = 26485)

The WHERE EXISTS is to prevent against NULL results. I m assuming Store has an id column to match.
This will only work if the tables will return one (and only one) row for the given selection criteria. If this is not the case, you will need to supply more details.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签