views:

46

answers:

3

I've created the following sql statement, every thing in $() is a variable (comes from the module we're using on our site so $(varname) is the right format in this case, not @varname.

It keeps telling me there's an error near THEN... how am I supposed to format this properly?

IF ($(Shiptobilling) = 'yes') THEN
    BEGIN
        CardOrder_Add $(UserID), $(Firstname), $(Lastname), $(BillStreet1), $(BillCity), $(BillState), $(BillCountry), $(BillZip), getdate(), $(ExpiryDate)
    END
ELSE
    BEGIN
        CardOrder_Add $(UserID), $(Firstname), $(Lastname), $(ShipStreet1), $(ShipCity), $(ShipState), $(ShipCountry), $(ShipZip), getdate(), $(ExpiryDate) 
    END

Thanks,
Matt

+3  A: 

Get rid of the THEN, and add EXEC before your stored procedure calls.

IF ($(Shiptobilling) = 'yes') 
BEGIN
EXEC CardOrder_Add $(UserID), $(Firstname), $(Lastname), $(BillStreet1), $(BillCity), $(BillState), $(BillCountry), $(BillZip), getdate(), $(ExpiryDate)
END
ELSE
BEGIN
EXEC CardOrder_Add $(UserID), $(Firstname), $(Lastname), $(ShipStreet1), $(ShipCity), $(ShipState), $(ShipCountry), $(ShipZip), getdate(), $(ExpiryDate) 
END

Also, you should use some better indentation. If this were my code I'd format it more like this:

IF ($(Shiptobilling) = 'yes') 
  BEGIN
  EXEC CardOrder_Add 
    $(UserID), 
    $(Firstname), 
    $(Lastname), 
    $(BillStreet1), 
    $(BillCity), 
    $(BillState), 
    $(BillCountry), 
    $(BillZip), 
    getdate(), 
    $(ExpiryDate)
  END
ELSE
  BEGIN
  EXEC CardOrder_Add 
    $(UserID), 
    $(Firstname), 
    $(Lastname), 
    $(ShipStreet1), 
    $(ShipCity), 
    $(ShipState), 
    $(ShipCountry), 
    $(ShipZip), 
    getdate(), 
    $(ExpiryDate) 
  END

It's pretty long, but then again it's easy to maintain because you don't have to scroll left and right to see the entire procedure call.

Welbog
+1 beat me to it
Irwin M. Fletcher
Sorry the indentation was a result of poor copy paste formatting from the sandbox I was using... I got rid of the THEN, now I'm getting another error: Incorrect syntax near CardOrder_Add
Matt
@Matt: The `EXEC` statement would certainly help there. At least, I'm assuming `CardOrder_Add` is a stored procedure.
Welbog
What is CardOrder_Add? Is it a stored procedure? You need EXEC CardOrder_Add, and you should also have the schema prefix (e.g. EXEC dbo.CardOrder_Add). I assume your module translates $(UserID) etc. to @UserID because otherwise that will be the next syntax error.
Aaron Bertrand
+1  A: 

There is no Then in t-sql if statments

Irwin M. Fletcher
+1  A: 

As well as the extra "Then" as others have mentioned, you will need to add exec before the stored procedure calls within the body of the if statements.

scottman666