Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.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 在SqlServer中执行Update命令之前,如何在更新过程中设置变量?_Sql Server_Variables_Set_Procedure - Fatal编程技术网

Sql server 在SqlServer中执行Update命令之前,如何在更新过程中设置变量?

Sql server 在SqlServer中执行Update命令之前,如何在更新过程中设置变量?,sql-server,variables,set,procedure,Sql Server,Variables,Set,Procedure,我正在创建一个计算固定资产折旧递减的程序。 为此,我需要在基于案例的过程中设置一个值,而案例又基于我在Update命令中使用的表中的值。 在更新表之前,我需要知道如何设置该变量。 问题如下所示: Create procedure Depreciation as begin declare @k numeric (10,2) set @k=case when Value From a table inside the FROM(UPDATE)>1 then 2 else 3 end up

我正在创建一个计算固定资产折旧递减的程序。 为此,我需要在基于案例的过程中设置一个值,而案例又基于我在Update命令中使用的表中的值。 在更新表之前,我需要知道如何设置该变量。 问题如下所示:

Create procedure Depreciation
as begin
declare @k numeric (10,2) 
set @k=case 
when Value From a table inside the FROM(UPDATE)>1 then 2
else 3
end
update Deprectiation Table set (Deprectiation=@k*Value)
From Tables
where conditions 

谢谢。

这是一个错误的语法。尽管您发布的代码一点也不清楚,也不确定您想要实现什么,但您可以执行以下操作

Create procedure Depreciation
as 
begin
declare @k numeric (10,2); 
declare @value numeric (10,2); 
select @value = [Value], @k = case when [Value] > 1 then 2 else 3 end 
From table1 ;

update Deprectiation set Deprectiation=@k*@value where conditions;
end
试试这个:

Create Procedure Description
AS Begin
    Update DeprectiationTable 
    SET Deprectiation=CASE WHEN Value>1 THEN 2 ELSE 3 END * Value
    From Tables
    Where conditions 
End