0

I get the above error message when i run the following perl script from the cmd, that is supposed to do some transaction in my database.

#!/usr/bin/perl.

use warnings;
use strict;

my ( $pid1, $pid2, $pid3, $i );    #Declaration of local variables
$| = 1;

if ( ($pid1 = fork()) && ($pid2 = fork()) && ($pid3 = fork()) ) {       #Starts child processes
   print( "I have to wait for my kids.\n" );        #The main process execute this code
   my $straggler = wait();              #The main process waits for all the children to finish
   print( "Finally $straggler finished, now I can go.\n" );
}
elsif ( $pid1 && $pid2 && defined($pid3)) {         #Is executed by the third child 
   sleep( 1 );                      #Wait 1 second
   print( "Start moving money between account 1 - 2" );
   sleep(2);
   for ($i=0; $i < 1000; $i++) {            #Start transaction repeatedly
    system("sqlcmd -S DESKTOP-U368A1B  -Q tranKonto(1,2,100,1)"); #Moves 100 from account 1 to account 2
   }
   print( "End moving money between accounts trans 1 - 2 " );
   exit();
}
else {
   die( "Forking problems: " );
}

The transaction looks like this:

PROCEDURE [dbo].[tranKonto] @konto1 INT,  @konto2 INT, @belopp INT, @isoLevel INT
AS
BEGIN TRANSACTION tranKonto

    IF(@isoLevel=0)
        SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
    ELSE IF (@isoLevel = 1)
        SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    ELSE IF (@isoLevel=2)
        SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    ELSE IF(@isoLevel=3)
        SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

    BEGIN 
        UPDATE Konto SET belopp=belopp-@belopp WHERE kontonr=@konto1
        UPDATE Konto SET belopp=belopp+@belopp WHERE kontonr=@konto2    
        EXEC kontoLog_SP @konto1,  @konto2 , @belopp , @isoLevel;
    END

COMMIT TRANSACTION tranKonto
RETURN

I get the following error message when running the script:

Msg 102, Level 15, State 1, Server DESKTOP-U368A1B, Line 1 Incorrect syntax near '1'.

I would really appreciate if somebody could help me out. I have tried to google a solution but havn't had any luck.

  • When i remove the parenthesis i do get another error: Sqlcmd: '1,2,100,1': Unexpected argument. – user127832 Mar 27 at 16:48
1

You should remove the parentheses from around the parameters for your stored procedure, and enclose the call in double quotation marks. In a normal command line, this would look like:

sqlcmd -S DESKTOP-U368A1B  -Q "tranKonto 1,2,100,1"

Since this is in perl, you will need to escape the quotation marks:

system("sqlcmd -S DESKTOP-U368A1B  -Q \"tranKonto 1,2,100,1\"");
  • Thank you som much! This worked perfectly! – user127832 Mar 27 at 17:14
  • I would also be in the habit of proper syntax and reference, e.g. EXEC dbo.tranKonto...; and maybe even consider naming the parameters. – Aaron Bertrand Mar 27 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy