我是存储过程的初学者。我在IF...End If
条件下尝试了以下存储过程,以便如何制作...我很困惑....所以任何人都创建它
strSql = "SELECT count(*) " & _
" FROM hist_billgen_report r, hist_billgen_header h " & _
" WHERE r.invoice_number=h.invoice_number " & _
" and h.macnum = '" & l_macnum & "' " & _
" and r.rep_type = 1 " & _
" and r.rep_call_type = '" & line_type & "' " & _
" and h.billing_job_id = '" & arg_job & "' "
'Special code for data lines for Newcore
If gcompany = "NCW" Then
strSql += " and r.rep_number not in ( "
strSql += "select distinct a.mdn from order_wireless a where"
strSql += " a.id in"
strSql += " ("
strSql += " select c.serviceid from cust_charge_file b, service_charges c, main_company_utilities d"
strSql += " where(b.chg_main_index = c.chargeid)"
strSql += " and c.serviceid = a.id"
strSql += " and (b.chg_main_category_id = d.utilities_id and d.utilities_type = 'CS' and (utilities_desc_short like '%FDS1%' or utilities_desc_short like '%FDS2%' or utilities_desc_short like '%FDS3%' or utilities_desc_short like '%MBS1%' or utilities_desc_short like '%MBS2%' or utilities_desc_short like '%MBS3%' ) )"
strSql += " )"
strSql += " and a.accountnumber = '" & l_macnum & "' "
strSql += " )"
End If
答案 0 :(得分:0)
将查询放入存储过程并不一定意味着性能提升。根据表中的数据,由于其中包含OR LIKES '%%'
,此查询可能会非常慢,但您可以执行以下操作:
create procedure [dbo].[spname]
@l_macNum int -- note, you haven't given a lot of information, create all query parameters with appropriate types here
-- more parameters
@arg_job int -- same
AS
BEGIN
if (@company = 'NCW')
begin
SELECT count(*)
FROM hist_billgen_report r, hist_billgen_header h
WHERE r.invoice_number=h.invoice_number
and h.macnum = @l_macNum
-- etc
and r.rep_number not in (
-- etc
)
end
else
begin
SELECT count(*)
FROM hist_billgen_report r, hist_billgen_header h
WHERE r.invoice_number=h.invoice_number
and h.macnum = @l_macNum
-- etc
end
END
GO
请注意,If和else逻辑是完全独立的查询,因为您不能在不使用动态sql的情况下执行我认为您希望在连续查询中执行的操作。 对此有一些警告,但鉴于你是sql的新手,要坚持
我使用-- etc
作为文字的占位符,因为我不打算提供整个解决方案:P
如果这没有意义,请告诉我。