0

以下代码将新记录输入到表中。但是,由于某种原因,它正在获取 3126,并且该 ID 的记录已经存在。每次运行此函数时,它都会递增 1,但 id 已经有一个相应的值,直到 3198。我将如何更改以及我将进行哪些更改,以便它获取记录中已经存在的最大值并递增ID 加 1,然后添加记录。

SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString);
try
{
    conn.Open();

    SqlCommand sqlCMD = new SqlCommand("InsertNewDeal", conn);

    sqlCMD.CommandType = System.Data.CommandType.StoredProcedure;
    sqlCMD.Parameters.Add("@Title", System.Data.SqlDbType.VarChar, 100);
    sqlCMD.Parameters.Add("@Office", System.Data.SqlDbType.Char, 3);
    sqlCMD.Parameters.Add("@EntryBy", System.Data.SqlDbType.VarChar, 50);
    sqlCMD.Parameters.Add("@CSR1", System.Data.SqlDbType.VarChar, 50);
    sqlCMD.Parameters.Add("@id", System.Data.SqlDbType.Int);
    sqlCMD.Parameters["@id"].Direction = System.Data.ParameterDirection.Output;


    sqlCMD.Parameters["@Title"].Value = txtName.Text;
    sqlCMD.Parameters["@Office"].Value = cmbOffice.SelectedValue;
    sqlCMD.Parameters["@CSR1"].Value = cmbCSR.SelectedValue;
    sqlCMD.Parameters["@EntryBy"].Value = LisaDatabaseManager.CurrentCSR.Username;

    CSR user = CSR.LoadCurrentUser();
    sqlCMD.ExecuteNonQuery();
    this.newProductionID = (int)sqlCMD.Parameters["@id"].Value;


    SqlCommand cmd = new SqlCommand("UPDATE Productions SET CountryCode = 'CAN', ProvinceCode = '" + user.GetProvinceCode() + "' WHERE ID = " + newProductionID, conn);
    cmd.ExecuteNonQuery();

    AddOptionalFields(newProductionID);

    LisaDatabaseManager.DealsTable.Fill(LisaDatabaseManager.DSGlobal.LoadDeals);
    DialogResult = DialogResult.OK;
    Close();
}
catch (Exception ex)
{
    ExceptionHandler.LogException("New deal", ex, LogSource.CHECK);
}
finally
{
    conn.Close();
}
}
}

InsertNewdeal 的存储过程如下:

ALTER PROCEDURE [dbo].[InsertNewDeal]
    @Title varchar(100),
    @EntryBy varchar(50),
    @Office char(3),
    @CSR1 varchar(50),
    @id int OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
    declare @CamProb int;
    IF @PSOffice = 'VAN'
        set @CamProb = 1;
    else
        set @CamProb = NULL;

    INSERT INTO Productions(Title, EntryDate, EntryBy, Camera, Light, Grip, Generator, Expendables,IsDead, Office, CSR1, Indie,ProbCam)
        VALUES (@Title, CURRENT_TIMESTAMP, @EntryBy, 0,0,0,0,0,0, @Office, @CSR1,0,@CamProb);

    SELECT @id = Scope_Identity();

    INSERT INTO PostMortems(ProductionID,Indie) VALUES (@id,0);

    INSERT INTO Deals(Production, Date, SubRentals,InsuranceOnFile,DealOnFile, ChargeDVan, Charge5Tonne, ChargeDVan_out, Charge5Tonne_out, ChargeBulbReplacement) VALUES(@id,CURRENT_TIMESTAMP, 'Cost+10%',0,0, 75.00, 100.00, 150.00, 200.00, 1);

    DECLARE ItemCursor Cursor FOR
        select item from dealitems where id in (46048, 46052, 46036, 1, 46054, 15, 46056, 46057, 46058)
        order by
        case
            when id = 46048 then 1
            when id = 46052 then 2
            when id = 46036 then 3
            when id = 1 then 4
            when id = 46054 then 5
            when id = 15 then 6
            when id = 46056 then 7
            when id = 46057 then 8
            else 9
        end

    DECLARE @Description varchar(100)
    DECLARE @count int
    Set @count = 0

    OPEN ItemCursor

    FETCH NEXT FROM ItemCursor INTO @Description

    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO DealMemoItems(Deal, Item, Ord)
        VALUES(@id, @Description, @count)

        SET @count = (@count + 1)

        FETCH NEXT FROM ItemCursor INTO @Description
    END
    CLOSE ItemCursor
    DEALLOCATE ItemCursor
END
4

2 回答 2

0
(SELECT TOP 1 id FROM <yourtable> ORDER BY id DESC) + 1

这会从表中获取最后一个最高 ID,并将其加一。您可以将其作为值直接放入 SQL 插入语句中,如下所示:

INSERT INTO <YOURTABLE> (id, name)
values (
     (SELECT TOP 1 id FROM <yourtable> ORDER BY id DESC) + 1,
     'Goober fish'
);
于 2013-05-31T14:48:27.583 回答
0

你能不能改变你的存储过程来使用:

SELECT SCOPE_IDENTITY()

这将返回最后插入的记录的 id。您不能依赖 id 等于最大 id 加一,因为 id 可以超时重用。

请注意,这是一个 SQL Server 解决方案,但在其他数据库引擎中通常有等价物。

于 2013-05-31T14:48:40.073 回答