Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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之间比较结果_Sql_Sql Server - Fatal编程技术网

在一组值和存储过程SQL之间比较结果

在一组值和存储过程SQL之间比较结果,sql,sql-server,Sql,Sql Server,我在一列中有一组值,如DeviceId1、DeviceId2、DeviceId3等。 我的示例输入由一个表和一列组成,如下所示 Device DeviceId1 DeviceId2 现在,对于本列中的每个值,我想将DeviceId1传递到一个存储过程中,该存储过程将返回与传递的每个输入相对应的更多值。我希望最终结果在一张表中是这样的 Device DeviceParameter1FromSP DeviceParameter2FromSP DeviceId1 100

我在一列中有一组值,如DeviceId1、DeviceId2、DeviceId3等。 我的示例输入由一个表和一列组成,如下所示

Device

DeviceId1
DeviceId2
现在,对于本列中的每个值,我想将DeviceId1传递到一个存储过程中,该存储过程将返回与传递的每个输入相对应的更多值。我希望最终结果在一张表中是这样的

Device       DeviceParameter1FromSP   DeviceParameter2FromSP

DeviceId1    100                      200
DeviceId2    120                      222
获得此结果的最佳方法是什么? 注意:不能更改存储过程。
我还没有尝试过任何方法,因为我想不出一种方法。

您应该为循环中的每一行声明游标并执行sp

DECLARE @result TABLE (
    @DeviceId                INT,
    @DeviceParameter1FromSP  INT,
    @DeviceParameter2FromSP  INT,
)

DECLARE @DeviceId                INT, 
        @DeviceParameter1FromSP  INT,
        @DeviceParameter2FromSP  INT,

DECLARE cur CURSOR FOR
    SELECT DeviceId
    FROM dbo.DeviceList

OPEN cur 
FETCH NEXT FROM cur INTO @DeviceId

WHILE @@FETCH_STATUS = 0 
BEGIN
    EXEC dbo.YOUR_SP_NAME @DeviceId, @DeviceParameter1FromSP OUTPUT, @DeviceParameter2FromSP OUTPUT

    INSERT INTO @result VALUES(@DeviceId, @DeviceParameter1FromSP, @DeviceParameter2FromSP)

    FETCH NEXT FROM cur INTO @DeviceId1
END

CLOSE cur 
DEALLOCATE cur

SELECT * FROM @result

创建用户定义的表类型和表来存储您期望的结果,例如,我已经根据您的需要创建了临时表和存储过程

CREATE TABLE ##Result  (
    DeviceId                INT,
    DeviceParameter1FromSP  INT,
    DeviceParameter2FromSP  INT
)

CREATE TYPE DeviceTbl AS TABLE(
    DeviceId                INT,
    DeviceParameter1FromSP  INT,
    DeviceParameter2FromSP  INT
)



IF OBJECT_ID('dbo.usp_DeviceOutPut') IS NOT NULL
BEGIN 
    DROP PROCEDURE dbo.usp_DeviceOutPut 
END 

CREATE PROCEDURE [dbo].[usp_DeviceOutPut]
(
@DeviceTbl AS DeviceTbl READONLY       
)
As 
BEGIN TRANSACTION;
BEGIN TRY

IF EXISTS (SELECT 1 From @DeviceTbl)
BEGIN
DECLARE @minid Int,@maxId INT, 
        @Sql NVARCHAR(MAX),
        @DeviceParameter1FromSP INT,
        @DeviceParameter2FromSP INT,
        @DeviceId INT

SELECT @minid=MIN(DeviceId) From @DeviceTbl
SELECT @maxId=MAX(DeviceId) From @DeviceTbl

        WHILE (@minid<=@maxId)
        BEGIN
                INSERT INTO ##Result(
                            DeviceId,
                            DeviceParameter1FromSP,
                            DeviceParameter2FromSP
                            )
                SELECT  DeviceId,
                        DeviceParameter1FromSP ,
                        DeviceParameter2FromSP 
                FROM @DeviceTbl o  WHERE NOT EXISTS (SELECT 1 From ##Result R Where R.DeviceId=o.DeviceId )--Duplicate records cnnot be inserted with this clause
                AND o.DeviceId=@minid
                SET @minid=@minid+1
        END
END


END TRY
BEGIN CATCH
    SELECT 
         ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;


    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
END CATCH;

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;
输出

Device       DeviceParameter1FromSP   DeviceParameter2FromSP

1             100                      200
2             120                      222

请张贴您的样品输入日期。您可以使用pivot函数来实现这种方法。但要清楚你的想法question@JimMacaulay:我已经发布了示例输入数据。100200等值如何。您是如何生成它们的?@jimmacauley:这些是SP针对设备列中的每个项目返回的值。@ckv该SP对您的需求有用吗
Device       DeviceParameter1FromSP   DeviceParameter2FromSP

1             100                      200
2             120                      222