jjlbmcnews
IS-IT--Management
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?
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?