简体   繁体   English

通过执行T-SQL过程更新表

[英]Update on table by executing a T-SQL procedure

I need to update column in each empty row by data from procedure. 我需要通过过程中的数据更新每个空行中的列。

The problem is that I need a unique value for each record. 问题是我需要每个记录的唯一值。 Procedure returns it, but I don't know how to make it happen. 过程返回它,但是我不知道如何实现它。

The results I got so far are same in every single row - remember that when I exec procedure it check table for existing values and gives me the unique value. 到目前为止,我得到的结果在每一行中都是相同的-请记住,当我执行过程时,它会检查表中的现有值并为我提供唯一值。 How do I update every time other value by this procedure? 如何通过此过程每次更新其他值?

declare @ean varchar(40)

exec dbo.generateEAN 1, '', @ean output

update t 
set Twr_Ean = @ean 
from dbo.table t  
where t.Code = ''

Try Scalar Functions ? 尝试标量函数? Perhaps easier than Stored procedures for an in-line update statement. 内联更新语句可能比存储过程更容易。 Here's a simple snippet. 这是一个简单的片段。 But performance degrades when working over a large dataset. 但是,当处理大型数据集时,性能会下降。 You may want to rethink how you approach your problem. 您可能需要重新考虑如何解决问题。

create function dbo.ufn_mypower (@x int) 创建函数dbo.ufn_mypower(@x int)
returns int with execute as caller as 返回int,其执行者为as
begin return power( @x, 2 ) 开始返回功率(@x,2)
end 结束
go

create table updatewithfunction ( originalvalue int null 创建表updatewithfunction(originalvalue int null
,calculatedvalue int null ) ,calculatedvalue int null)

insert updatewithfunction ( originalvalue ) values (1), (2), (3), (4), (5), (6) 插入updatewithfunction(originalvalue)值(1),(2),(3),(4),(5),(6)

update updatewithfunction set calculatedvalue = dbo.ufn_mypower(originalvalue) 更新updatewith函数设置计算值= dbo.ufn_mypower(原始值)
where calculatedvalue is null and originalvalue >= 4 其中计算值为null,原始值> = 4

select *from updatewithfunction 选择* from updatewithfunction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM