如何在 SQL 服务器中对我的场景进行动态查询?

How to make dynamic Query in SQL Server to my Scenario?

当我执行下面的动态查询时,我得到了结果

[dbo].[spGetTotalSalesQuantity] @CustomerQuery=1,@DateFrom='2015-01-01 00:00:00',@DateTo='2015-12-31 23:59:59',@whereSql='tq.storeid=1001'

@whereSql='tq.storeid=1001' This is the important one

当我执行以下命令时显示错误

[dbo].[spGetTotalSalesQuantity] @CustomerQuery=1,@DateFrom='2015-01-01 00:00:00',@DateTo='2015-12-31 23:59:59',@whereSql='tq.itemlookupcode=807424C0072'

@whereSql='tq.itemlookupcode=807424C0072' in this part it shows an error. I should add the single quotation before and after the value.

how can I add the single quotation before and after like this tq.itemlookupcode='807424C0072'?

这是错误消息

Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'C0072'.

只需双倍报价就可以了

@whereSql='tq.itemlookupcode=''807424C0072'''

尝试以下 select

select 'tq.itemlookupcode=''807424C0072'''

你会得到这样的结果

tq.itemlookupcode='807424C0072'