Solved

Pass multiple parameter to TSQL

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

Technology Partners: 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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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