如何在查询字符串中添加别名。?

时间:2010-04-16 07:41:10

标签: sql

请查看别名。我希望将值设置为字符串var。 如何将单引号放在单引号的字符串中。

   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 个答案:

答案 0 :(得分:2)

您需要使用第二个引号来转义引号:

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' 

答案 1 :(得分:1)

您可以将单引号放在SQL中带单引号的字符串中,加倍,即:

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'

请注意,这是两个单引号(''),而不是一个双引号(“)

但是,在这种情况下,您不需要使用单引号,但应该能够简单地使用

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'