我正在尝试从多个表中进行选择,并且某些列值将基于这样的情况(else 循环中的语句给出错误)
(select case
when @caQuoteIdTemp = corporateQuotes.caQuoteID
then @randomNumber
else (set @caQuoteIdTemp = corporateQuotes.caQuoteID )
end )as costmoney
这里是我这样声明的局部变量@caQuoteIdTemp
-@randomNumber
DECLARE @randomNumber FLOAT
declare @caQuoteIdTemp int
set @randomNumber = convert(numeric(15,0),rand() * 899999999999999) + 100000000000000
set @caQuoteIdTemp = 1
现在我的问题是:如何在 else 语句中设置@caQuoteIdTemp
to的值?@caQuoteIdTemp = corporateQuotes.caQuoteID
目前我收到语法错误。我想知道是否根本不可能更改变量的值
编辑:::
完整的查询是这样的
DECLARE @randomNumber FLOAT
declare @caQuoteIdTemp int
set @randomNumber = convert(numeric(15,0),rand() * 899999999999999) + 100000000000000
set @caQuoteIdTemp = 1
select
corporateQuotes.caQuoteID,
prod_sub.ProdID,
corporateQuoteItems.prodSubID,
prod.publisherID,
prod.Title,
prod.code,
corporateQuotes.date_ordered,
(select DATEPART(WK, corporateQuotes.date_ordered)) as week_ordered,
(select DATEPART(MONTH, corporateQuotes.date_ordered)) as month_ordered,
(select DATEPART(YEAR, corporateQuotes.date_ordered)) as year_ordered,
prod_sub.[length],
prod_sub.issues,
prod_sub.extra_info,
-- cost money --
(select case
when corporateQuotes.discountIsPoundValue = 1
then prod_sub.your_price + prod_sub.commission - corporateQuotes.discount
else (prod_sub.your_price + prod_sub.commission - (corporateQuotes.discount * prod_sub.your_price/100))
end as costmoney)as costmoney,
corporateAccounts.companyName,
-- saving money --
(prod_sub.rrp * prod_sub.issues - prod_sub.your_price )as savingmoney,
corporateAccounts.title,
corporateAccounts.firstName,
corporateAccounts.lastName,
corporateAccounts.street,
corporateAccounts.suburb,
corporateAccounts.[state],
corporateAccounts.postcode,
corporateAccounts.country,
corporateAccounts.phone,
-- Do something about TransID here--
(select @caQuoteIdTemp =(
case
when @caQuoteIdTemp = corporateQuotes.caQuoteID
then @randomNumber
else corporateQuotes.caQuoteID
as test) ,
@randomNumber as st_transId,
prod_sub.start_issue,
prod_sub.effort_code,
prod_sub.premium_code,
--do something about commison --
-- discount applied --
(select
case
when corporateQuotes.discountIsPoundValue = 1
then corporateQuotes.discount
else (corporateQuotes.discount * prod_sub.your_price/100)
end as discountApplied)as discountApplied,
-- pending orderID --
--PP transID --
-- commison --
corporateQuotes.commission,
corporateQuotes.discount
from corporateQuotes
join corporateQuoteItems on
corporateQuotes.caQuoteID = corporateQuoteItems.caQuoteID
join corporateAccounts on
corporateQuotes.caID = corporateAccounts.caID
join prod_sub on
corporateQuoteItems.prodSubID = prod_sub.ProdSubID
join prod on
prod_sub.ProdID = prod.ProdID
where corporateQuotes.orderId is null
--group by corporateQuotes.caQuoteID
order by corporateQuotes.caQuoteID
;