SQL Server 中的 INSERT INTO SET 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6973846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:36:32  来源:igfitidea点击:

INSERT INTO SET syntax in SQL Server

sqlsql-server

提问by Alexander Molodih

I come from mySQL to SQL Server. Doesn't the following syntax work in SQL Server?

我从 mySQL 到 SQL Server。以下语法在 SQL Server 中不起作用吗?

 INSERT INTO table SET fil1="234", fil2="324"

Is there an comparable statement in SQL Server?

SQL Server 中是否有类似的语句?

采纳答案by Tim Schmelter

The SETway to insert records is not standard SQL. If you need it to use similar sql's for updates and inserts, you should use Stored-Procedures in MS SQL-Server instead, for example:

SET插入记录的方法是不是标准的SQL。如果您需要使用类似的 sql 进行更新和插入,则应改用 MS SQL-Server 中的存储过程,例如:

CREATE Procedure tableInsertUpdate
(
     @ID int,
     @fil1 int,
     @fil2 int,
     @IDOut int OUTPUT
)
AS
     IF EXISTS(SELECT ID from table WHERE ID=@ID)
     BEGIN
        UPDATE table SET
            fil1 = @fil1 
            fil2 = @fil2 
        WHERE ID=@ID
        SET @IDOut=null
      END
      ELSE
      BEGIN
         INSERT INTO table 
         (fil1, fil2)
         VALUES
         (@fil1, @fil2 )
         SET @IDOut=scope_identity()
      END

回答by JK.

INSERT INTO table (fil1, fil2) VALUES ('234', '324');