次のコードは、テーブルに新しいレコードを入力します。しかし、何らかの理由で 3126 を取得しており、その ID のレコードが既に存在しています。この関数を実行するたびに、1 ずつインクリメントされますが、3198 までの id に対応する値が既に存在します。レコードに既に存在する最大値を取得して、 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