0

我正在尝试从多个表中进行选择,并且某些列值将基于这样的情况(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 语句中设置@caQuoteIdTempto的值?@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
;
4

2 回答 2

0

你所拥有的是一个 select 语句,但在 else 部分你所做的只是设置一个变量。我假设您想选择 @randomNumber 如果 Id 匹配,否则只需重置变量,在这种情况下为什么不使用 IF...ELSE... 例如

IF (EXISTS (SELECT * FROM corporateQuotes WHERE @caQuoteIdTemp = corporateQuotes.caQuoteID))
    SELECT  @randomNumber AS costmoney
ELSE
    SET @caQuoteIdTemp = corporateQuotes.caQuoteID 
于 2013-03-20T12:53:32.863 回答
0
DECLARE @randomNumber      FLOAT
declare @caQuoteIdTemp int
set @randomNumber = convert(numeric(15,0),rand() * 1000) + 1000
set @caQuoteIdTemp = 1

select @caQuoteIdTemp =(case when @caQuoteIdTemp = corporateQuotes.caQuoteID
    then @randomNumber
    else corporateQuotes.caQuoteID
                        end ) from corporateQuotes

SELECT @caQuoteIdTemp as costmoney

我减少了随机数的范围,以前的值导致算术溢出。

于 2013-03-20T13:18:11.803 回答