Solved

Pass multiple parameter to TSQL

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now