我现在所做的工作:
insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
select
@group_id,
pt.PaymentTypeID
from
PaymentType pt
where charindex(pt.Title, @payment_type) > 0
直到@payment_type为null,才会插入任何内容。
任何人都可以帮我找到一个优雅的方法吗? (where子句是伪代码)
insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
select
@group_id,
pt.PaymentTypeID
from
PaymentType pt
where
if @payment_type is null
PaymentTypeID = 2
else
charindex(pt.Title, @payment_type) > 0
编辑:我非常感谢答案,但不幸的是,这些答案都没有更新表格。这种伪代码可以更好地解释需要发生什么。如果@payment_type为null,则将groupID插入GroupId列,将值2插入PaymentTypeId列,否则正常运行该语句。
where
if @payment_type is null
INSERT INTO GroupAcceptedPaymentType (GroupID, PaymentTypeID)
VALUES (GroupId, '2')
else
charindex(pt.Title, @payment_type) > 0
编辑:我们找到了解决方案,但它不会让我回答我自己的问题(8小时。也许我应该设置我的警报?),但现在是。希望它可以帮助某人:
if @payment_type <> ''
begin
insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
select
@group_id,
pt.PaymentTypeID
from
PaymentType pt
where charindex(pt.Title, @payment_type) > 0
end
else
begin
INSERT INTO GroupAcceptedPaymentType (GroupID, PaymentTypeID)
VALUES (@group_id, '2')
end
答案 0 :(得分:2)
怎么样
select
@group_id,
pt.PaymentTypeID
from
PaymentType pt
where
(@payment_type is null AND PaymentTypeID = 2)
OR
(@payment_type is not null AND charindex(pt.Title, @payment_type) > 0)
答案 1 :(得分:1)
您可以使用OR
insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
select
@group_id,
pt.PaymentTypeID
from
PaymentType pt
where
(@payment_type is null AND
PaymentTypeID = 2 )
OR
(charindex(pt.Title, @payment_type) > 0)
答案 2 :(得分:1)
试试这个 -
where (@payment_type is null and PaymentTypeID = 2)
or (charindex(pt.Title, @payment_type) > 0)