2017-05-04 85 views
0

我在一列中有一组值,如DeviceId1,DeviceId2,DeviceId3等。 我的示例输入包含一个表,其中一列如下所示。在一组值和存储过程之间比较结果SQL

Device 

DeviceId1 
DeviceId2 

现在对于在此列中的每个值我想将DeviceId1传递到一个存储过程将返回对应于传递的每个输入多个值。我希望最终的结果能像这样在一张桌子上。

Device  DeviceParameter1FromSP DeviceParameter2FromSP 

DeviceId1 100      200 
DeviceId2 120      222 

获得此结果的最佳方法是什么? 注意:存储过程无法更改。 我还没有尝试过任何东西,因为我无法想象一种方法。

+0

请张贴您的示例输入日期。您可以对此方法使用数据透视功能。但要明确你的问题 –

+0

@JimMacaulay:我发布了样本输入数据。 – ckv

+0

100,200,...等值如何?你如何生成它们? –

回答

1

你应该声明光标和exec你的每一行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 
0

创建用户定义的表类型和表来存储你所期望的结果(例如我创建临时表)和存储过程,你需要

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<[email protected]) 
     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 [email protected] 
       SET @[email protected]+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; 

执行存储过程与样本数据

DECLARE @DeviceTbl AS DeviceTbl 

INSERT INTO @DeviceTbl 
SELECT 1,100,200 UNION ALL 
SELECT 2,120,222 

EXEC DBO.usp_DeviceOutPut @[email protected] 

SELECT * FROM ##Result 

输出

Device  DeviceParameter1FromSP DeviceParameter2FromSP 

1    100      200 
2    120      222