Solved

Pass multiple parameter to TSQL

Posted on 2015-01-18
5
127 Views
Last Modified: 2015-01-19
Passing multiple value into a parameter but it can't return any records. No idea what's wrong. Any idea ?

exec MyStorePro  'P000042545,P000160279'

CREATE PROCEDURE [dbo].[MyStorePro] @INVENTBATCHID VARCHAR(3000)
AS
BEGIN
SELECT A.INVENTBATCHID,D.ITEMID ,D.INVENTD2ID, D.INVENTD3ID, D.INVENTD4ID, D.INVENTDGOLDTYPEID, REFBOMID,
CASE WHEN A.COSTGROUPID NOT LIKE 'ST%' THEN 4 ELSE B.STONETYPE END AS STONETYPE,
CASE WHEN A.COSTGROUPID NOT LIKE 'ST%' THEN SUM(OUTQTY) ELSE 0 END AS GoldCost,
CASE WHEN A.COSTGROUPID LIKE 'ST%' THEN CAST(CAST(SUM(OutQtySec) AS decimal) AS NVARCHAR(10))
                  +SUBSTRING(A.COSTGROUPID,4,3)+CAST(CAST(SUM(OUTQTY) AS decimal(20,2)) AS NVARCHAR(10)) Else '' END AS Material
into #TM1 FROM .LASTWIPMATERIALTRANS A
INNER JOIN .BOMCostGroup B ON B.COSTGROUPID=A.COSTGROUPID
INNER JOIN .INVENTBATCH C ON A.INVENTBATCHID=C.INVENTBATCHID AND C.DATAAREAID='OPR'
INNER JOIN .BOMVERSION D ON C.REFBOMID=D.BOMID AND C.ITEMID=D.ITEMID AND D.DATAAREAID='OPR'
WHERE A.INVENTBATCHID in (''' + @INVENTBATCHID + ''')
GROUP BY SUBSTRING(A.COSTGROUPID,4,3),CASE WHEN A.COSTGROUPID NOT LIKE 'ST%' THEN 4 ELSE B.STONETYPE END,
A.COSTGROUPID,A.INVENTBATCHID,D.ITEMID,D.INVENTD2ID,D.INVENTD3ID,D.INVENTD4ID,D.INVENTDGOLDTYPEID,
REFBOMID
0
Comment
Question by:AXISHK
5 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40555956
Consider simplifying your proc to the point is works. Then add to it gradually the feature you need to have. Along the way, you will come across to the problem and will be isolate it.

CASE WHEN A.COSTGROUPID NOT LIKE 'ST%' THEN 4 ELSE B.STONETYPE END AS STONETYPE,
CASE WHEN A.COSTGROUPID NOT LIKE 'ST%' THEN SUM(OUTQTY) ELSE 0 END AS GoldCost,

Do you want the similar CASE WHEN two times?
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 40555979
Hi,
You now are selecting records into one temp table.

1) Try to remove "into ..." clause
2) Also directly run the statement to ensure it can really capture records.
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 40556097
Hi AxisHK,

Just an issue with quotes within variable.

Please check below  code.It will work for you.

CREATE PROCEDURE [dbo].[MyStorePro] 
  @INVENTBATCHID VARCHAR(3000) 
AS 
  BEGIN 
    SELECT     a.inventbatchid, 
               d.itemid , 
               d.inventd2id, 
               d.inventd3id, 
               d.inventd4id, 
               d.inventdgoldtypeid, 
               refbomid, 
               CASE 
                          WHEN a.costgroupid NOT LIKE 'ST%' THEN 4 
                          ELSE b.stonetype 
               END AS stonetype, 
               CASE 
                          WHEN a.costgroupid NOT LIKE 'ST%' THEN Sum(outqty) 
                          ELSE 0 
               END AS goldcost, 
               CASE 
                          WHEN a.costgroupid LIKE 'ST%' THEN Cast(Cast(Sum(outqtysec) AS DECIMAL) AS NVARCHAR(10)) +Substring(a.costgroupid,4,3)+Cast(Cast(Sum(outqty) AS DECIMAL(20,2)) AS NVARCHAR(10))
                          ELSE '' 
               END AS material 
    INTO       #tm1 
    FROM       .lastwipmaterialtrans A 
    INNER JOIN .bomcostgroup B 
    ON         b.costgroupid=a.costgroupid 
    INNER JOIN .inventbatch C 
    ON         a.inventbatchid=c.inventbatchid 
    AND        c.dataareaid='OPR' 
    INNER JOIN .bomversion D 
    ON         c.refbomid=d.bomid 
    AND        c.itemid=d.itemid 
    AND        d.dataareaid='OPR' 
    WHERE      a.inventbatchid IN (' + @INVENTBATCHID + ') 
    GROUP BY   Substring(a.costgroupid,4,3), 
               CASE 
                          WHEN a.costgroupid NOT LIKE 'ST%' THEN 4 
                          ELSE b.stonetype 
               END, 
               a.costgroupid, 
               a.inventbatchid, 
               d.itemid, 
               d.inventd2id, 
               d.inventd3id, 
               d.inventd4id, 
               d.inventdgoldtypeid, 
               refbomid

Open in new window

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 40557371
sql doesn't interpret the values within the variable as multiple parameters ....

if you want to allow users to pass multiple values into your sql then you need to use different techniques.

e.g.  see http://ole.michelsen.dk/blog/split-string-to-table-using-transact-sql.html

for a split to table function that you can use.
0
 

Author Closing Comment

by:AXISHK
ID: 40557571
Tks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question