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