tags:

views:

53

answers:

2

Please look that alias name. I hope to set the value into a string var. How to put single quote inside a string which is in single quote.

   SET @SQLString = N'SELECT purDetQty as 'detQty',stkBatchCode as 'batchCode',purDetProductId as 'productId'
               INTO #ProductTable FROM PurchaseDetail 
               INNER JOIN Stock on stkId=purDetStockId 
               WHERE purDetID=@detId'
+2  A: 

You need to escape your quotes with a second one:

SET @SQLString = N'SELECT purDetQty as ''detQty''
      ,stkBatchCode as ''batchCode''
      ,purDetProductId as ''productId'' 
           INTO #ProductTable FROM PurchaseDetail  
           INNER JOIN Stock on stkId=purDetStockId  
           WHERE purDetID=@detId' 
ck
+1  A: 

You can put single quotes inside a string with single quotes in SQL by doubling them, i.e.:

SET @SQLString = N'SELECT purDetQty as ''detQty'',stkBatchCode as ''batchCode'',purDetProductId as ''productId''
           INTO #ProductTable FROM PurchaseDetail 
           INNER JOIN Stock on stkId=purDetStockId 
           WHERE purDetID=@detId'

Note that this is two single quotes (''), not one double quote (")

However, in this case, you should not need to use the single quotes, but should be able to simply use

SET @SQLString = N'SELECT purDetQty as detQty,stkBatchCode as batchCode,purDetProductId as productId
           INTO #ProductTable FROM PurchaseDetail 
           INNER JOIN Stock on stkId=purDetStockId 
           WHERE purDetID=@detId'
Mario Menger