2017-05-04 66 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