Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
ALTER procedure [dbo].[ERP_ShowStockRegi]
(
   @input varchar(100)
)
as
begin

DECLARE @sqlCommand varchar(1000)
SET @sqlCommand ='select DISTINCT * from 
(SELECT dbo.StockLedger.SledId, dbo.StockLedger.SledCode, dbo.StockLedger.StockDetId, dbo.StockLedger.SLedType, dbo.StockLedger.SLedQty, 
dbo.StockLedger.SLedInOut, dbo.StockLedger.SLedPrice, dbo.StockLedger.SLedDate, dbo.StockLedger.SLedRefNo, dbo.StockLedger.BranchId, 
dbo.StockLedger.CustId, dbo.StockMaster.StockId, dbo.StockMaster.StockParent, dbo.StockMaster.StockName, dbo.StockMaster.StockAlias, 
dbo.StockMaster.StockUnit, dbo.StockMaster.StockType, dbo.StockMaster.Branch, dbo.StockMaster.StockComp, 
dbo.StockMaster.StockName + ' (' + CASE WHEN dbo.StockDetails.PartNo IS NULL THEN '' ELSE dbo.StockDetails.PartNo END + ')' AS ItemPart, 
CASE WHEN sledinout = '1' AND sledtype <> 'OPN' THEN sledqty ELSE 0 END AS QtyIn, CASE WHEN sledinout = '0' AND 
sledtype <> 'OPN' THEN sledqty ELSE 0 END AS QtyOut, dbo.StockDetails.PartNo, 0 AS [OPEN], 0 AS PositiveOpeningQtyIn, 0 AS positiveClosingQty, 
0 AS [CLOSE], dbo.AcctMaster.AcctAlias AS PartyName, dbo.StockMaster.Active
FROM         dbo.StockLedger INNER JOIN
dbo.StockMaster ON dbo.StockLedger.StockDetId = dbo.StockMaster.StockId INNER JOIN
dbo.StockDetails ON dbo.StockMaster.StockId = dbo.StockDetails.StockId LEFT OUTER JOIN
dbo.AcctMaster ON dbo.StockLedger.CustId = dbo.AcctMaster.AcctId
WHERE (dbo.StockMaster.Active = N'1')) as tab1
'+ @input
 execute(@sqlCommand)
end
Posted
Updated 26-May-15 19:42pm
v2
Comments
RossMW 27-May-15 1:48am    
What is the error message or what is not working?
RossMW 27-May-15 1:52am    
Also as you are creating a statement the quotes will need to be fixed. To add a quotes two quotes for each qoute
Maciej Los 27-May-15 1:58am    
My virtual 5!
Maciej Los 27-May-15 1:58am    
What stores @input variable?
mrprakash 27-May-15 4:53am    
where id='50' and refno='ref1'

1 solution

I did few corrections in the query:

1) Please make sure when you are working on dynamic query then if you want to use single quote then put it 2 times (ex. ' will be '' and '' will be '''')

2) If you want to insert bracket then it will be part of the string. So no need to make it separate by using + symbol.

Please try with the below query:

SQL
DECLARE @sqlCommand varchar(1000)
SET @sqlCommand ='select DISTINCT * from 
(SELECT dbo.StockLedger.SledId, dbo.StockLedger.SledCode, dbo.StockLedger.StockDetId, dbo.StockLedger.SLedType, dbo.StockLedger.SLedQty, 
dbo.StockLedger.SLedInOut, dbo.StockLedger.SLedPrice, dbo.StockLedger.SLedDate, dbo.StockLedger.SLedRefNo, dbo.StockLedger.BranchId, 
dbo.StockLedger.CustId, dbo.StockMaster.StockId, dbo.StockMaster.StockParent, dbo.StockMaster.StockName, dbo.StockMaster.StockAlias, 
dbo.StockMaster.StockUnit, dbo.StockMaster.StockType, dbo.StockMaster.Branch, dbo.StockMaster.StockComp, 
dbo.StockMaster.StockName (CASE WHEN dbo.StockDetails.PartNo IS NULL THEN '''' ELSE dbo.StockDetails.PartNo END ) AS ItemPart, 
CASE WHEN sledinout = ''1'' AND sledtype <> ''OPN'' THEN sledqty ELSE 0 END AS QtyIn, CASE WHEN sledinout = ''0'' AND 
sledtype <> ''OPN'' THEN sledqty ELSE 0 END AS QtyOut, dbo.StockDetails.PartNo, 0 AS [OPEN], 0 AS PositiveOpeningQtyIn, 0 AS positiveClosingQty, 
0 AS [CLOSE], dbo.AcctMaster.AcctAlias AS PartyName, dbo.StockMaster.Active
FROM         dbo.StockLedger INNER JOIN
dbo.StockMaster ON dbo.StockLedger.StockDetId = dbo.StockMaster.StockId INNER JOIN
dbo.StockDetails ON dbo.StockMaster.StockId = dbo.StockDetails.StockId LEFT OUTER JOIN
dbo.AcctMaster ON dbo.StockLedger.CustId = dbo.AcctMaster.AcctId
WHERE (dbo.StockMaster.Active = N''1'')) as tab1
'+ @input
 execute(@sqlCommand)


Please let me know if you have any concern or query or if you are still facing issue.

Thanks
Advay Pandya
 
Share this answer
 
Comments
mrprakash 27-May-15 2:10am    
Sir,

thanks for solve this issue.
Advay Pandya 27-May-15 2:11am    
Always welcome :)
mrprakash 27-May-15 4:53am    
sir i hv a prblm to this query
Advay Pandya 27-May-15 6:04am    
Which problem are you facing ? I saw your new post. Please reply there so if possible I can help you quicker.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900