wiggy353
asked on
Using Parameters in SQL query
I am working on a stored procedure that someone else built and I need to add to it. It the section the was previously written, part of the code is this:
Obviously before this statement the SQL variable was declared and some text was put in it. Then this combines that with these parameters and then makes a new statement that gets executed.
The section that I am adding looks like this:
The problem is that the previous code works fine. The new code works fine but only if one parameter is passed to the @CMOPIDs. With the original code, I can send multiples separated by a comma and it works as an in clause would. But if I try to pass multiples to the new code it returns nothing. It doesn't error or anything, but it returns no results which is not correct.
Please help!
Select @SQL = @SQL + '' + @dropdownlist + ' = ''' + @control + ''' and CMOPID in (' + @CMOPIDs + ')
Order by CMOPID, ProductCode'
Obviously before this statement the SQL variable was declared and some text was put in it. Then this combines that with these parameters and then makes a new statement that gets executed.
The section that I am adding looks like this:
SELECT
DISTINCT
fm.[Cmopid]
,fm.[OnHandStock]
,fm.[ProductCode]
,fm.[DispensingPackSize]
,vp.HostDescription
,fm.[ProductEnabled]
FROM [vhacmncdb].[CDB].[dbo].[FormularyMaster] fm with (nolock)
join [vhacmncdb].[CDB].[info].[va_productlist] vp with (NOLOCK)
on
fm.productcode = vp.productcode
where
fm.productcode like '%'+@control+'%'
and
CMOPID in (@CMOPIDs)
The problem is that the previous code works fine. The new code works fine but only if one parameter is passed to the @CMOPIDs. With the original code, I can send multiples separated by a comma and it works as an in clause would. But if I try to pass multiples to the new code it returns nothing. It doesn't error or anything, but it returns no results which is not correct.
Please help!
ASKER
Thanks. I tried that at first but then had another problem. Later in the same query is this section:
and it kept breaking because of the literals (is that the right term) that I am selecting; like 'Unavailable'. Is there a way to get around that?
SELECT
distinct
cmopid
,'Outsource Vendor' as ndc
,qtyavail
,ProductCode
,DispensingPackSize as packsize
,vaprintname
,'Unavailable' as Manufacturer
,'Unavailable' as [Image]
FROM
@tmpFMResults tr
and it kept breaking because of the literals (is that the right term) that I am selecting; like 'Unavailable'. Is there a way to get around that?
There is nothing obviously wrong with that query. It would help to see the full query, and exactly how it is breaking with the inputs.
ASKER
Oh, I forgot to change it into the form for the dynamic SQL, so it would look like this:
You can now see where it would have problems with the 'Outsource Vendor' and 'Unavailable'.
@SQL = ' SELECT
distinct
cmopid
,'+'Outsource Vendor'+' as ndc
,qtyavail
,ProductCode
,DispensingPackSize as packsize
,vaprintname
,'+'Unavailable'+' as Manufacturer
,'+'Unavailable'+' as [Image]
FROM
@tmpFMResults tr'
You can now see where it would have problems with the 'Outsource Vendor' and 'Unavailable'.
When you are embedding string literals into dynamic SQL, you need to double up the quotes, or just add them as a constant.
@SQL = ' SELECT
distinct
cmopid
,''Outsource Vendor'' as ndc
,qtyavail
,ProductCode
,DispensingPackSize as packsize
,vaprintname
,''Unavailable'' as Manufacturer
,''Unavailable'' as [Image]
FROM
@tmpFMResults tr'
ASKER
OK, so that helps. Here is what I have now:
and I am getting this error:
Msg 102, Level 15, State 1, Line 42
Incorrect syntax near 'jo'.
set @SQL2 = '
DECLARE @tmpFinalResults TABLE
(
[cmopid] varchar(50)
,[ndc] varchar(50)
,[qtyavail] int
,[ProductCode] VARCHAR(50)
,[DispensingPackSize] real
,[vaprintname] varchar(50)
,[manufacturer] varchar(50)
,[image] varchar(50)
)
DECLARE @tmpFMResults TABLE
(
[cmopid] varchar(50)
,[qtyavail] int
,[ProductCode] VARCHAR(50)
,[DispensingPackSize] VARCHAR(50)
,[vaprintname] varchar(100)
,[ProductEnabled] INT
)
INSERT INTO @tmpFMResults
(
CmopID
,qtyavail
,ProductCode
,DispensingPackSize
,vaprintname
,ProductEnabled
)
SELECT
DISTINCT
fm.[Cmopid]
,fm.[OnHandStock]
,fm.[ProductCode]
,fm.[DispensingPackSize]
,vp.HostDescription
,fm.[ProductEnabled]
FROM [servername].[dbname].[dbo].[tablename] fm with (nolock)
join [servername].[dbname].[info].[table2name] vp with (NOLOCK)
on
fm.productcode = vp.productcode
where
fm.productcode like ''%'+@control+'%''
and
CMOPID in ('+@CMOPIDs+')
insert into @tmpFinalResults
(
cmopid
,ndc
,qtyavail
,productcode
,dispensingpacksize
,vaprintname
,manufacturer
,[image]
)
SELECT
distinct cmopid
, ndc
, qtyavail
, productcode
, packsize
, vaprintname
, Manufacturer
, NDC as "Image"
FROM
CMOPProductLine.Formulary
Where
productcode like ''%'+@control+'%''
and
CMOPID in ('+@CMOPIDs+')
insert into @tmpFinalResults
(
cmopid
,ndc
,qtyavail
,productcode
,dispensingpacksize
,vaprintname
,manufacturer
,[image]
)
SELECT
distinct
cmopid
,''Outsource Vendor'' as ndc
,qtyavail
,ProductCode
,DispensingPackSize as packsize
,vaprintname
,''Unavailable'' as Manufacturer
,''Unavailable'' as [Image]
FROM
@tmpFMResults tr
WHERE
tr.ProductCode IN
(
SELECT
ProductCode
FROM
@tmpFMResults
GROUP BY
ProductCode
HAVING
(COUNT(ProductCode) = 1)
)
AND tr.ProductCode IN
(
SELECT
ProductCode
FROM
[servername].[dbname].[dbo].[tablename] with (nolock)
GROUP BY
ProductCode
HAVING
COUNT(ProductCode) = 7
)
AND ProductEnabled = 1
ORDER BY
tr.ProductCode
select
cmopid
,ndc
,qtyavail
,productcode
,dispensingpacksize as packsize
,vaprintname
,manufacturer
,[image]
from
@tmpFinalResults'
exec ( @SQL2)
and I am getting this error:
Msg 102, Level 15, State 1, Line 42
Incorrect syntax near 'jo'.
What are the values for @control AND @CMOPIDs
ASKER
@control = A00
@CMOPIDs = 762
@CMOPIDs = 762
Instead of EXECUTE, try just PRINT and then take that query and past it back into SSMS and run it and see if it runs. It looks ok to me on first glance, and the only place you have the letters 'JO' is in a JOIN, so I don't see where it is erroring.
ASKER
I did that and ran it. The message in SSMS was:
DECLARE @tmpFinalResults TABLE
(
[cmopid] varchar(50)
,[ndc] varchar(50)
,[qtyavail] int
,[ProductCode] VARCHAR(50)
,[DispensingPackSize] real
,[vaprintname] varchar(50)
,[manufacturer] varchar(50)
,[image] varchar(50)
)
DECLARE @tmpFMResults TABLE
(
[cmopid] varchar(50)
,[qtyavail] int
,[ProductCode] VARCHAR(50)
,[DispensingPackSize] VARCHAR(50)
,[vaprintname] varchar(100)
,[ProductEnabled] INT
)
INSERT INTO @tmpFMResults
(
CmopID
,qtyavail
,ProductCode
,DispensingPackSize
,vaprintname
,ProductEnabled
)
SELECT
DISTINCT
fm.[Cmopid]
,fm.[OnHandStock]
,fm.[ProductCode]
,fm.[DispensingPackSize]
,vp.HostDescription
,fm.[ProductEnabled]
FROM [vhacmncdb].[CDB].[dbo].[F ormularyMa ster] fm with (nolock)
jo
I don't know why it is stopping at the join.
DECLARE @tmpFinalResults TABLE
(
[cmopid] varchar(50)
,[ndc] varchar(50)
,[qtyavail] int
,[ProductCode] VARCHAR(50)
,[DispensingPackSize] real
,[vaprintname] varchar(50)
,[manufacturer] varchar(50)
,[image] varchar(50)
)
DECLARE @tmpFMResults TABLE
(
[cmopid] varchar(50)
,[qtyavail] int
,[ProductCode] VARCHAR(50)
,[DispensingPackSize] VARCHAR(50)
,[vaprintname] varchar(100)
,[ProductEnabled] INT
)
INSERT INTO @tmpFMResults
(
CmopID
,qtyavail
,ProductCode
,DispensingPackSize
,vaprintname
,ProductEnabled
)
SELECT
DISTINCT
fm.[Cmopid]
,fm.[OnHandStock]
,fm.[ProductCode]
,fm.[DispensingPackSize]
,vp.HostDescription
,fm.[ProductEnabled]
FROM [vhacmncdb].[CDB].[dbo].[F
jo
I don't know why it is stopping at the join.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OMG it was 1000. What a bonehead move! Thanks man for all the information.
You could do something like this;
Open in new window