Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Pass multiple parameter to TSQL

Posted on 2015-01-18
5
126 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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