Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Scripting a stored procedure as a view


Scripting a stored procedure as a view

Author
Message
sKreetz!
sKreetz!
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 104
I have a stored procedure with 17 parameters. Not all 17 parameters are needed for the intended use so I have narrowed the scope of parameters down to 8. Ultimately I would like to remove all of the parameters. Once it is all said and done I can reference the view when needed and it will show the exact same data as the stored procedure. The code below is what I have so far. Any and all advice is appreciated.

DECLARE 
@UnitQtyPlaces DecimalPlacesType,
@UseEffD FlagNyType,
@RunBasis RunBasisType,
@ShowInternal FlagNyType = 0,
@ShowExternal FlagNyType = 1,
@PrintItemMaterials ListYesNoType = 1,
@EffectiveDate DateType = Null,
@DisplayReferenceFields ListYesNoType = Null

SELECT @UnitQtyPlaces = places_qty_per FROM invparms

SELECT @RunBasis = sfcparms.run_basis FROM sfcparms

SELECT
item.item,
item.description,
item.revision,
jobroute.oper_num,
jobroute.wc,
Case
When @RunBasis = jobroute.run_basis_lbr
Then ' '
Else jobroute.run_basis_lbr
End as run_basis_lbr_marker,
Case
When @RunBasis = jobroute.run_basis_mch
Then ' '
Else jobroute.run_basis_mch
End as run_basis_mch_marker,
jobroute.run_basis_lbr,
jobroute.run_basis_mch,
JobrouteNoteExists = dbo.ReportNotesExist('jobroute', jobroute.RowPointer, @ShowInternal,
@ShowExternal, jobroute.NoteExistsFlag),
jobroute.rowpointer as jobroute_rowpointer,
wc.description as wc_description,
jrt_sch.move_ticks / 100 as move_hours,
jrt_sch.queue_ticks / 100 as queue_hours,
jrt_sch.setup_ticks / 100 as setup_hours,
jrt_sch.sched_ticks / 100 as fix_sch_hours,
jrt_sch.sched_off / 100 as offset_hrs,
jrt_sch.pcs_per_lbr_hr,
jrt_sch.pcs_per_mch_hr,
jrt_sch.run_ticks_mch / 100 as mch_hr_per_pc,
jrt_sch.run_ticks_lbr / 100 as lbr_hr_per_pc,
jobroute.cntrl_point,
jobmatl.sequence as jobmatl_sequence,
jobmatl.matl_type,
jobmatl.item as jobmatl_item,
Case
When x_item.description Is Null
Then jobmatl.description
Else x_item.description
End as jobmatl_description,
x_item.revision as x_item_revision,
jobmatl.units,
jobmatl.matl_qty_conv,
jobmatl.u_m,
jobmatl.ref_type,
jobmatl.effect_date,
jobmatl.obs_date,
jobmatl.bom_seq,
JobmatlNoteExists = dbo.ReportNotesExist('jobmatl', jobmatl.RowPointer, @ShowInternal,
@ShowExternal, jobmatl.NoteExistsFlag),
jobmatl.rowpointer as jobmatl_rowpointer,
jobmatl.alt_group,
jobmatl.alt_group_rank,
job_ref.sequence as job_ref_sequence,
job_ref.ref_des,
job_ref.bubble,
job_ref.assy_seq,
@UnitQtyPlaces AS 'UnitQtyPlaces'

FROM item
INNER JOIN jobroute ON item.job = jobroute.job AND item.suffix = jobroute.suffix
Left Outer JOIN jobmatl ON @PrintItemMaterials = 1 and jobroute.job = jobmatl.job AND jobroute.suffix = jobmatl.suffix AND
jobroute.oper_num = jobmatl.oper_num And

Case When @UseEffD = 1 Then
Case
When jobmatl.effect_date is Null Then 1
When jobmatl.effect_date <= @EffectiveDate Then 1
Else 0
End
Else 1
End = 1 AND
Case When @UseEffD = 1 Then
Case
When jobmatl.obs_date Is Null Then 1
When jobmatl.obs_date > @EffectiveDate Then 1
Else 0
End
Else 1
End = 1
Left Outer Join jrt_sch on jobroute.job = jrt_sch.job and jobroute.suffix = jrt_sch.suffix and
jobroute.oper_num = jrt_sch.oper_num
Left Outer Join wc on jobroute.wc = wc.wc
Left Outer Join job_ref on @DisplayReferenceFields = 1 and @PrintItemMaterials = 1 and
jobmatl.job = job_ref.job and jobmatl.suffix = job_ref.suffix and
jobmatl.oper_num = job_ref.oper_num and jobmatl.sequence = job_ref.sequence
Left Outer Join item as x_item on @PrintItemMaterials = 1 and jobmatl.item = x_item.item

ORDER BY
item.item,
jobroute.oper_num,
jobmatl.alt_group,
jobmatl.alt_group_rank,
job_ref.ref_seq


wolfkillj
wolfkillj
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1104 Visits: 2582
You can't declare variables in a view, and of course views can't take parameters. You use variables in number of places:

-- as a column and in CASE expressions in the SELECT clause;
-- as parameters to functions called in the SELECT clause;
-- in the join conditions of almost every joined table or view.

What will you use for those values in a view?

If you need an object that can be used in the FROM clause of a select statement and also accepts parameters that can be used in the query, you could turn this code into an inline table-valued function.

As an aside, calling scalar functions in a SELECT clause can really hurt performance. You call the dbo.reportNotesExist function twice in your SELECT clause. That function will be called twice for every row that the query returns - if you get 1,000 rows, the function will have to run 2,000 times.

Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search