Sql 用程序结果填写临时表
我有一个很有诱惑力的名字叫Sql 用程序结果填写临时表,sql,sql-server,Sql,Sql Server,我有一个很有诱惑力的名字叫#tempPrueba,像这样: +-----+----------+--------+ | ID | FiltroID | Planta | +-----+----------+--------+ | 1 | 12 | NULL | | 2 | 4 | NULL | | 3 | 8 | NULL | +-----+----------+--------+ EXEC [BSC].[ftNombreCor
#tempPrueba
,像这样:
+-----+----------+--------+
| ID | FiltroID | Planta |
+-----+----------+--------+
| 1 | 12 | NULL |
| 2 | 4 | NULL |
| 3 | 8 | NULL |
+-----+----------+--------+
EXEC [BSC].[ftNombreCortoBSC]
@pe_BSCID = 12 --this equals to FiltroID of temporary table
正如您所看到的,我有一个带有null值的Planta,我想通过执行存储过程来填充它,当我执行存储过程时,如下所示:
+-----+----------+--------+
| ID | FiltroID | Planta |
+-----+----------+--------+
| 1 | 12 | NULL |
| 2 | 4 | NULL |
| 3 | 8 | NULL |
+-----+----------+--------+
EXEC [BSC].[ftNombreCortoBSC]
@pe_BSCID = 12 --this equals to FiltroID of temporary table
它返回Planta
值,因此我想用每个FiltroID
的tetrable执行这个存储过程,并将它添加到Planta
列中。我怎样才能做到呢?问候
存储过程:
USE [SIOD]
GO
/****** Object: StoredProcedure [BSC].[ftNombreCortoBSC] Script Date: 11/04/2018 11:39:16 a. m. ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [BSC].[ftNombreCortoBSC] (@pe_BSCID INT NULL, @splanta varchar (100) OUTPUT)
AS
BEGIN
--declare @splanta varchar(100) =''
set @splanta =''
declare @tablatemporal varchar(100) = ''
declare @campollave varchar(50) = ''
declare @camponombre varchar(50) = ''
declare @query nvarchar(max) = ''
Select @tablatemporal = d.sNombreFisico from bsc.Dimension d
inner join bsc.ConfiguracionBalance cb on cb.DimensionID = d.ID
WHERE cb.mdEstatusRegistro = 1 AND d.mdEstatusRegistro = 1 and cb.ID = @pe_BSCID
Select @camponombre = 'sNombre'+da.snombre from BSC.Dimension_Atributo da
inner join bsc.ConfiguracionBalance cb on cb.DimensionID = da.DimensionID
where da.mdEstatusRegistro = 1 and da.sTipoAtributo = 'N' and cb.ID = @pe_BSCID
Select @campollave = da.snombre from BSC.Dimension_Atributo da
inner join bsc.ConfiguracionBalance cb on cb.DimensionID = da.DimensionID
where da.mdEstatusRegistro = 1 and da.mdEstatusRegistro = 1 and da.sTipoAtributo = 'L' and cb.ID = @pe_BSCID
set @query = N'Select @splanta = '+@camponombre+' from '+@tablatemporal+' as tt
inner join bsc.ConfiguracionBalance cb on cb.FiltroID = tt.key'+@campollave+' and cb.ID =
' + convert(varchar(10),@pe_BSCID)
EXECUTE sp_executesql @query,N'@splanta varchar(50) output',@splanta output
SELECT @splanta
END
如果存储过程一次只能获取一个FiltroID值,那么使用它的唯一方法是使用一个游标,该游标在临时表中的FiltroID中循环,获取与该FiltroID关联的Planta,然后更新该FiltroID的表。根据您的注释,下面是一个如何使用游标的掩码。我希望临时表只有4行,因为游标的性能很差。我就是搞不懂在将其转换为基于集合的操作的过程中您在做什么。话虽如此,这里有一个例子
--create a cursor to loop through your temp table
declare someCur cursor local fast_forward for
select id from #tempPrueba
--variables for cursor and holding output from proc
declare @id int
declare @procOutput varchar(256) --change this to the correct datatype and size
open someCur
fetch next from someCur into @id
while @@FETCH_STATUS = 0
begin
--get the result from the proc for the first ID
EXEC [BSC].[ftNombreCortoBSC] @id, @procOutput OUTPUT
--update the temp table
update #tempPrueba
set Planta = @procOutput
where ID = @id
--get the next ID
fetch next from someCur into @id
end
close someCur
deallocate someCur
在这种情况下,你能举一些使用coursor的例子吗?如果你不使用我的例子,那没关系,我想知道的是你在说什么,最终结果会是什么样子?i、 e.Planta将从何处更新或更新到何处?用从存储过程@scsimonI获得的值替换tetrable的
NULL
值。我明白这一点,但存储过程做什么?你能把那个密码贴出来吗。你不应该一个接一个地这么做。我加上了@scsimonWhoa,真是太疯狂了。我猜您的数据根本没有标准化——您能解释一下为什么某些表中有一些值以及此过程背后的逻辑吗?在EXEC行中,我得到错误:必须以'@name=value'的形式传递参数编号2和后续参数。使用格式“@name=value”后,所有后续参数必须以格式“@name=value”传递。
,我们应该更改为@procOutput=output
?您需要在过程中将@splanta设置为输出变量。或者如果这不可行,我可以改变它。让我知道@splanta
已经是sp中的输出
变量,请查看spIt的非内部过程声明的执行。。。。像这样更改:ALTER PROCEDURE[BSC].[ftNombreCortoBSC](@pe_BSCID INT NULL,@splanta varchar(100)output)
并确保从procdeclare@splanta varchar(100)=''
中删除这一行,并将其更改为set@splanta=''
,这是关于输出参数的更多信息。