sql server 2008 - fill a Local #temptable in SQL with multiple Table variables -
i'm in sql server management studio 2008 , set multiple orderids run them through query. heard have use temptable i'm stuck @ how set multiple table variables. original query use:
declare @orderid int declare @status int -- ff nummer set @orderid = 134 -- huidige status set @status = 4 begin transaction update ff_task set tsk_fk_currentstatus = null (tsk_fk_order = @orderid) update ff_order set ord_fk_currentorderstatus = null (ord_pk = @orderid) delete ff_statuslog (stl_pk in (select top (1) ff_statuslog_1.stl_pk ff_statuslog ff_statuslog_1 inner join ff_task on ff_statuslog_1.stl_fk_task = ff_task.tsk_pk (ff_task.tsk_fk_order = @orderid) order ff_statuslog_1.stl_pk desc)) delete ff_orderstatuslog (osl_status = @status) , (osl_fk_order = @orderid) update ff_task set tsk_fk_currentstatus = (select top (1) ff_statuslog_1.stl_pk ff_statuslog ff_statuslog_1 inner join ff_task on ff_statuslog_1.stl_fk_task = ff_task.tsk_pk (ff_task.tsk_fk_order = @orderid) order ff_statuslog_1.stl_pk desc) (tsk_fk_order = @orderid) update ff_order set ord_fk_currentorderstatus = (select top 1 osl_status ff_orderstatuslog (osl_fk_order = @orderid) order osl_creationdatetime desc) (ord_pk = @orderid) commit transaction
thank you.
you can use table variable or temp table
also in sqlserver2008+ can passing table-valued parameters sps , udfs.a table-value parameter allows pass rows of data sps , udfs in tabular format. create table-valued parameter must first create table type defines table structure.
declare @tableofparemeters table(orderid int, status int) insert @tableofparemeters values(134, 4), (135, 5) begin transaction update ff_task set tsk_fk_currentstatus = null tsk_fk_order in (select orderid @tableofparemeters) update ff_order set ord_fk_currentorderstatus = null ord_pk in (select orderid @tableofparemeters) ;with cte ( select *, row_number() over(partition stl_pk order stl_pk desc) rn ff_statuslog stl inner join ff_task ff on stl.stl_fk_task = ff.tsk_pk ff.tsk_fk_order in (select orderid @tableofparemeters) ) delete cte rn = 1 delete ff_orderstatuslog exists ( select 1 @tableofparemeters t osl_status = t.status , osl_fk_order = t.orderid ) ;with cte ( select stl.stl_pk, ff.tsk_fk_currentstatus, row_number() over(partition stl_pk order stl_pk desc) rn ff_statuslog stl inner join ff_task ff on stl.stl_fk_task = ff.tsk_pk ff.tsk_fk_order in (select orderid @tableofparemeters) ) update cte set tsk_fk_currentstatus = stl_pk rn = 1 ;with cte ( select o.ord_fk_currentorderstatus, l.osl_status, row_number() over(partition osl_status order osl_creationdatetime desc) rn ff_order o inner join ff_orderstatuslog l on o.ord_pk = l.osl_fk_order l.osl_fk_order in (select orderid @tableofparemeters) ) update cte set tsk_fk_currentstatus = osl_status rn = 1 commit transaction
Comments
Post a Comment