| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 618 人关注过本帖
标题:请教:批量SQL命令如何在VFP里执行(不用存储过程实现)
只看楼主 加入收藏
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
结帖率:58.02%
收藏
已结贴  问题点数:20 回复次数:9 
请教:批量SQL命令如何在VFP里执行(不用存储过程实现)
因为出于保密计算方法的原因不想在SQL数据库中建存储过程,有什么办法运行本来要建成存储过程的一大批T-SQL(sql代码如下),类似把下面的SQL脚本语句封装在VFP里执行

    --更新数据
    update a set a.oee_num=isnull(b.bd_num,0),a.oee_den=isnull(b.bd_total,0),
    a.cal_oee=round(case when isnull(b.bd_total,0)=0 then 0.00 else isnull(b.bd_num,0)/b.bd_total*100.0 end,2),
    a.oee_coef=isnull(b.bd_coef,1),
    a.dis_oee=round(case when isnull(b.bd_total,0)=0 then 0.00 else isnull(b.bd_num,0)/b.bd_total*100.0 end*isnull(b.bd_coef,1),2),
    a.modify_time=getdate()
    from ky_mcp.dbo.ky_mcp_oee a
    left join(
        select c.parent_location_id, b.location_id,b.date_code,b.data_type,b.bd_coef,sum(b.bd_num) as bd_num,sum(b.bd_total) as bd_total--,b.bd_cal_data,b.bd_coef,b.bd_dis_data
        from ky_bd_tmp_data b with(nolock)
        join bs_location c with(nolock) on b.location_id=c.location_id
        where b.bd_type='OEE'
        group by c.parent_location_id, b.location_id,b.date_code,b.data_type,b.bd_coef
    )b on a.wsp_id=b.parent_location_id and a.line_id=b.location_id and  a.day_type=b.date_code
   
    declare @wsp_id varchar(50),@tody varchar(10)
    declare ky_proc_mcp_oee_cursor_wsp cursor for
        select location_id
        from bs_location with(nolock)
        where parent_location_id='' and location_type_code='area' and is_delete=0
    open ky_proc_mcp_oee_cursor_wsp
    fetch next from ky_proc_mcp_oee_cursor_wsp into @wsp_id
    while @@FETCH_STATUS=0
    begin
        select @tody = right(dbo.get_shift_date(getdate(),'0',@wsp_id),10) --根据当前时刻判定当前是哪天
        update x set x.oee_num=isnull(y.oee_num,0),x.oee_den=isnull(y.oee_den,0),x.cal_oee=isnull(y.cal_oee,0),
        x.oee_coef= case when isnull(y.foee,0)=0 then 1 else y.foee end,
        x.dis_oee = round(isnull(y.cal_oee,0)*case when isnull(y.foee,0)=0 then 1 else y.foee end,2),
        x.modify_time=getdate()
        from dbo.ky_mcp_oee x
        left join (
            select e.parent_location_id,b.location_id,sum(isnull(user_num9,0))as oee_num,sum(isnull(d.time_open,0)*isnull(d.num_hour,0)) as oee_den,
            case when sum(isnull(d.time_open,0)*isnull(d.num_hour,0))>0 then round( sum(isnull(user_num9,0))/sum(isnull(d.time_open,0)*isnull(d.num_hour,0)) * 100,2) else 0 end as cal_oee,max(d.foee) as foee
            from ky_hch_eqptline d with(nolock)
            join(select max(id) as id from ky_hch_eqptline with(nolock) group by eqpt_id) dd on d.id=d.id
            join bs_eqpt_sub c with(nolock) on d.eqpt_id= c.eqpt_id
            join bs_eqpt_all b with(nolock) on c.sub_eqpt_id = b.eqpt_id
            left join ky_hch_daynum a with(nolock) on c.sub_eqpt_id=a.eqpt_id and a.wo_code= @tody
            join bs_location e with(nolock) on b.location_id=e.location_id
            where  b.res1 = '1' and e.parent_location_id=@wsp_id
            group by e.parent_location_id, b.location_id
        )y on  x.wsp_id=y.parent_location_id and x.line_id=y.location_id
        where x.day_type = 'TODAY' and x.wsp_id=@wsp_id

        fetch next from ky_proc_mcp_oee_cursor_wsp into @wsp_id
    end
    close ky_proc_mcp_oee_cursor_wsp
    deallocate ky_proc_mcp_oee_cursor_wsp

[此贴子已经被作者于2023-10-5 22:22编辑过]

搜索更多相关主题的帖子: with join from sum and 
2023-10-05 22:20
laowan001
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:66
帖 子:1089
专家分:2682
注 册:2015-12-30
收藏
得分:5 
把这一大堆语句搞成一个文本,一次发给SQL喽
2023-10-06 08:34
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
收藏
得分:0 
回复 2楼 laowan001
转成文本后如何发给SQL执行呢
2023-10-06 10:01
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:火星
等 级:版主
威 望:37
帖 子:707
专家分:2833
注 册:2018-3-13
收藏
得分:5 
这段代码也没超过8K啊,直接发给后台执行出了什么问题,不支持只进游标?

这家伙很懒,啥也没留下
2023-10-06 11:28
laowan001
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:66
帖 子:1089
专家分:2682
注 册:2015-12-30
收藏
得分:0 
以下是引用laibinhua在2023-10-6 10:01:36的发言:

转成文本后如何发给SQL执行呢


执行一句SQL知道怎么发吧?把大段文本替换那一句话就是了
2023-10-06 11:36
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
收藏
得分:0 
回复 4楼 csyx
不知道如何包装这么一大段字符串,然后用什么方法运行,这样可以吗?(注:Handle1是数据库连接)
TEXT TO cSQLchar  TEXTMERGE NOSHOW PRETEXT 7
    --更新数据
    update a set a.oee_num=isnull(b.bd_num,0),a.oee_den=isnull(b.bd_total,0),
    a.cal_oee=round(case when isnull(b.bd_total,0)=0 then 0.00 else isnull(b.bd_num,0)/b.bd_total*100.0 end,2),
    a.oee_coef=isnull(b.bd_coef,1),
    a.dis_oee=round(case when isnull(b.bd_total,0)=0 then 0.00 else isnull(b.bd_num,0)/b.bd_total*100.0 end*isnull(b.bd_coef,1),2),
    a.modify_time=getdate()
    from ky_mcp.dbo.ky_mcp_oee a
    left join(
        select c.parent_location_id, b.location_id,b.date_code,b.data_type,b.bd_coef,sum(b.bd_num) as bd_num,sum(b.bd_total) as bd_total--,b.bd_cal_data,b.bd_coef,b.bd_dis_data
        from ky_bd_tmp_data b with(nolock)
        join bs_location c with(nolock) on b.location_id=c.location_id
        where b.bd_type='OEE'
        group by c.parent_location_id, b.location_id,b.date_code,b.data_type,b.bd_coef
    )b on a.wsp_id=b.parent_location_id and a.line_id=b.location_id and  a.day_type=b.date_code
   
    declare @wsp_id varchar(50),@tody varchar(10)
    declare ky_proc_mcp_oee_cursor_wsp cursor for
        select location_id
        from bs_location with(nolock)
        where parent_location_id='' and location_type_code='area' and is_delete=0
    open ky_proc_mcp_oee_cursor_wsp
    fetch next from ky_proc_mcp_oee_cursor_wsp into @wsp_id
    while @@FETCH_STATUS=0
    begin
        select @tody = right(dbo.get_shift_date(getdate(),'0',@wsp_id),10) --根据当前时刻判定当前是哪天
        update x set x.oee_num=isnull(y.oee_num,0),x.oee_den=isnull(y.oee_den,0),x.cal_oee=isnull(y.cal_oee,0),
        x.oee_coef= case when isnull(y.foee,0)=0 then 1 else y.foee end,
        x.dis_oee = round(isnull(y.cal_oee,0)*case when isnull(y.foee,0)=0 then 1 else y.foee end,2),
        x.modify_time=getdate()
        from dbo.ky_mcp_oee x
        left join (
            select e.parent_location_id,b.location_id,sum(isnull(user_num9,0))as oee_num,sum(isnull(d.time_open,0)*isnull(d.num_hour,0)) as oee_den,
            case when sum(isnull(d.time_open,0)*isnull(d.num_hour,0))>0 then round( sum(isnull(user_num9,0))/sum(isnull(d.time_open,0)*isnull(d.num_hour,0)) * 100,2) else 0 end as cal_oee,max(d.foee) as foee
            from ky_hch_eqptline d with(nolock)
            join(select max(id) as id from ky_hch_eqptline with(nolock) group by eqpt_id) dd on d.id=d.id
            join bs_eqpt_sub c with(nolock) on d.eqpt_id= c.eqpt_id
            join bs_eqpt_all b with(nolock) on c.sub_eqpt_id = b.eqpt_id
            left join ky_hch_daynum a with(nolock) on c.sub_eqpt_id=a.eqpt_id and a.wo_code= @tody
            join bs_location e with(nolock) on b.location_id=e.location_id
            where  b.res1 = '1' and e.parent_location_id=@wsp_id
            group by e.parent_location_id, b.location_id
        )y on  x.wsp_id=y.parent_location_id and x.line_id=y.location_id
        where x.day_type = 'TODAY' and x.wsp_id=@wsp_id

        fetch next from ky_proc_mcp_oee_cursor_wsp into @wsp_id
    end
    close ky_proc_mcp_oee_cursor_wsp
    deallocate ky_proc_mcp_oee_cursor_wsp
ENDTEXT

Sqlexec(Handle1,cSQLchar,'')
2023-10-06 11:42
csyx
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:火星
等 级:版主
威 望:37
帖 子:707
专家分:2833
注 册:2018-3-13
收藏
得分:0 
以下是引用laibinhua在2023-10-6 11:42:22的发言:
不知道如何包装这么一大段字符串,然后用什么方法运行,这样可以吗?(注:Handle1是数据库连接)
TEXT TO cSQLchar  TEXTMERGE NOSHOW PRETEXT 7
   。。。
ENDTEXT
Sqlexec(Handle1,cSQLchar,'')


正常就这样,返回什么错误消息了?

这家伙很懒,啥也没留下
2023-10-06 11:45
laibinhua
Rank: 2
等 级:论坛游民
威 望:1
帖 子:295
专家分:38
注 册:2010-12-23
收藏
得分:0 
回复 7楼 csyx
正确了,就用这个方法可以
2023-10-06 12:44
z12689
Rank: 2
等 级:论坛游民
帖 子:49
专家分:83
注 册:2021-4-15
收藏
得分:5 
2023-10-07 09:29
asdf_123000
Rank: 4
等 级:业余侠客
威 望:1
帖 子:273
专家分:227
注 册:2012-12-20
收藏
得分:5 
2023-10-09 15:32
快速回复:请教:批量SQL命令如何在VFP里执行(不用存储过程实现)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033643 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved