Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL-将SELECT语句的结果转换为存储过程中的参数_Sql Server - Fatal编程技术网

Sql server SQL-将SELECT语句的结果转换为存储过程中的参数

Sql server SQL-将SELECT语句的结果转换为存储过程中的参数,sql-server,Sql Server,考虑以下存储过程: alter procedure getRisks @id int, @convertedRiskValue int as select top 1 table1.id, idRisk, riskValue from table1 left join table2 on table1.id = table2.id where table1.id= @id order by table2.id desc idRisk值始终在1-4之间变化,riskValue在0-1000之间

考虑以下存储过程:

alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, idRisk, riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc
  • idRisk
    值始终在1-4之间变化,
    riskValue
    在0-1000之间变化

  • 我想做什么

    • 我想将
      riskValue
      转换为
      idRisk
      “类型”,如下所示:

,然后将其放入
@convertedRiskValue
并从select语句返回

有什么想法吗?

选择
SELECT 
   riskValue,
   CASE 
     WHEN riskValue > -1 AND riskValue < 251 THEN 1
     WHEN riskValue > 250 AND riskValue < 501 THEN 2
     WHEN riskValue > 500 AND riskValue < 751 THEN 3
     WHEN riskValue > 750 AND riskValue < 1001 THEN 4
  END AS idRisk
FROM Table
风险价值, 案例 当riskValue>-1且riskValue<251时,则为1 当riskValue>250且riskValue<501时,则为2 当riskValue>500且riskValue<751时,则为3 当riskValue>750且riskValue<1001时,则为4 以idRisk结尾 从桌子上
选择
风险价值,
案例
当riskValue>-1且riskValue<251时,则为1
当riskValue>250且riskValue<501时,则为2
当riskValue>500且riskValue<751时,则为3
当riskValue>750且riskValue<1001时,则为4
以idRisk结尾
从桌子上
试试这个:

alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, CASE WHEN RiskValue >= 0 AND RiskValue <= 250 THEN 1 
WHEN RiskValue >= 251 and RiskValue <= 500 THEN 2
WHEN RiskValue >= 501 and RiskValue <= 750 THEN 3
WHEN RiskValue >= 751 and RiskValue <= 1000 THEN 4 end as idRiskType
 , riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc
alter过程getRisks@id int、@convertedRiskValue int
作为
选择top 1 table1.id,当RiskValue>=0且RiskValue=251且RiskValue=501且RiskValue=751且RiskValue时,请尝试以下操作:

alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, CASE WHEN RiskValue >= 0 AND RiskValue <= 250 THEN 1 
WHEN RiskValue >= 251 and RiskValue <= 500 THEN 2
WHEN RiskValue >= 501 and RiskValue <= 750 THEN 3
WHEN RiskValue >= 751 and RiskValue <= 1000 THEN 4 end as idRiskType
 , riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc
alter过程getRisks@id int、@convertedRiskValue int
作为

选择顶部1 table1.id,RiskValue>=0和RiskValue=251时的情况,RiskValue=501和RiskValue=751,以及RiskValue>=0和RiskValue=251时的情况……
。(除非将参数
输出
)左连接,否则不能在
@convertedRiskValue
中放入任何内容以返回调用方?如果你没有价值怎么办?
当RiskValue>=0和RiskValue=251时的情况…
等等。(除非将参数
输出
)左连接,否则不能在
@convertedRiskValue
中放入任何内容以返回调用方?如果你没有价值怎么办?