Sqlserver
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Sqlserver

Chèn một danh sách <> vào bảng SQL Server

Tôi giả sử bạn nói SQL (ngôn ngữ truy vấn có cấu trúc) và ý bạn thực sự là Microsoft SQL Server (sản phẩm cơ sở dữ liệu thực tế) thay vào đó - phải không?

Bạn không thể chèn toàn bộ danh sách vào SQL Server - bạn cần chèn một hàng cho mỗi mục nhập. Điều này có nghĩa là bạn cần gọi câu lệnh INSERT nhiều lần.

Làm như thế này:

// define the INSERT statement using **PARAMETERS**
string insertStmt = "INSERT INTO dbo.REPORT_MARJORIE_ROLE(REPORT_ID, ROLE_ID, CREATED_BY, CREATED) " + 
                    "VALUES(@ReportID, @RoleID, 'SYSTEM', CURRENT_TIMESTAMP)";

// set up connection and command objects in ADO.NET
using(SqlConnection conn = new SqlConnection(-your-connection-string-here))
using(SqlCommand cmd = new SqlCommand(insertStmt, conn)
{
    // define parameters - ReportID is the same for each execution, so set value here
    cmd.Parameters.Add("@ReportID", SqlDbType.Int).Value = YourReportID;
    cmd.Parameters.Add("@RoleID", SqlDbType.Int);

    conn.Open();

    // iterate over all RoleID's and execute the INSERT statement for each of them
    foreach(int roleID in ListOfRoleIDs)
    {
      cmd.Parameters["@RoleID"].Value = roleID;
      cmd.ExecuteNonQuery();
    }

    conn.Close();
}      


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. nhóm sql theo so với riêng biệt

  2. Crystal Reports so với Microsoft SQL Server Reporting Services

  3. Hàm SUM () trong SQL Server

  4. 5 Lợi ích bảo mật của các giải pháp giám sát cơ sở dữ liệu dựa trên đám mây

  5. Làm thế nào để thực hiện một GROUP BY phân biệt chữ hoa chữ thường?