首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 在表中添加的新列不能更新的问题 [已结贴,结贴人:marbleqi]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 11:31:07 楼主
    先说明一下我想处理的具体问题。

    有多名会员在多处商家消费的记录。

    财务需要一个报表,报表的列头如下:

    会员号,会员名,1月总消费额,1月份消费量第一的商家名,1月份消费量第一的商家名处的消费量,1月份消费量第二的商家名,1月份消费量第二的商家名处的消费量,
    1月份消费量第三的商家名,1月份消费量第三的商家名处的消费量,2月总消费额,2月份消费量第一的商家名,2月份消费量第一的商家名处的消费量,2月份消费量第二的商家名,2月份消费量第二的商家名处的消费量,2月份消费量第三的商家名,2月份消费量第三的商家名处的消费量。。。。。。4月份消费量第三的商家名,4月份消费量第三的商家名处的消费量。

    也就是要每个会员每个月消费前三名的商家和相应的消费额,都放在一列。因为这个数据每个月都需要给的,所以我想写一个通用的代码,每次只要修改参数就好了。

    我目前的设想是:

    1、生成一个表tableA,列名为会员号,会员名。

    2、设置两个参数,为起始月@MonthStart和终止月@MonthEnd。然后循环,每次在tablea上添加新的列名TradeMoney08011,TradeCompany08011(表示1月份消费量第一的商家名处的消费量,1月份消费量第二的商家名),然后再更新进去。

    目前碰上一个问题,无法更新。

    原因也已经查明:比如下面的代码就会报错。

    SQL code
    if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table abc create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc ALTER Table abc add edf numeric(18,2),hij numeric(18,2) select * from abc update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 select * from abc


    下面的代码就能通过:

    SQL code
    if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table abc create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc ALTER Table abc add edf numeric(18,2),hij numeric(18,2) GO select * from abc update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 select * from abc


    也就是说,必须先将添加表列的操作在一个批中处理完后,才能更新。

    请问我的问题有什么办法解决吗?
    50  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • liangCK
    • 等级:
    发表于:2008-05-08 11:33:221楼 得分:0
    严重关注.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 11:34:092楼 得分:30
    SQL code
    用动态语句 if exists (select * from dbo.sysobjects where id = object_id(N'abc') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table abc create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc exec(' ALTER Table abc add edf numeric(18,2),hij numeric(18,2) ') select * from abc exec(' update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 ') select * from abc
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2008-05-08 11:36:343楼 得分:0
    ALTER Table abc add edf numeric(18,2),hij numeric(18,2)
    go

    加个go
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • dawugui
    • 等级:
    发表于:2008-05-08 11:37:394楼 得分:20
    SQL code
    create table abc(num int) select * from abc insert into abc(num) values(1) insert into abc(num) values(2) insert into abc(num) values(3) insert into abc(num) values(4) insert into abc(num) values(5) select * from abc ALTER Table abc add edf numeric(18,2),hij numeric(18,2) go select * from abc update abc set edf = 5 where num = 4 update abc set hij = 6 where edf = 5 select * from abc drop table abc /* num ----------- 1 2 3 4 5 (所影响的行数为 5 行) num edf hij ----------- -------------------- -------------------- 1 NULL NULL 2 NULL NULL 3 NULL NULL 4 NULL NULL 5 NULL NULL (所影响的行数为 5 行) (所影响的行数为 1 行) (所影响的行数为 1 行) num edf hij ----------- -------------------- -------------------- 1 NULL NULL 2 NULL NULL 3 NULL NULL 4 5.00 6.00 5 NULL NULL (所影响的行数为 5 行) */
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 12:22:285楼 得分:0
    你们还有什么别的方案吗?
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    世纪乐知(北京)网络技术有限公司 版权所有 京 ICP 证 020026 号
    Copyright © 2000-2007, CSDN.NET, All Rights Reserved