2017-06-13 81 views
1

這是發生在第4行關鍵字'Select'附近的語法不正確。對於balancecode

UPDATE CashBalances 
    SET Balance1=0, 
     Balance2=0 
WHERE PID=100040 
    AND Date='20081024' 

IF @@ROWCOUNT=0 
    INSERT INTO CashBalances(PID,Date,BalanceCode,Balance1,Balance2,CCY) 
    VALUES (100040 ,'20081024', (SELECT TOP 1 balancecode 
            FROM cashbalances 
            WHERE date=(SELECT MAX(Date) 
               FROM CashBalances 
               WHERE PID=100040) 
    IF @@ROWCOUNT = 0 SELECT 'I' AS balancecode),0,0,' ') 

我試圖檢索balancecode其中日期是從一個特定的PID最大。 如果返回零行,則返回'I'作爲餘額代碼。

謝謝。

+3

不能內部子查詢使用'IF'。 – Arvo

回答

1

只需創建另一個valiable,然後將其插入:

IF (SELECT COUNT(PID) FROM CashBalances WHERE PID=100040) > 0 
BEGIN 
    UPDATE CashBalances SET Balance1=0,Balance2=0 WHERE PID=100040 AND Date='20081024'; 
END 
ELSE 
BEGIN 
    --Here your create it, could be that you use different type 
    DECLARE @ConditionalValue NVARCHAR(MAX) = 'I'; 

    --Here you set it with a right value that you need on condition 
    IF @@ROWCOUNT > 0 
    SET @ConditionalValue = (SELECT TOP 1 
           FROM cashbalances 
           WHERE date=(SELECT MAX(Date) 
             FROM CashBalances 
             WHERE PID=100040)) 
    --Insert your final value 
    INSERT INTO CashBalances(PID, Date, BalanceCode, Balance1, Balance2, CCY) 
    VALUES (100040 ,'20081024', @ConditionalValue,0,0,' ') 
END 
+0

在這段代碼中總會有一個插入。我認爲只有在沒有行更新時才應該進行更新。問題中有'@@ ROWCOUNT = 0'。 – Peter

+0

@Peter可能是,讓我們等待OP的更多細節 –

+0

@Peter你是對的,它總是插入一個新的記錄。 –

1

要麼堅持選擇或值。不是都。如Arvo提到的,如果在查詢中不能使用。您可以使用大小寫,但您不需要它。

update CashBalances 
set Balance1=0, Balance2=0 
where PID=100040 AND Date='20081024' 

if @@ROWCOUNT=0 
Begin 
    declare @Balancecode char(1)='I' 

    select top 1 @Balancecode = balancecode 
    from cashbalances cb 
    where PID=100040 
    order by date desc 

    insert into CashBalances(PID,Date,BalanceCode,Balance1,Balance2,CCY) 
    values (100040 ,'20081024', @balanceCode,0,0,' ') 
end 
+0

您應該努力使**一致* *如何編寫SQL關鍵字 - *要麼將它們寫入所有大寫字母(SELECT,FROM),要麼以小寫字母(select,from)或混合大小寫形式(Select,From)寫入它們,但請選擇一種**然後**堅持它** - 不要把它們全部混合在一起! –

+0

@marc_s感謝您的反饋。我更喜歡小寫,但是複製了一些代碼。我已經更新了我的答案。 – Peter

0

你可以用不同的方式來做到這一點,如下所示。

如果PID=100040 AND Date='20081024'上沒有數據插入查詢將起作用。

DECLARE @Query NVARCHAR(MAX) = ' 
    INSERT INTO CashBalances(PID,Date,BalanceCode,Balance1,Balance2,CCY) 
    VALUES 
    (
     100040 , 
     ''20081024'', 
     COALESCE ((SELECT TOP 1 balancecode FROM cashbalances WHERE date=(SELECT MAX(Date) FROM CashBalances WHERE PID=100040), ''I'', 
     0, 
     0, 
     '' '' 
    )'; 


SELECT @Query = 'UPDATE CashBalances 
        SET Balance1=0, 
        Balance2=0 
        WHERE PID=100040 
       AND Date=''20081024'' 
       ' 
WHERE 
    PID=100040 AND 
    Date='20081024' 


EXECUTE sp_executesql @Query 
0
UPDATE CashBalances SET Balance1=0,Balance2=0 WHERE PID=258 AND 
Date='20081024' 

IF @@ROWCOUNT = 0 
BEGIN 
DECLARE @BalCode Char(1) 
SET @BalCode = ISNULL((SELECT TOP 1 Balancecode 
FROM CashBalances 
WHERE Date=(SELECT MAX(Date) FROM CashBalances WHERE PID=258)),'I') 

INSERT INTO CashBalances(PID,Date,BalanceCode,Balance1,Balance2,CCY) 
VALUES (258 ,'20081024',@BalCode,0,0,' ') 
END 

--- @ teo van tok 
相關問題