我正在处理存储过程以将项目添加到我的数据库。该过程还处理编辑项目。代码一直在跳过insert语句,即使在我的测试运行中我声明Id = 0应该启动Post而不是Put。有人可以指出我的代码中哪里出错了吗?
DECLARE @PersonId INT,
@Description VARCHAR(300),
@ModifiedById INT,
@NeedId INT
SET @PersonId = 11
SET @Description = 'Testing again'
SET @ModifiedById = 1
BEGIN
--SET NOCOUNT ON;
--SET XACT_ABORT ON;
DECLARE @TimeStamp DATETIME
SET @TimeStamp = GETUTCDATE()
BEGIN TRY
BEGIN TRANSACTION
/*--------------------------------------------------------------------------
INSERT
--------------------------------------------------------------------------*/
IF(@NeedId = 0)
BEGIN
INSERT INTO [dbo].[CustomerNeeds]
([PersonId]
,[Description]
,[IsActive]
,[LastUpdated]
,[ModifiedById]
,[CreateDate]
,[CreatedById])
VALUES
(@PersonId
,@Description
,1
,@TimeStamp
,@ModifiedById
,@TimeStamp
,@ModifiedById)
SELECT @NeedId = CAST(SCOPE_IDENTITY() AS INT)
END
/*--------------------------------------------------------------------------
UPDATE
--------------------------------------------------------------------------*/
ELSE
BEGIN
UPDATE
[dbo].[CustomerNeeds]
SET
[PersonId] = @PersonId
,[Description] = @Description
,[IsActive] = 1
,[LastUpdated] = @TimeStamp
,[ModifiedById] = @ModifiedById
WHERE
Id = @NeedId
END
/*--------------------------------------------------------------------------
DELETE ADDRESS ASSOCIATION TO PERSON
--------------------------------------------------------------------------*/
DELETE
FROM
[dbo].[CustomerNeeds]
WHERE
[PersonId] = @PersonId
AND
[Id] = @NeedId
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = 'Error happened while saving a customer need -- ' + ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
-- Raise an error and return
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
RETURN
END CATCH
-- RESULT SET 1: IList<ICustomerNeedsDataContract>
EXEC [API].[CustomerNeedsList]
@PersonId = @PersonId
END
答案 0 :(得分:2)
我想知道你的DELETE部分是否引用了错误的表格?
就目前而言,我相信你只需删除刚刚插入的行。
答案 1 :(得分:0)
DROP TABLE #CustomerNeeds
CREATE TABLE #CustomerNeeds(
[PersonId] INT IDENTITY(1,1) NOT NULL
,[Description] NVARCHAR(100)
,[IsActive] BIT
,[LastUpdated] DATETIME
,[ModifiedById] NVARCHAR(100)
,[CreateDate] DATETIME
,[CreatedById] NVARCHAR(100))
INSERT INTO #CustomerNeeds
(Description, IsActive, LastUpdated, ModifiedById, CreateDate, CreatedById)
VALUES
('test row 3', 3, CURRENT_TIMESTAMP, 'test_user', CURRENT_TIMESTAMP, 'test_user')
,('test row 4', 4, CURRENT_TIMESTAMP, 'test_user', CURRENT_TIMESTAMP, 'test_user')
,('test row 5', 5, CURRENT_TIMESTAMP, 'test_user', CURRENT_TIMESTAMP, 'test_user')
DECLARE @PersonId INT,
@Description VARCHAR(300),
@ModifiedById INT,
@NeedId INT
SET @PersonId = 1
SET @Description = 'Testing again'
SET @ModifiedById = 1
BEGIN
--SET NOCOUNT ON;
--SET XACT_ABORT ON;
DECLARE @TimeStamp DATETIME
SET @TimeStamp = GETUTCDATE()
BEGIN TRY
BEGIN TRANSACTION
/*--------------------------------------------------------------------------
INSERT
--------------------------------------------------------------------------*/
IF(@PersonId = 0)
BEGIN
PRINT 'Insert'
INSERT INTO #CustomerNeeds
([Description]
,[IsActive]
,[LastUpdated]
,[ModifiedById]
,[CreateDate]
,[CreatedById])
VALUES
(@Description
,1
,@TimeStamp
,@ModifiedById
,@TimeStamp
,@ModifiedById)
SELECT @PersonId = CAST(SCOPE_IDENTITY() AS INT)
SELECT *
FROM #CustomerNeeds
END
/*--------------------------------------------------------------------------
UPDATE
--------------------------------------------------------------------------*/
ELSE
BEGIN
PRINT 'Update'
UPDATE
#CustomerNeeds
SET
[Description] = @Description
,[IsActive] = 1
,[LastUpdated] = @TimeStamp
,[ModifiedById] = @ModifiedById
WHERE
[PersonId] = @PersonId
SELECT *
FROM #CustomerNeeds
END
/*--------------------------------------------------------------------------
DELETE ADDRESS ASSOCIATION TO PERSON
--------------------------------------------------------------------------*/
DELETE
FROM
#CustomerNeeds
WHERE
[PersonId] = @PersonId
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = 'Error happened while saving a customer need -- ' + ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
-- Raise an error and return
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH
SELECT *
FROM #CustomerNeeds
END