利用exec方法執行儲存過程示例

2021-06-28 21:35:41 字數 3522 閱讀 9705

create procedure [dbo].[proc_amountenergy_month_singlebuild]

-- add the parameters for the stored procedure here    

@p_buildid varchar(20)='100000a007',

@p_date varchar(20)='2008-01-01',

@p_lastperiodnum int=1  --資料持續時段數目    

asbegin

--分類分項**指標

declare energyitemcodecur cursor for select f_energyitemcode from t_dt_energyitemdict where f_energyitemtype_name ='分項能耗' and f_energyitemstate=1

--declare @buildid varchar(16)

declare @energyitemcode varchar(16)

declare @dbvalue decimal(18,2)

declare @startt varchar(20)

declare @endt varchar(20)

declare @i int

declare @sql varchar(2000)

set @sql=''

--set @i=0;

set @startt=left(@p_date,7) + '-01 00:00:00';

while @i<@p_lastperiodnum

begin

set @endt=convert(varchar(20),dateadd(day,-1,dateadd(month,1,@startt)),120)

open energyitemcodecur

fetch next from energyitemcodecur into @energyitemcode

while @@fetch_status = 0

begin

--先清理記錄

set @sql='delete from t_ec_energyitemmonthresult where f_buildid ='''+@p_buildid +''' and f_energyitemcode='''+@energyitemcode +''' and f_startmonth ='''+@startt+''' and f_endmonth ='''+@endt +''''

print '語句執行:'  + @sql

exec(@sql);

--將指定建築id、能耗分項的日資料統計出來

--insert into t_ec_energyitemmonthresult(f_monthresultid,f_buildid,f_energyitemcode,f_startmonth,f_endmonth,f_monthvalue,f_monthequvalue,f_daymaxvalue,f_daymaxequvalue,f_dayminvalue,f_dayminequvalue,f_state)  

--            select '',@p_buildid,@energyitemcode,@startt,@endt,sum(day_energy),sum(day_energy)*0.1229,max(day_energy) ,max(day_energy)*0.1229,min(day_energy) ,min(day_energy) *0.1229,1

--            from (select distinct     f_buildid ,    f_energyitemcode,

--                 cast(year(f_startday) as varchar(4)) + '年' + datename(mm, f_startday) + '月' as 年月,

--                 cast(f_dayvalue as decimal(18, 2)) as day_energy from  t_ec_energyitemdayresult

--             where f_startday >=@startt and f_endday<= @endt and f_buildid=@p_buildid and f_energyitemcode = @energyitemcode ) a group by f_buildid,f_energyitemcode,年月

set @sql =' insert into t_ec_energyitemmonthresult(f_monthresultid,f_buildid,f_energyitemcode,f_startmonth,f_endmonth,f_monthvalue,f_monthequvalue,f_daymaxvalue,f_daymaxequvalue,f_dayminvalue,f_dayminequvalue,f_state) '

set @sql=@sql+' select '''','''+@p_buildid+''','''+@energyitemcode+''','''+@startt+''','''+@endt+''',sum(day_energy),sum(day_energy)*0.1229,max(day_energy) ,max(day_energy)*0.1229,min(day_energy) ,min(day_energy) *0.1229,1 '

set @sql=@sql+' from (select distinct     f_buildid ,    f_energyitemcode,

cast(year(f_startday) as varchar(4)) + ''年'' + datename(mm, f_startday) + ''月'' + datename(dd, f_startday) + ''日'' as 年月日, '

set @sql=@sql+' cast(f_dayvalue as decimal(18, 2)) as day_energy from  t_ec_energyitemdayresult '

set @sql=@sql+ '                 where f_startday >='''+@startt+''' and f_endday<= '''+@endt+''' and f_buildid='''+@p_buildid+''' and f_energyitemcode = '''+@energyitemcode+''' ) a group by f_buildid,f_energyitemcode,年月日'

exec (@sql)

print @sql

if @@error  <> 0

begin

print '語句執行錯誤:

' + @sql

endfetch next from energyitemcodecur into @energyitemcode

endclose energyitemcodecur

set @i=@i+1

enddeallocate energyitemcodecur

endgo

hibernate 執行儲存過程 方法

private sessionfactory sessionfactory public void setsessionfactory sessionfactory sessionfactory 通過sql執行無返回結果的儲存過程 僅限於儲存過程 param querystring param pa...

C 執行儲存過程 方法

功能 根據呼叫的方法名稱 反射動態呼叫 sql command 的方法 現定義乙個類用來儲存儲存過程引數 儲存過程的屬性 procname 儲存過程的名稱 methodname 執行sqlcommand 方法的名稱 prmlist 儲存過程的引數 public class exeproc 根據制定的...

利用JOB定時執行儲存過程

一 首先還是得有個儲存過程,假如名稱是myproc。二 建立job,在 sql行 下 variable job no number begin dbms job.submit job no,myproc sysdate,sysdate 1 1440 commit end 其中job no是該作業的唯...