带有 IF ELSE 的 SQL 脚本无法正常工作
我使用的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标量变量只是一个值。当您分配变量时,您认为哪一行填充该变量? SQL Server 将选择任意行。在本例中,它选择了 5,因此只执行了其中一个查询。暴力破解的简单方法是:
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:
用例语句如下:
use case statement as follows: