简体   繁体   English

从 MS Access 更新查询在 SQL 服务器中创建存储过程

[英]Create stored procedure in SQL Server from a MS Access update query

I'm trying to create my first stored procedure.我正在尝试创建我的第一个存储过程。

I did copy the following SQL from MS Access to a SQL Server stored procedure and I have "LEFT JOIN" syntax errors.我确实将以下 SQL 从 MS Access 复制到了 SQL 服务器存储过程,并且出现“LEFT JOIN”语法错误。 What's wrong with?有什么问题?

UPDATE dbo.AssemblyInstanceMassProperties
LEFT JOIN dbo.BOM ON dbo.AssemblyInstanceMassProperties.ID = dbo.BOM.AssemblyInstanceID
SET dbo.AssemblyInstanceMassProperties.NumberofSubInstance = 1
WHERE (((dbo.BOM.AssemblyInstanceID) IS NULL));

In TSQL for an update involving multiple tables you should always write a SELECT statement first and UPDATE that.在 TSQL 中,对于涉及多个表的更新,您应该始终首先编写 SELECT 语句并对其进行更新。 And always use table aliases.并且始终使用表别名。 And always terminate each statement with a ;并且总是用;终止每个语句. .

So first write a query that returns the rows you want to update, explicitly listing all columns involved in the UPDATE:因此,首先编写一个返回要更新的行的查询,明确列出 UPDATE 中涉及的所有列:

SELECT p.NumberofSubInstance, *
FROM dbo.AssemblyInstanceMassProperties p
LEFT JOIN dbo.BOM b
ON p.ID = b.AssemblyInstanceID
 WHERE b.AssemblyInstanceID Is Null;

then when you're confident you've got the right rows turn it into an UPDATE like this:然后当你确信你有正确的行时,把它变成这样的更新:

with q as
(
    SELECT p.NumberofSubInstance
    FROM dbo.AssemblyInstanceMassProperties p
    LEFT JOIN dbo.BOM b
    ON p.ID = b.AssemblyInstanceID
     WHERE b.AssemblyInstanceID Is Null
)
update q set NumerOfSubInstance = 1;

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

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