2013-04-04 50 views
1

我嘗試了很多,你看到得到這個代碼更聰明......它總是一樣的...一個變量可以有6倍不同的值。 ..這決定在哪一列的其他值將被寫入......如果記錄仍然存在,我做一個「更新...」別的我做的「插入...」IF ELSE IF ELSE IF ....代碼想要變得更聰明

我試着做一些動態SQL ...但它不工作...

我試着做一些「CASE ...」代碼...但它不工作...

此代碼的工作:

-- How many roads must a man walk down... 
IF @DatenFeld = 'weaNr' 
    IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
     INSERT INTO @StaDa_Table (DatenSatz, weaNr) VALUES (@DatenSatz, Convert(nvarchar(20),@DatenWert)) 
    ELSE 
     UPDATE @StaDa_Table SET weaNr = Convert(nvarchar(20),@DatenWert) WHERE [email protected] 
ELSE 
    IF @DatenFeld = 'weaTyp' 
     IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
      INSERT INTO @StaDa_Table (DatenSatz, weaTyp) VALUES (@DatenSatz, Convert(nvarchar(20),@DatenWert)) 
     ELSE 
      UPDATE @StaDa_Table SET weaTyp = Convert(nvarchar(20),@DatenWert) WHERE [email protected] 
    ELSE  
     IF @DatenFeld = 'nennP_W' 
      IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
       INSERT INTO @StaDa_Table (DatenSatz, nennP_W) VALUES (@DatenSatz, Convert(int,@DatenWert)) 
      ELSE 
       UPDATE @StaDa_Table SET nennP_W = Convert(int,@DatenWert) WHERE [email protected] 
     ELSE 
      IF @DatenFeld = 'refErt_Wh' 
       IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
        INSERT INTO @StaDa_Table (DatenSatz, refErt_Wh) VALUES (@DatenSatz, Convert(bigint,@DatenWert)) 
       ELSE 
        UPDATE @StaDa_Table SET refErt_Wh = Convert(bigint,@DatenWert) WHERE [email protected] 
      ELSE 
       IF @DatenFeld = 'inbetrieb' 
        IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
         INSERT INTO @StaDa_Table (DatenSatz, inbetrieb) VALUES (@DatenSatz, Convert(datetime,@DatenWert)) 
        ELSE 
         UPDATE @StaDa_Table SET inbetrieb = Convert(datetime,@DatenWert) WHERE [email protected] 
       ELSE 
        IF @DatenFeld = 'uw' 
         IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
          INSERT INTO @StaDa_Table (DatenSatz, uw) VALUES (@DatenSatz, Convert(nvarchar(50),@DatenWert)) 
         ELSE 
          UPDATE @StaDa_Table SET uw = Convert(nvarchar(50),@DatenWert) WHERE [email protected] 

...所以也許有人北京時間哪個更熟悉TSQL可以幫助我得到這個代碼更聰明?

+4

*它不工作*是不是一個非常有用的評論 - 請** **解釋爲何以及如何不起作用;你會得到一個錯誤 - 如果是這樣的話:***你得到了什麼***錯誤?它沒有返回任何值嗎?錯誤的值?它回來了什麼,你期望什麼? – 2013-04-04 14:06:18

+0

你知道有一個'case'聲明,你已經嘗試過了 - 你有谷歌它看它是如何使用? – 2013-04-04 14:06:38

+1

人們不應該忘記問永恆的問題:你有什麼嘗試?至今? 「請爲我做,大聲笑」並不真正在SO上工作。 ;-) – akluth 2013-04-04 14:07:27

回答

1

也許是這樣的幫助:

declare @weaNr nvarchar(20) = null 
declare @weaTyp nvarchar(20) = null 
declare @nennP_W int = null 
declare @refErt_Wh bigint = null 
declare @inbetrieb datetime = null 
declare @uw nvarchar(50) = null 

select @weaNr = case when @DatenFeld = 'weaNr' then Convert(nvarchar(20),@DatenWert) end 
     ,@weaTyp = case when @DatenFeld = 'weaTyp' then Convert(nvarchar(20),@DatenWert) end 
     ,@nennP_W = case when @DatenFeld = 'nennP_W' then Convert(int,@DatenWert) end 
     ,@refErt_Wh = case when @DatenFeld = 'refErt_Wh' then Convert(bigint,@DatenWert) end 
     ,@inbetrieb = case when @DatenFeld = 'inbetrieb' then Convert(datetime,@DatenWert) end 
     ,@uw = case when @DatenFeld = 'uw' then Convert(nvarchar(50),@DatenWert) end 

IF NOT EXISTS(SELECT * FROM @StaDa_Table WHERE [email protected]) 
    INSERT INTO @StaDa_Table (DatenSatz, weaNr, weaTyp, nennP_W, refErt_Wh, inbetrieb, uw) 
    VALUES (@DatenSatz, @weaNr, @weaTyp, @nennP_W, @refErt_Wh, @inbetrieb, @uw) 
ELSE 
    UPDATE @StaDa_Table 
    SET  weaNr = ISNULL(@weaNr, weaNr) 
      ,weaTyp = ISNULL(@weaTyp, weaTyp) 
      ,nennP_W = ISNULL(@nennP_W, nennP_W) 
      ,refErt_Wh = ISNULL(@refErt_Wh, refErt_Wh) 
      ,inbetrieb = ISNULL(@inbetrieb, inbetrieb) 
      ,uw = ISNULL(@uw, uw) 
    WHERE [email protected] 

假設所有列都爲空。