带有 IF ELSE 的 SQL 脚本无法正常工作

发布于 2025-01-08 02:20:15 字数 2987 浏览 0 评论 0原文

我使用的是 SQL Server 2000。有一个名为 Trialbalance_diff 的表。结构如下图所示。

accno       des                 month   diff

1010011001  Cash in Hand          5     -732230.0
1030033001  Seylan Bank           4      309042.0
1050011001  Lease Debtors         2        9899.0
1050011002  Lease VAT Suspense    5        2240.0

我需要将这些数据重写到另一个名为 temp_TB_Diff 的表中。应根据月份插入字段Diff。像这样,

acc_code    Acc_desc               Jan           Feb       Mar      Apr     May

1010011001  Cash in Hand                                             -732230.0
1030033001  Seylan Bank                                    309042.0
1050011001  Lease Debtors               9899.0
1050011002  Lease VAT Suspense                                          2240.0

我为此编写了这个SQL脚本。

DECLARE @month int;

SELECT @month = [month] FROM trialbalance_diff

BEGIN 

IF @month = 1
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, jan)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 2
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Feb)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 3
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Mar)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 4
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Apr)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 5
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, May)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 6
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, June)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 7
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, July)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 8
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Aug)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 9
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Sep)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 10
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Oct)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 11
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Nov)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 12
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, [Dec])
    SELECT accno, des, diff FROM trialbalance_diff

END

除了一个小问题之外,它可以工作。数据不会插入特定月份。应该进入不同月份列的值仅插入到一列中。

看起来像这样,

acc_code    Acc_desc            Jan      Feb       Mar       Apr        May

1010011001  Cash in Hand                                             -732230.0
1030033001  Seylan Bank                                               309042.0
1050011001  Lease Debtors                                               9899.0
1050011002  Lease VAT Suspense                                          2240.0

我在脚本中缺少什么导致了这个问题?我似乎不明白为什么。

任何帮助将不胜感激。非常感谢。

I'm using SQL Server 2000. There is a table called trialbalance_diff. The structure is shown below.

accno       des                 month   diff

1010011001  Cash in Hand          5     -732230.0
1030033001  Seylan Bank           4      309042.0
1050011001  Lease Debtors         2        9899.0
1050011002  Lease VAT Suspense    5        2240.0

I need these data to be re-written to another table called temp_TB_Diff. The field Diff should be inserted according to the month. Like this,

acc_code    Acc_desc               Jan           Feb       Mar      Apr     May

1010011001  Cash in Hand                                             -732230.0
1030033001  Seylan Bank                                    309042.0
1050011001  Lease Debtors               9899.0
1050011002  Lease VAT Suspense                                          2240.0

I wrote this SQL script for this.

DECLARE @month int;

SELECT @month = [month] FROM trialbalance_diff

BEGIN 

IF @month = 1
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, jan)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 2
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Feb)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 3
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Mar)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 4
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Apr)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 5
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, May)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 6
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, June)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 7
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, July)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 8
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Aug)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 9
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Sep)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 10
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Oct)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 11
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, Nov)
    SELECT accno, des, diff FROM trialbalance_diff
ELSE IF @month = 12
    INSERT INTO temp_TB_Diff(acc_code, acc_desc, [Dec])
    SELECT accno, des, diff FROM trialbalance_diff

END

It works except for one small problem. The data doesn't get inserted to specific months. The values which are supposed to go to different month columns get inserted to only one column.

It looks like this,

acc_code    Acc_desc            Jan      Feb       Mar       Apr        May

1010011001  Cash in Hand                                             -732230.0
1030033001  Seylan Bank                                               309042.0
1050011001  Lease Debtors                                               9899.0
1050011002  Lease VAT Suspense                                          2240.0

What am I missing in the script that causes this? I can't seem to figure out why.

Any help would be appreciated. Thanks very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我还不会笑 2025-01-15 02:20:15

标量变量只是一个值。当您分配变量时,您认为哪一行填充该变量? SQL Server 将选择任意行。在本例中,它选择了 5,因此只执行了其中一个查询。暴力破解的简单方法是:

INSERT INTO temp_TB_Diff(acc_code, acc_desc, Jan)
    SELECT accno, des, diff FROM trialbalance_diff WHERE Month = 1;

INSERT INTO temp_TB_Diff(acc_code, acc_desc, Feb)
    SELECT accno, des, diff FROM trialbalance_diff WHERE Month = 2;

INSERT INTO temp_TB_Diff(acc_code, acc_desc, Mar)
    SELECT accno, des, diff FROM trialbalance_diff WHERE Month = 3;

...

A scalar variable is just one value. Which row do you think populates the variable when you assign it? SQL Server is going to pick an arbitrary row. In this case it chose 5, so only one of your queries were executed. Brute force simple way is:

INSERT INTO temp_TB_Diff(acc_code, acc_desc, Jan)
    SELECT accno, des, diff FROM trialbalance_diff WHERE Month = 1;

INSERT INTO temp_TB_Diff(acc_code, acc_desc, Feb)
    SELECT accno, des, diff FROM trialbalance_diff WHERE Month = 2;

INSERT INTO temp_TB_Diff(acc_code, acc_desc, Mar)
    SELECT accno, des, diff FROM trialbalance_diff WHERE Month = 3;

...
拔了角的鹿 2025-01-15 02:20:15

用例语句如下:

 INSERT INTO temp_TB_Diff
    SELECT 
    accno, 
    des, 
    case when month = 1 then diff end as jan,
    case when month = 2 then diff end as feb,
    case when month = 3 then diff end as mar,
    case when month = 4 then diff end as apr
    ....

    from trialbalance_diff

use case statement as follows:

 INSERT INTO temp_TB_Diff
    SELECT 
    accno, 
    des, 
    case when month = 1 then diff end as jan,
    case when month = 2 then diff end as feb,
    case when month = 3 then diff end as mar,
    case when month = 4 then diff end as apr
    ....

    from trialbalance_diff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文