Hi,
I'm having trouble creating/running an SP that accepts two string parameters that will be used in a where in clause. Below is how I'm going about it and the error I'm getting. Would like to resolve the error but if someone has a better way to do this I'm all for it! (I try to avoid string queries)
----- SP -------
DECLARE @strSQL as varchar(1000), @a varchar (400), @b varchar (400)
set @a =@ComCd
set @a=replace (@a, ',' , ''',''')
set @b =@group
set @b=replace (@b, ',' , ''',''')
select @strSQL = "SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( '"+@a+ "' ) and PO_Super_Cat in ('" +@b+ "')"
--print (@strSQL)
execute @strSQL
GO
----------- END SP ---------
How I'm executing it
exec Web_GetVendorCat 'elx,frg', 'cooking,LAUNDRY'
----------------------------
The error I'm getting:
-- PS IF I RUN THE FIRST LINE of code RETURN IN QUERY ANALYZER IT WORKS I'm not following the error "is not a valid identifier" something to do with the quotes???
SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( 'elx','frg' ) and PO_Super_Cat in ('cooking','LAUNDRY')
Server: Msg 203, Level 16, State 2, Procedure Web_GetVendorCat, Line 22
The name 'SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( 'elx','frg' ) and PO_Super_Cat in ('cooking','LAUNDRY')'
is not a valid identifier.
PS: -- IF I RUN THE FIRST PIECE RETURNED IN QUERY ANALYZER IT WORKS
SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( 'elx','frg' ) and PO_Super_Cat in ('cooking','LAUNDRY')
Thank You
CJ
I'm having trouble creating/running an SP that accepts two string parameters that will be used in a where in clause. Below is how I'm going about it and the error I'm getting. Would like to resolve the error but if someone has a better way to do this I'm all for it! (I try to avoid string queries)
----- SP -------
DECLARE @strSQL as varchar(1000), @a varchar (400), @b varchar (400)
set @a =@ComCd
set @a=replace (@a, ',' , ''',''')
set @b =@group
set @b=replace (@b, ',' , ''',''')
select @strSQL = "SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( '"+@a+ "' ) and PO_Super_Cat in ('" +@b+ "')"
--print (@strSQL)
execute @strSQL
GO
----------- END SP ---------
How I'm executing it
exec Web_GetVendorCat 'elx,frg', 'cooking,LAUNDRY'
----------------------------
The error I'm getting:
-- PS IF I RUN THE FIRST LINE of code RETURN IN QUERY ANALYZER IT WORKS I'm not following the error "is not a valid identifier" something to do with the quotes???
SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( 'elx','frg' ) and PO_Super_Cat in ('cooking','LAUNDRY')
Server: Msg 203, Level 16, State 2, Procedure Web_GetVendorCat, Line 22
The name 'SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( 'elx','frg' ) and PO_Super_Cat in ('cooking','LAUNDRY')'
is not a valid identifier.
PS: -- IF I RUN THE FIRST PIECE RETURNED IN QUERY ANALYZER IT WORKS
SELECT DISTINCT VW.Prod_Cat, VW.Prod_Cat_Desc FROM vw_IMITMIDX_GroupingFields as VW LEFT JOIN tblProductSet PS ON VW.prod_cat = PS.Prod_Cat Where VW.Prod_Cat < '990' and Commodity_Cd in ( 'elx','frg' ) and PO_Super_Cat in ('cooking','LAUNDRY')
Thank You
CJ