我想执行此SQL语句,但我不能100%确定它是否能正常工作。
MERGE [dbo].[Word] as target
USING [dbo].[AWL] as source
ON target.[WordId] = source.[col1]
WHEN MATCHED THEN
UPDATE SET [AWL570Sublist] = col2;
有没有办法可以启动事务,执行MERGE,检查它是否有效,然后从SQL查询窗口回滚或提交?
答案 0 :(得分:1)
出现错误回滚:
Scanner inputMenuChoice = new Scanner(System.in);
private int getMenuItem()
{
System.out.println("\nPlease select from the following");
System.out.println(ENTER_CAR + ". Enter licence plate and hours stayed");
System.out.println(DISPLAY_CARS + ". Display all licence plates, hours and fees");
System.out.println(DISPLAY_STATISTICS + ". Display Statistics");
System.out.println(SEARCH_CARS + ". Search for car");
System.out.println(EXIT + ". Exit the application");
System.out.print("Enter choice==> ");
return inputMenuChoice.nextInt();
}
private void processCars()
{
int choice = getMenuItem();
while (choice != EXIT)
{
switch (choice)
{
case ENTER_CAR:
enterCar();
break;
case DISPLAY_CARS:
displayAllCars();
break;
case DISPLAY_STATISTICS:
displayStatistics();
break;
case SEARCH_CARS:
searchCar();
break;
default:
System.out.println("ERROR choice not recognised");
}
choice = getMenuItem();
}
}
private void enterCar()
{
String licPlate="",hr="";
int d=1,carMax,numHr;
int i=0;
Scanner sa=new Scanner(System.in);
//for(int i=0;i<a.length;i++)
{
licPlate=JOptionPane.showInputDialog(null, "Please enter the license plate of the car");
while(licPlate.isEmpty())
{
if(licPlate.isEmpty())
{
JOptionPane.showMessageDialog(null, "Error - Licence Plate cannot be blank");
licPlate=JOptionPane.showInputDialog(null, "Please enter the license plate of the car");
}
}
hr=JOptionPane.showInputDialog(null, "Enter the number of hour(s) car was parked (1-12)");
numHr=Integer.parseInt(hr);
while(numHr>12 || numHr<1)
{
if(numHr>12 || numHr<1)
{
JOptionPane.showMessageDialog(null, "Error - Hours must be between 1 and 12", "Error", JOptionPane.ERROR_MESSAGE);
hr=JOptionPane.showInputDialog(null, "Enter the number of hour(s) car was parked (1-12)");
}
}
System.out.printf("Details for car %d entered\n",d);
a[i]=new Car(licPlate,numHr);
d++;
}
{System.out.println(a[i]);}
i++;
}
private void displayAllCars()
{
}
private void displayStatistics()
{
}
private void searchCar()
{
}
public static void main(String [] args)
{
CarPark app = new CarPark();
app.processCars();
}
}
好article有解释。
答案 1 :(得分:0)
BEGIN TRY;
BEGIN TRAN;
MERGE [dbo].[Word] as target
USING [dbo].[AWL] as source
ON target.[WordId] = source.[col1]
WHEN MATCHED THEN
UPDATE SET [AWL570Sublist] = col2;
IF @@ERROR = 0
BEGIN
COMMIT TRAN;
END
END TRY
BEGIN CATCH
SELECT @@ERROR AS ERROR, ERROR_MESSAGE() AS [Error Description]
ROLLBACK TRAN;
END CATCH
答案 2 :(得分:-1)
BEGIN TRANSACTION
MERGE [dbo].[Word] as target
USING [dbo].[AWL] as source
ON target.[WordId] = source.[col1]
WHEN MATCHED THEN
UPDATE SET [AWL570Sublist] = col2;
-- Other statements
ROLLBACK TRANSACTION
请注意,如果您的操作导致标识列递增,则回滚将不会在事务开始时将下一个标识值重置为它的值。