从选择中更新 SQL

发布于 2024-08-13 03:26:41 字数 683 浏览 8 评论 0原文

我想从其他表中选择更新表(商店)的两个字段,但我不知道如何做到这一点。 SQL系统是AS/400,所以没有SQL Server或Oracle技巧:(

这是SELECT,我想要订单的价格和金额(文章表连接仅选择现有的文章)

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

这是更新,我想更新上次选择的每篇文章的价格和数量

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

感谢您的帮助,请原谅我的泰山英语;)

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 ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味拾荒者 2024-08-20 03:26:42

如果您有驱动程序,您可以通过链接服务器查询执行此更新,即 SQL Server 可以将 AS/400 添加为链接服务器并对文件执行更新,我们有一个 AS400 DB2,我们通常通过 SQL Server 存储过程进行更新,但是您必须首先执行选择,然后运行更新(这是特定于供应商的 - IBM AS/400 w/DB2 和 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

所以基本上您正在执行选择然后更新...

不知道链接服务器是否适合您,但这是一种方法。

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.

青萝楚歌 2024-08-20 03:26:42

我相信这应该有效:

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)

WHERE EXISTS 是为了防止 NULL 结果。我假设 Store 有一个要匹配的 id 列。
仅当表针对给定的选择条件返回一行(且仅一行)时,这才有效。如果情况并非如此,您将需要提供更多详细信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文