注册 登录
编程论坛 VFP论坛

请教:批量SQL命令如何在VFP里执行(不用存储过程实现)

laibinhua 发布于 2023-10-05 22:20, 632 次点击
因为出于保密计算方法的原因不想在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编辑过]

9 回复
#2
laowan0012023-10-06 08:34
把这一大堆语句搞成一个文本,一次发给SQL喽
#3
laibinhua2023-10-06 10:01
回复 2楼 laowan001
转成文本后如何发给SQL执行呢
#4
csyx2023-10-06 11:28
这段代码也没超过8K啊,直接发给后台执行出了什么问题,不支持只进游标?
#5
laowan0012023-10-06 11:36
以下是引用laibinhua在2023-10-6 10:01:36的发言:

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


执行一句SQL知道怎么发吧?把大段文本替换那一句话就是了
#6
laibinhua2023-10-06 11:42
回复 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,'')
#7
csyx2023-10-06 11:45
以下是引用laibinhua在2023-10-6 11:42:22的发言:
不知道如何包装这么一大段字符串,然后用什么方法运行,这样可以吗?(注:Handle1是数据库连接)
TEXT TO cSQLchar  TEXTMERGE NOSHOW PRETEXT 7
   。。。
ENDTEXT
Sqlexec(Handle1,cSQLchar,'')


正常就这样,返回什么错误消息了?
#8
laibinhua2023-10-06 12:44
回复 7楼 csyx
正确了,就用这个方法可以
#9
z126892023-10-07 09:29
#10
asdf_1230002023-10-09 15:32
1