Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More efficent way to do the following...

Status
Not open for further replies.

jjlbmcnews

IS-IT--Management
Oct 4, 2001
60
GB
Hi,

I'm looking for a way to do the following in a more efficent way within a stored procedure. Basically the SP will return certain rows depending on the filters, it was all done originally as dynamic SQL but had to be reviewed due to performance. I've managed to rewrite most of the SP using the COALESCE function on the other filters but I think the COALESCE function is not going to help with the following -

IF @Time <> ''
IF Left(@Time,1) <> '='
SET @SQL = @SQL + ' AND (SUBSTRING(pd.vchTime,28,2) IN (''' + @Time + ''') OR SUBSTRING(pd.vchTime,40,2) IN (''' + @Time + ''') OR SUBSTRING(pd.vchTime,46,2) IN (''' + @Time + '''))'
ELSE
IF @Time = '=Morning'
SET @SQL = @SQL + 'AND (SUBSTRING(pd.vchTime,28,5) BETWEEN ''00:01'' AND ''10:59'')'
IF @Time = '=Lunchtime'
SET @SQL = @SQL + 'AND (SUBSTRING(pd.vchTime,40,5) BETWEEN ''11:00'' AND ''14:59'')'
IF @Time = '=Afternoon'
SET @SQL = @SQL + 'AND (SUBSTRING(pd.vchTime,46,5) BETWEEN ''15:00'' AND ''23:59'')'

IF @Sector <> ''
SET @SQL = @SQL + ' AND CASE WHEN l.vchSector IS NULL THEN c.vchSector ELSE l.vchSector END IN (''' + @Sector + ''')'
--ELSE
-- SET @SQL = @SQL + ' AND CASE WHEN l.vchSector IS NULL THEN c.vchSector ELSE l.vchSector END NOT LIKE ''VACHER DOD%'''

Can any one suggest a method to achive this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top