我有一个TSQL-preparedStatement,用于根据ID更新表中的单个寄存器。该寄存器也可能与多对多关系有关,应该更新。换句话说,应该增加新的关系,可能需要删除一些关系。
这是伪代码(最后是完整的SQL代码):
SQL工作正常,但当我将语句构建为java.sql.Connection.prepareStatement
并调用.executeUpdate()方法时,我想存储返回的整数值,以确保至少有一行更新。如果没有更新行,则表示更新失败,并且我向用户应用程序界面报告失败。
如果座位列表保持不变,则此TSQL尤其会返回失败。即使预订登记详细信息已更新。如果预订更新,我希望它返回1,并且只有在没有任何事情发生时才返回0。
返回的行数如何在TSQL中运行? java方法与TSQL不兼容吗?如何确保更新已发生?
SET XACT_ABORT ON
BEGIN TRANSACTION
DECLARE @BookingID INT = 33;
UPDATE Booking SET DateTime=6543546, PersonID=4, Comment='', Confirmed='true', StartDate=43265436534, EndDate=-1, StartSession=720, EndSession=900 WHERE BookingID = @BookingID;
DELETE FROM BookingHasSeat WHERE BookingID = @BookingID AND SeatID NOT IN (1,2,3,4,5,6);
DECLARE @SeatID1 INT = 1;
IF @SeatID1 NOT IN (SELECT SeatID FROM BookingHasWorkStation WHERE BookingID = @BookingID)
BEGIN
INSERT INTO BookingHasSeat (BookingID, SeatID) VALUES (@BookingID, @SeatID1);
END
DECLARE @SeatID2 INT = 2;
IF @SeatID2 NOT IN (SELECT SeatID FROM BookingHasWorkStation WHERE BookingID = @BookingID)
BEGIN
INSERT INTO BookingHasWorkStation (BookingID, SeatID) VALUES (@BookingID, @SeatID2);
END
DECLARE @SeatID3 INT = 3;
IF @SeatID3 NOT IN (SELECT SeatID FROM BookingHasWorkStation WHERE BookingID = @BookingID)
BEGIN
INSERT INTO BookingHasWorkStation (BookingID, SeatID) VALUES (@BookingID, @SeatID3);
END
COMMIT TRANSACTION
答案 0 :(得分:-1)
使用JDBC调用存储过程时,应使用CallableStatement。