Declare @cardNumber;
select name, address,
(select @cardNumber = select top 1 value from card cd
inner join cardNotuse cdnu on cd.id = cdnu.cardId
where cdnu.type = 'CreditCard'),
case when (@cardNumber = 1) then 'TopPriority'
when (@cardNumber = 2) then 'SecondPriority'
Else 'Normal' end as PriorityLeval
from customer
我不能以某种方式为@cardNumber
分配价值。错误我有
'' ='。
附近的语法不正确
我无法弄清楚为什么我无法使用这样的查询。
答案 0 :(得分:1)
由于变量@cardNumber与customer表无关,您可以编写两个独立的查询AS:
Declare @cardNumber int;
set @cardNumber = (SELECT top 1 value
from card cd
inner join cardNotuse cdnu on cd.id = cdnu.cardId
where cdnu.[type] = 'CreditCard')
select name,
address,
@cardNumber,
case when (@cardNumber = 1) then 'TopPriority'
when (@cardNumber = 2) then 'SecondPriority'
Else 'Normal' end as PriorityLeval
from customer
答案 1 :(得分:0)
请检查样本和评论。
Declare @cardNumber varchar(50) --here datatype missing
select name, address,
(--select @cardNumber = --you can not do 2 thing in one select query, either set variable or get result, not both in a single query
select top 1 value from card cd
inner join cardNotuse cdnu on cd.id = cdnu.cardId
where cdnu.type = 'CreditCard'
),
case when (@cardNumber = 1) then 'TopPriority'
when (@cardNumber = 2) then 'SecondPriority'
Else 'Normal' end as PriorityLeval
from customer
--1. Either set statment to get value
set @cardNumber =
(select top 1 value from card cd
inner join cardNotuse cdnu on cd.id = cdnu.cardId
where cdnu.type = 'CreditCard'
)
--2. or in select query you can set,but result always be a single value as you taken top 1 will not work for you.
select @cardNumber = --this gives error as top clause will not work
top 1 value from card cd
inner join cardNotuse cdnu on cd.id = cdnu.cardId
where cdnu.type = 'CreditCard'
--This will work. If a single value there, it return a value. If more than one value, set last record.
select @cardNumber = value _Id from card cd
inner join cardNotuse cdnu on cd.id = cdnu.cardId
where cdnu.type = 'CreditCard'