我创建了以下sql语句,$()中的每个东西都是一个变量(来自我们在我们网站上使用的模块,因此在这种情况下$(varname)是正确的格式,而不是@varname。
它一直告诉我那附近有一个错误......我该如何正确地格式化?
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
谢谢,
马特
答案 0 :(得分:3)
摆脱THEN
,并在存储过程调用之前添加EXEC
。
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
另外,你应该使用一些更好的缩进。如果这是我的代码,我会将其格式化为:
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
这很长,但又很容易维护,因为你不必向左和向右滚动来查看整个过程调用。
答案 1 :(得分:1)
在t-sql if语句中没有Then
答案 2 :(得分:1)
除了其他人提到的额外“Then”之外,还需要在if语句的主体内存储过程调用之前添加exec。