Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pass multiple parameter to TSQL

Posted on 2015-01-18
5
Medium Priority
?
144 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
[X]
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
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 11

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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