📜  sqlserver insert with set identity - C 编程语言(1)

📅  最后修改于: 2023-12-03 15:20:19.052000             🧑  作者: Mango

SQL Server Insert with Set Identity

Inserting data into SQL Server tables that have an identity column can be a challenge. This is because the identity column automatically generates a unique number for each row that is inserted. Fortunately, SQL Server provides a way to insert data into a table with an identity column using the "SET IDENTITY_INSERT" statement. In this article, we will show how to insert data into a table with an identity column using the "SET IDENTITY_INSERT" statement in C programming language.

1. Prerequisites

Before we start, you need to have the following installed on your computer:

  • Microsoft SQL Server
  • Visual Studio (or any C compiler)
2. Creating a SQL Server Table

First, we need to create a SQL Server table with an identity column. Here is an example SQL statement:

CREATE TABLE Customers
(
    CustomerID int identity(1,1) primary key,
    FirstName nvarchar(50) not null,
    LastName nvarchar(50) not null,
    Email nvarchar(50) not null
)

This statement creates a table called "Customers" with four columns. The first column is an identity column that starts at 1 and increments by 1 for each new row. The second column is "FirstName", the third column is "LastName", and the fourth column is "Email". The "primary key" constraint is added to the identity column to ensure that it is unique for each row.

3. Inserting Data into the Table

Once the table is created, we can insert data into it using the "SET IDENTITY_INSERT" statement. This statement allows us to insert data into the identity column directly. Here is an example C program that inserts data into the "Customers" table:

#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>

int main()
{
    SQLHANDLE sqlConnHandle;
    SQLHANDLE sqlStmtHandle;
    SQLRETURN retCode;

    // Connect to SQL Server
    retCode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlConnHandle);
    retCode = SQLSetEnvAttr(sqlConnHandle, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
    retCode = SQLAllocHandle(SQL_HANDLE_DBC, sqlConnHandle, &sqlConnHandle);
    retCode = SQLConnect(sqlConnHandle, (SQLCHAR*) "SERVER_NAME", SQL_NTS, (SQLCHAR*) "USERNAME", SQL_NTS, (SQLCHAR*) "PASSWORD", SQL_NTS);

    // Insert data into SQL Server table with identity column
    retCode = SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle);
    retCode = SQLSetStmtAttr(sqlStmtHandle, SQL_ATTR_CURSOR_TYPE, (void*) SQL_CURSOR_DYNAMIC, 0);
    retCode = SQLSetStmtAttr(sqlStmtHandle, SQL_ATTR_CONCURRENCY, (void*) SQL_CONCUR_ROWVER, 0);

    retCode = SQLExecDirect(sqlStmtHandle, (SQLCHAR*) "SET IDENTITY_INSERT Customers ON", SQL_NTS);

    retCode = SQLPrepare(sqlStmtHandle, (SQLCHAR*) "INSERT INTO Customers (CustomerID, FirstName, LastName, Email) VALUES (?, ?, ?, ?)", SQL_NTS);
    int customerId = 1;
    SQLCHAR* firstName = (SQLCHAR*) "John";
    SQLCHAR* lastName = (SQLCHAR*) "Doe";
    SQLCHAR* email = (SQLCHAR*) "johndoe@example.com";

    retCode = SQLBindParameter(sqlStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &customerId, 0, NULL);
    retCode = SQLBindParameter(sqlStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, strlen(firstName), 0, firstName, sizeof(firstName), NULL);
    retCode = SQLBindParameter(sqlStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, strlen(lastName), 0, lastName, sizeof(lastName), NULL);
    retCode = SQLBindParameter(sqlStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, strlen(email), 0, email, sizeof(email), NULL);

    retCode = SQLExecute(sqlStmtHandle);

    retCode = SQLExecDirect(sqlStmtHandle, (SQLCHAR*) "SET IDENTITY_INSERT Customers OFF", SQL_NTS);

    // Disconnect from SQL Server
    SQLDisconnect(sqlConnHandle);
    SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
    SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
    SQLFreeHandle(SQL_HANDLE_ENV, sqlConnHandle);

    return 0;
}

This program first connects to the SQL Server using the "SQLConnect" function, and then allocates a statement handle using the "SQLAllocHandle" function. The "SQLSetStmtAttr" function is used to set the attributes of the statement handle. The "SQLExecDirect" function is used to execute the "SET IDENTITY_INSERT Customers ON" statement, which allows us to insert data into the identity column directly. The "SQLPrepare" function is used to prepare the "INSERT INTO Customers" statement with parameter markers. The "SQLBindParameter" function is used to bind parameters to the statement. The "SQLExecute" function is used to execute the statement and insert the data into the table. Finally, the "SQLDisconnect" function is used to disconnect from the SQL Server and free the statement handle and connection handle.

Conclusion

In this article, we have shown how to insert data into a SQL Server table with an identity column using the "SET IDENTITY_INSERT" statement in C programming language. This can be a very useful tool for developers who need to insert data into SQL Server tables with identity columns.