How do I pass a parameter into the Query.CommandText in SQL Server Reporting Services? - oracle

I have the following query against an Oracle database:
select * from (
select Person.pId, Person.lastName as patLast, Person.firstName as patFirst
, Person.dateOfBirth
, Medicate.mId, Medicate.startDate as medStart, Medicate.description
, cast(substr(Medicate.instructions, 1, 50) as char(50)) as instruct
, ml.convert_id_to_date(Prescrib.pubTime) as scripSigned
, max(ml.convert_id_to_date(Prescrib.pubTime)) over (partition by Prescrib.pId, Prescrib.mId) as LastScrip
, UsrInfo.pvId, UsrInfo.lastName as provLast, UsrInfo.firstName as provFirst
from ml.Medicate
join ml.Prescrib on Medicate.mId = Prescrib.mId
join ml.UsrInfo on Prescrib.pvId = UsrInfo.pvId
join ml.Person on Medicate.pId = Person.pId
where Person.isPatient = 'Y'
and Person.pStatus = 'A'
and Medicate.xId = 1.e+035
and Medicate.change = 2
and Medicate.stopDate > sysdate
and REGEXP_LIKE(Medicate.instructions
, ' [qtb]\.?[oi]?\.?[dw][^ayieo]'
|| '|[^a-z]mg?s'
|| '|ij'
|| '|[^a-z]iu[^a-z]'
|| '|[0-9 ]u '
|| '|[^tu]hs'
, 'i')
order by ScripSigned desc
) where scripSigned = lastScrip and scripSigned > date '2011-01-01'
I have a Report Parameter defined, DateBegin, defined as a DateTime, and I've associated it with a Query Parameter also called DateBegin. I just can't figure out how to replace "date '2011-01-01'" with "DateBegin" so that the blinkin' thing actually works. Thanks!

Use the Oracle format for parameters - so use the following:
) where scripSigned = lastScrip and scripSigned > :DateBegin
(The # sign is used in SQLServer to identify SQLServer variables.)

Related

Ways to optimize DB2 Query

I have used the below SQL statement to create a DB2 view. Can someone help me on how to improve this view performance as it runs slow sometimes.
The one thing I noted is that it runs slow the first time but then for subsequent runs, it picks up speed. I have analyzed with the visual explain but when it runs slow, I don't see any index advises. So not really sure how to optimize this.
-- Generate SQL
-- Version: V7R4M0 190621
-- Generated on: 02/09/21 03:08:31
-- Relational Database: BIGBLUE
-- Standards Option: Db2 for i
CREATE VIEW IESQAFILE.PSCMPORDVW
AS
WITH INPROGRESS AS
(
SELECT
DIODR#
, DIDISP
, DIUNIT
, DISTST
, DIAPRV
, DIETAD
, DITRLR AS TRAILER_ID
, DIDR1
, DIETAT
FROM
IESQAFILE.LOAD
WHERE
DIETAD <> 0
AND DIETAT <> '0000'
ORDER BY
1
)
, STOPGROUP AS
(
SELECT
SOORD STOPORDER
, COUNT(*) STOPSREMAIN
, MIN(SOSTP#) NEXTSTOP
, MAX(SOAPPR) APPTREQ
FROM
PAVT.STOP
INNER JOIN
INPROGRESS
ON
DIODR# = SOORD
WHERE
SOARDT = 0
GROUP BY
SOORD
ORDER BY
1
)
, STOPAPPTS AS
(
SELECT
SOORD APPTORDER
, SOCUST STOPCUST
, SOEDA ETADATE
, SOETA ETATIME
, SOADT1 EARLYDATE
, SOATM1 EARLYTIME
, SOADT2 LATEDATE
, SOATM2 LATETIME
, SOCTYC NEXTCITY
, SOSTP# APPTSTOP
, SOST NEXTSTATE
FROM
IESQAFILE.STOPOFF
INNER JOIN
STOPGROUP
ON
STOPORDER = SOORD
AND NEXTSTOP = SOSTP#
)
SELECT
ORDER_NUMBER
, SHIPPER_ID
, SHIPPER_NAME
, SHIPPER_ADDRESS_1
, SHIPPER_ADDRESS_2
, SHIPPER_CITY
, SHIPPER_ST
, SHIPPER_ZIP
, SHIPPER_ZIP_EXT
, LOAD_AT_ID
, LOAD_AT_NAME
, LOAD_AT_ADDRESS_1
, LOAD_AT_ADDRESS_2
, LOAD_AT_CITY
, LOAD_AT_ST
, LOAD_AT_ZIP
, LOAD_AT_ZIP_EXT
, LOAD_AT_LATITUDE
, LOAD_AT_LONGITUDE
, EARLY_PU_DATE_TIME
, LATE_PU_DATE_TIME
, EARLY_DELV_DATE_TIME
, EST_REVENUE
, ORDER_DIV
, CONSIGNEE_ID
, CONSIGNEE_NAME
, CONSIGNEE_ADDRESS_1
, CONSIGNEE_ADDRESS_2
, CONSIGNEE_CITY
, CONSIGNEE_ST
, CONSIGNEE_ZIP
, CONSIGNEE_ZIP_EXT
, CONSIGNEE_LATITUDE
, CONSIGNEE_LONGITUDE
, TRAILER_TYPE
, ORDER_MESSAGE
, ADDITIONAL_STOPS
, CMDTY_CODE
, CMDTY_DESCRIPTION
, ORDER_MILES
, ORDER_WGT
, ORIGIN_CITY_CODE
, ORIGIN_CITY
, ORIGIN_ST
, DEST_CITY_CODE
, DEST_CITY_NAME
, DEST_ST
, PICK_UP_AREA
, PLAN_INFO
, NUMBER_LDS
, NUMBER_DISP
, SHIP_DATE_TIME
, NEW_PICKUP_AREA
, EQUIPMENT_NUMBER
, APPT_REQ
, APPT_MADE
, PRE_T_SEQ
, PRE_T_AREA
, LOAD_DISPATCHED
, CUST_SERV_REP
, NEGOTIATIONS
,
(
CASE
WHEN UNUNIT IS NOT NULL
THEN UNUNIT
ELSE ' '
END
)
UNIT_DISPATCHED
,
(
CASE
WHEN UNSUPR IS NOT NULL
THEN UNSUPR
ELSE ' '
END
)
DRIVER_MGR_CODE
, COALESCE(SUPNAM, ' ') DRIVER_MGR_NAME
,
(
CASE
WHEN UNFMGR IS NOT NULL
THEN UNFMGR
ELSE ' '
END
)
FLEET_MGR_CODE
, COALESCE(FLTNAM,' ') FLEET_MGR_NAME
,
(
CASE
WHEN UNTRL1 IS NOT NULL
THEN UNTRL1
ELSE ' '
END
)
TRAILER_ID,
DIDISP DISPATCH_NUMBER,
(COALESCE(BCMCNEW, ' ')) FED_MC_ID,
DIUNIT DISPATCHED_UNIT,
CASE
WHEN UNETAD <> 0
AND UNETAT = ''
THEN SMIS.CVTDATETIM(CHAR(UNETAD),'0000', (
SELECT
SUBSTR(DATA_AREA_VALUE, 1109, 2) AS TIMEZONE
FROM
TABLE(QSYS2.DATA_AREA_INFO('COMPAN', '*LIBL'))
)
)
WHEN UNETAD <> 0
THEN SMIS.CVTDATETIM(CHAR(UNETAD),UNETAT, (
SELECT
SUBSTR(DATA_AREA_VALUE, 1109, 2) AS TIMEZONE
FROM
TABLE(QSYS2.DATA_AREA_INFO('COMPAN', '*LIBL'))
)
)
WHEN UNETAD = 0
THEN '0000-00-00T00:00:00-00:00'
END AS ETA_DATE_TIME,
NEXTSTOP , CASE
WHEN SOARDT <> 0
AND SOARTM = ''
THEN SMIS.CVTDATETIM(CHAR(SOARDT),'0000', (
SELECT
SUBSTR(DATA_AREA_VALUE, 1109, 2) AS TIMEZONE
FROM
TABLE(QSYS2.DATA_AREA_INFO('COMPAN', '*LIBL'))
)
)
WHEN SOARDT <> 0
THEN SMIS.CVTDATETIM(CHAR(SOARDT),SOARTM, (
SELECT
SUBSTR(DATA_AREA_VALUE, 1109, 2) AS TIMEZONE
FROM
TABLE(QSYS2.DATA_AREA_INFO('COMPAN', '*LIBL'))
)
)
WHEN SOARDT = 0
THEN '0000-00-00T00:00:00-00:00'
END AS STOP_ARRIVAL_DATE_TIME
, CASE
WHEN SOLUDT <> 0
AND SOLUTM = ''
THEN SMIS.CVTDATETIM(CHAR(SOLUDT),'0000', (
SELECT
SUBSTR(DATA_AREA_VALUE, 1109, 2) AS TIMEZONE
FROM
TABLE(QSYS2.DATA_AREA_INFO('COMPAN', '*LIBL'))
)
)
WHEN SOLUDT <> 0
THEN SMIS.CVTDATETIM(CHAR(SOLUDT),SOLUTM, (
SELECT
SUBSTR(DATA_AREA_VALUE, 1109, 2) AS TIMEZONE
FROM
TABLE(QSYS2.DATA_AREA_INFO('COMPAN', '*LIBL'))
)
)
WHEN SOLUDT = 0
THEN '0000-00-00T00:00:00-00:00'
END AS STOP_DEPART_DATE_TIME
, ORBAMT ORDER_INV_AMT
, ORARST AR_STATUS_FLAG
, DISTST SETTLEMENT_FLAG
, DIAPRV APPROVED_FOR_PAY
, BCCARR CARRIER_CODE
, BCNAME CARRIER_NAME
, BCADDR CARRIER_ADDRESS_1
, BCADR2 CARRIER_ADDRESS_2
, BCCITY CARRIER_CITY
, BCST CARRIER_ST
, BCZIP CARRIER_ZIP
FROM
INPROGRESS
INNER JOIN
IESQAFILE.PSMAINORVW A
ON
DIODR# = ORDER_NUMBER
AND DIDISP = NUMBER_DISP
AND
(
SUBSTR(ORDER_NUMBER, 1, 2) <> 'DH'
AND SUBSTR(ORDER_NUMBER, 1, 1) <> 'M'
)
LEFT OUTER JOIN
IESQAFILE.STOPOFF
ON
DIODR# = SOORD
AND SOSTP# = 90
LEFT OUTER JOIN
IESQAFILE.LMCARR
ON
DIUNIT = BCCARR
LEFT OUTER JOIN
IESQAFILE.MMILES
ON
MMORD# = DIODR#
AND MMRECTYPE = 'D'
AND MMDSP# = DIDISP
EXCEPTION JOIN
IESQAFILE.ORDBILL B
ON
B.ORODR# = DIODR#
AND B.ORSEQ = ' '
AND ORARST = '1'
LEFT OUTER JOIN
STOPGROUP
ON
STOPORDER = DIODR#
LEFT OUTER JOIN
STOPAPPTS
ON
APPTORDER = STOPORDER
AND APPTSTOP = NEXTSTOP
LEFT OUTER JOIN
IESQAFILE.UNITS
ON
UNUNIT = DIUNIT
AND UNORD# = ORDER_NUMBER
LEFT OUTER JOIN
IESQAFILE.SUPMAST
ON
SUPCDE = UNSUPR
LEFT OUTER JOIN
IESQAFILE.FLTMAST
ON
UNFMGR = FLTCDE
WHERE
DIETAD <> 0
AND DIETAT <> '0000'
RCDFMT PSCMPORDVW ;
Without seeing the Visual Explain (VE) data (and it's not really something easily sharable here) giving you a "magic wand" answer not likely.
I'm surprised you don't see any indexes advised. But even so, you should be able to compare the VE data between "fast" and "slow" runs to see where the differences are. Make sure you have "View"-->"Highlighting"-->"Expensive Icons"-->... turned on. Also check "Options"-->"Attribute Detail" and "Options"-->"Graph Detail"...
Always a good idea to be using the latest and greatest version (1.1.8.6) of Access Client Solutions (ACS) which includes the Run SQL Script & VE components.
A couple of things jump out, an Encoded Vector Index (EVI) that include aggregates(count,min,max) might help here...
select soord stoporder
,count(*) stopsremain
,min(sostp#) nextstop
,max(soappr) apptreq
from pavt.stop
And index (or two) with derived columns substr(order_number,....) might help here:
from inprogress
inner join iesqafile.psmainorvw a
on diodr# = order_number
and didisp = number_disp
and (substr(order_number, 1, 2) <> 'DH'
and substr(order_number, 1, 1) <> 'M')
Again VE is your friend, you'll have to dig into what's going on and where the time is being spent.
Break the various parts of your statement down and try to optimize individual components when possible. Hopefully, the gains will remain when you put it back together.
One thing to remember, a view is performance neutral. It neither helps nor hurts performance.
You don't give an example of how you access the view, but have you considered making use of user defined table functions (UDTF)? While the query engine attempts to push down selection criteria, a UDTF's parameters make it easy for you to explicitly include selection criteria. You might even benefit from encapsulating the statement in a UDTF and building the view over the UDTF. You can see an example (via Generate SQL Source) for the message_queue_info view & UDTF in QSYS2 that IBM provided.

Error Calling DB2 Stored Procedure

I defined a stored procedure with input and output parameters and am getting the following error when I try to call the stored procedure.
[SQL0312] Variable KUNNR not defined or not usable.
Here is the call:
CALL R3QA6DATA.SP_ADDRESS_CHANGES ('1999-12-31 23:59:59', '2016-06-01 23:59:59', :KUNNR,:KUNN2,:NAME1,:NAME2,:STRAS,:ORT01,:REGIO,:PSTLZ,:LAND1,:TELF1,:TELFX,:DEFPA)
GO
Here is the stored procedure:
CREATE PROCEDURE R3QA6DATA.SP_ADDRESS_CHANGES
(IN STARTDATE TIMESTAMP, IN ENDDATE TIMESTAMP,
OUT KUNNR GRAPHIC(10), OUT KUNN2 GRAPHIC(10), OUT NAME1 GRAPHIC(35), OUT NAME2 GRAPHIC(35), OUT STRAS GRAPHIC(35), OUT ORT01 GRAPHIC(35),
OUT REGIO GRAPHIC(3), OUT PSTLZ GRAPHIC(10), OUT LAND1 GRAPHIC(3), OUT TELF1 GRAPHIC(16), OUT TELFX GRAPHIC(31), OUT DEFPA GRAPHIC(1) )
LANGUAGE SQL
BEGIN
SELECT DISTINCT
knvp.kunnr
, knvp.kunn2
, kna1.name1
, kna1.name2
, kna1.stras
, kna1.ort01
, kna1.regio
, kna1.pstlz
, kna1.land1
, kna1.telf1
, kna1.telfx
, knvp.defpa
INTO KUNNR, KUNN2, NAME1, NAME2, STRAS, ORT01, REGIO, PSTLZ, LAND1, TELF1, TELFX, DEFPA
FROM
R3QA6DATA.KNA1 AS kna1
INNER JOIN
R3QA6DATA.ZMBCM AS zmbcm
ON
kna1.KUNNR = zmbcm.KUNAG
INNER JOIN
R3QA6DATA.KNVV AS knvv
ON
( kna1.KUNNR = knvv.KUNNR )
INNER JOIN
R3QA6DATA.KNVP AS knvp
ON
(
knvv.KUNNR = knvp.KUNNR
AND
knvv.VKORG = knvp.VKORG
AND
knvv.VTWEG = knvp.VTWEG
AND
knvv.SPART = knvp.SPART
)
WHERE
kna1.MANDT = '010'
AND
knvp.PARVW IN ('WE', 'AG')
AND
(
knvv.VKORG = zmbcm.VKORG
AND
knvv.VTWEG = zmbcm.VTWEG
AND
knvv.SPART = zmbcm.SPART
)
AND
kna1.loevm = ' '
AND
knvv.loevm = ' '
AND
knvp.KUNN2 IN
(
SELECT
SUBSTRING(bdcp2.tabkey, 4, 10)
FROM
R3QA6DATA.BDCP2 AS bdcp2
WHERE
bdcp2.mestype = 'DEBMAS'
AND
(
( bdcp2.tabname = 'KNA1'
AND
bdcp2.fldname IN
('NAME1'
, 'NAME2'
, 'STRAS'
, 'ORT01'
, 'REGIO'
, 'LAND1'
, 'PSTLZ'
, 'TELF1'
, 'TELFX') )
OR
( bdcp2.tabname = 'KNVP' )
)
AND
(
bdcp2.cretime > STARTDATE
AND
bdcp2.cretime < ENDDATE
)
)
AND
zmbcm.STOREID = 4 ;
END
Go
In RPGLE or COBOL this issue usually happens if you have the variables defined in an external source and accessed through COPY statement. If this is the case in your problem then try using the source through INCLUDE.

Re-writing Query

Overall Task :- I need to retrieve data from 45 fields in system A and dump that data into a temp table which is then picked up by a unix process which produces an xml data file to be imported into system B.
Specific Question : What would be the best way of retrieving the data to be written into the 45 fields. Majority of the data is independent and can't be retrieved using a single statement. The way i currently retrieve this data is as follows (example below)
My temp tables hold the affected properties ID that i need to extract data for. i.e PROP_LIST_TEMP and ASSOC_PROP_TEMP.
SELECT SUBSTR (pro.pro_propref, 1, 25) UPRN,
(SELECT SUBSTR (adr_building, 1, 100)
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
AND aus_pro_refno = pro.pro_refno)
BUILDING_NAME,
(SELECT CASE
WHEN (adr_street_number like 'BLOC%'
OR adr_street_number like '%-%'
OR adr_street_number like '%/%')
THEN NULL
ELSE regexp_replace (adr_street_number, '[^[:digit:]]+')
END
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
AND aus_pro_refno = pro.pro_refno)
STREET_NUMBER,
(SELECT CASE
WHEN (adr_street_number like 'BLOC%'
OR adr_street_number like '%-%'
OR adr_street_number like '%/%')
THEN SUBSTR (adr_street_number, 1, 20)
ELSE REGEXP_REPLACE (adr_street_number, '[^[:alpha:]]+', '')
END
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)
AND aus_pro_refno = pro.pro_refno)
STREET_NUMBER_SUFFIX,
(SELECT SUBSTR (ptv_pty_code, 1, 3)
FROM prop_type_values
WHERE ptv_refno = pro.pro_hou_ptv_refno)
HOUSE_TYPE
FROM properties pro
WHERE pro_refno IN (select * from PIMSS_PROP_LIST_TEMP
UNION
select * from PIMSS_ASSOC_PROP_TEMP)
AND pro.pro_hou_hrv_hot_code IN
(SELECT frv_code
FROM first_ref_values
WHERE frv_frd_domain IN ('ASS_OWN_REF')
AND frv_current_ind = 'Y');
Since the where clauses of the subqueries in the select statement are identical, you could simply pull that out into the where clause, like so:
SELECT SUBSTR (pro.pro_propref, 1, 25) UPRN,
SUBSTR (addr.adr_building, 1, 100) BUILDING_NAME,
CASE WHEN (addr.adr_street_number like 'BLOC%'
OR addr.adr_street_number like '%-%'
OR addr.adr_street_number like '%/%')
THEN NULL
ELSE regexp_replace (addr.adr_street_number, '[^[:digit:]]+')
END STREET_NUMBER,
CASE WHEN (addr.adr_street_number like 'BLOC%'
OR addr.adr_street_number like '%-%'
OR addr.adr_street_number like '%/%')
THEN SUBSTR (addr.adr_street_number, 1, 20)
ELSE REGEXP_REPLACE (addr.adr_street_number, '[^[:alpha:]]+', '')
END STREET_NUMBER_SUFFIX,
(SELECT SUBSTR (ptv_pty_code, 1, 3)
FROM prop_type_values
WHERE ptv_refno = pro.pro_hou_ptv_refno) HOUSE_TYPE
FROM properties pro,
(select adr_building,
adr_street_number
FROM addresses, address_usages
WHERE aus_adr_refno = adr_refno
AND aus_aut_far_code = 'PHYSICAL'
AND aus_aut_fao_code = 'PRO'
AND (aus_end_date IS NULL OR aus_end_date > SYSDATE)) addr
WHERE pro.pro_refno = aus_pro_refno
and pro_refno IN (select * from PIMSS_PROP_LIST_TEMP
UNION
select * from PIMSS_ASSOC_PROP_TEMP)
AND pro.pro_hou_hrv_hot_code IN (SELECT frv_code
FROM first_ref_values
WHERE frv_frd_domain IN ('ASS_OWN_REF')
AND frv_current_ind = 'Y');
You might possibly need an outer join if there's a chance that no rows could be returned from the addr subquery.

ORA-30928: "Connect by filtering phase runs out of temp tablespace"

i have created a query that is sued to display a data in a label. This particular query will then be stored into a program that we use. The query runs just fine until this morning when it returns the error ORA-30928: "Connect by filtering phase runs out of temp tablespace". I have Googled and found out that I can do any of the following:
Include a NO FILTERING hint - but did not work properly
Increase the temp tablespace - not applicable to me since this runs in a production server that I don't have any access to.
Are there other ways to fix this? By the way, below is the query that I use.
SELECT * FROM(
SELECT
gn.wipdatavalue
, gn.containername
, gn.l
, gn.q
, gn.d
, gn.l2
, gn.q2
, gn.d2
, gn.l3
, gn.q3
, gn.d3
, gn.old
, gn.qtyperbox
, gn.productname
, gn.slot
, gn.dt
, gn.ws_green
, gn.ws_pnr
, gn.ws_pcn
, intn.mkt_number dsn
, gn.low_number
, gn.high_number
, gn.msl
, gn.baketime
, gn.exptime
, NVL(gn.q, 0) + NVL(gn.q2, 0) + NVL(gn.q3, 0) AS qtybox
, row_number () over (partition by slot order by low_number) as n
FROM
(
SELECT
tr.*
, TO_NUMBER(SUBSTR(wipdatavalue, 1, INSTR (wipdatavalue || '-', '-') - 1)) AS low_number
, TO_NUMBER(SUBSTR(wipdatavalue, 1 + INSTR ( wipdatavalue, '-'))) AS high_number
, pm.msllevel MSL
, pm.baketime BAKETIME
, pm.expstime EXPTIME
FROM trprinting tr
JOIN CONTAINER c ON tr.containername = c.containername
JOIN a_lotattributes ala ON c.containerid = ala.containerid
JOIN product p ON c.productid = p.productid
LEFT JOIN otherdb.pkg_main pm ON trim(p.brandname) = trim(pm.pcode)
WHERE (c.containername = :lot OR tr.SLOT= :lot)
)gn
LEFT JOIN otherdb.intnr intn ON TRIM(gn.productname) = TRIM(intn.part_number)
connect by level <= HIGH_NUMBER + 1 - LOW_NUMBER and LOW_NUMBER = prior LOW_NUMBER and prior SYS_GUID() is not null
ORDER BY low_number,n
)
WHERE n LIKE :n AND wipdatavalue LIKE :wip AND ROWNUM <= 300 AND wipdatavalue NOT LIKE 0
I am using Oracle 11g too.
Thanks for the help everyone.

How to return all rows if IN clause has no value?

Following is sample query.
CREATE PROCEDURE GetModel
(
#brandids varchar(100), -- brandid="1,2,3"
#bodystyleid varchar(100) -- bodystyleid="1,2,3"
)
AS
select * from model
where brandid in (#brandids) -- use a UDF to return table for comma delimited string
and bodystyleid in (#bodystyleid)
My requirement is that if #brandids or #bodystyleid is blank, query should return all rows for that condition.
Please guide me how to do this? Also suggest how to write this query to optimize performance.
You'll need dynamic SQL or a split function for this anyway, since IN ('1,2,3') is not the same as IN (1,2,3).
Split function:
CREATE FUNCTION dbo.SplitInts
(
#List VARCHAR(MAX),
#Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN ( SELECT Item = CONVERT(INT, Item) FROM (
SELECT Item = x.i.value('(./text())[1]', 'int') FROM (
SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(#List, #Delimiter, '</i><i>')
+ '</i>').query('.') ) AS a CROSS APPLY [XML].nodes('i') AS x(i)) AS y
WHERE Item IS NOT NULL
);
Code becomes something like:
SELECT m.col1, m.col2 FROM dbo.model AS m
LEFT OUTER JOIN dbo.SplitInts(NULLIF(#brandids, ''), ',') AS br
ON m.brandid = COALESCE(br.Item, m.brandid)
LEFT OUTER JOIN dbo.SplitInts(NULLIF(#bodystyleid, ''), ',') AS bs
ON m.bodystyleid = COALESCE(bs.Item, m.bodystyleid)
WHERE (NULLIF(#brandids, '') IS NULL OR br.Item IS NOT NULL)
AND (NULLIF(#bodystyleid, '') IS NULL OR bs.Item IS NOT NULL);
(Note that I added a lot of NULLIF handling here... if these parameters don't have a value, you should be passing NULL, not "blank".)
Dynamic SQL, which will have much less chance of leading to bad plans due to parameter sniffing, would be:
DECLARE #sql NVARCHAR(MAX);
SET #sql = N'SELECT columns FROM dbo.model
WHERE 1 = 1 '
+ COALESCE(' AND brandid IN (' + #brandids + ')', '')
+ COALESCE(' AND bodystyleid IN (' + #bodystyleid + ')', '');
EXEC sp_executesql #sql;
Of course as #JamieCee points out, dynamic SQL could be vulnerable to injection, as you'll discover if you search for dynamic SQL anywhere. So if you don't trust your input, you'll want to guard against potential injection attacks. Just like you would if you were assembling ad hoc SQL inside your application code.
When you move to SQL Server 2008 or better, you should look at table-valued parameters (example here).
if(#brandids = '' or #brandids is null)
Begin
Set #brandids = 'brandid'
End
if(#bodystyleid = '' or #bodystyleid is null)
Begin
Set #bodystyleid = 'bodystyleid'
End
Exec('select * from model where brandid in (' + #brandids + ')
and bodystyleid in (' + #bodystyleid + ')')

Resources