简体   繁体   English

在 access 2013 中对条件更新表使用 SQL 语句

[英]Using SQL statement for conditional update tables in access 2013

I am new to databases.我是数据库的新手。 I am creating an Access database to track the inventory dynamics of our company.我正在创建一个 Access 数据库来跟踪我们公司的库存动态。 I have two tables.我有两张桌子。 One is the current inventory stock, another is consumption raised by production activity.一个是当前库存,另一个是生产活动增加的消费。 The update is done immediately after each production.每次生产后立即进行更新。

Before doing update, I want to verify the same unit is used in both stock status and consumption sheet.在进行更新之前,我想验证库存状态和消耗表中使用了相同的单位。 I try to do something like these:我尝试做这样的事情:

Private Sub Command4_Click()
   CurrentDb.Execute "SELECT o.Unit, s.Material_Unit" & _
                     "CASE WHEN o.Unite = s.Material_Unit" & _
                     "THEN UPDATE tbl_Current_Stock As o INNER JOIN bl_Temp_Raw_Material_Consumption AS s ON o.Raw_Material = s.[Ingredient/Packaging material] Set o.Stock_Level = o.Stock_Level - s.Consumption" & _
                    " Print'Congratulations! You have successfully updated inventory balance!'" & _
                     "ElSE PRINT ' Units of source data and targeted data are not matched!'" & _
                     "Exit Sub" & _
                     "End" & _
                    "FROM tbl_Current_Stock As o, bl_Temp_Raw_Material_Consumption AS s"
END sub

But it seems some errors exist.但似乎存在一些错误。 Please help me on these codes.请帮助我处理这些代码。

Thanks.谢谢。

Your code is totally wrong.你的代码是完全错误的。 You can not use CASE to datermine which statement to execute, UPDATE or PRINT ( PRINT is useless here, in MS Access).您不能使用CASE来确定要执行的语句、 UPDATEPRINT (在 MS Access 中PRINT在这里没用)。

You need to use either:您需要使用:

IF statement in your SQL query - declare o.Unite and s.Material_Unit as variables and compare them, if they match then execute update. SQL 查询中的IF语句 - 将o.Unites.Material_Unit声明为变量并比较它们,如果它们匹配则执行更新。

IF @unite = @materialunite 
BEGIN
     [Your Update Statement]
END

The other way: Declare VB variables, assign the o.Unite and s.Material_Unit values to them, and write IF - ELSE statements in VB and execute your SQL queries.另一种方式:声明 VB 变量,将o.Unites.Material_Unit值分配给它们,并在 VB 中编写IF - ELSE语句并执行 SQL 查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM