其中从另一个表子句

发布于 2025-01-27 04:50:25 字数 1318 浏览 2 评论 0原文

因此,目的是为表A进行SQL查询,但在表B中存储了其中的子句。

这是示例

表B:

BatchNumber | DataItem | Operator | TValue 
12345       | Branch   | =        | 2700
12345       | StockType| =        |S
12345       | PRCode   | <>       |AD

从此表中,我需要在下面进行示例

select *
from testdata.TableA
where Branch = 2700 and StockType = 'S' and PRCode <> 'AD'

declare @whereClause varchar(400)
declare @schema varchar(10) = 'testdata'
declare @Batch varchar(15) = 12345

select @whereClause = N' COALESCE(@whereClause + ' ' ,'') +  trim(DataItem) + ' ' + trim(Operator) + ' ' + 
        case 
            when trim(TValue) LIKE '[A-Za-z]%' then '''' + trim(TValue) + '''' + ' AND' 
            when trim(TValue) = ' ' then  ''''  + '''' + ' AND' 
            else trim(TValue) + ' AND'
        end 
from' + @schema + '.TableB where BatchNumber =' +  @Batch  

--remove AND from the end of statement 
SET @whereClause = SUBSTRING(@whereClause,0, LEN(@whereClause) - 3) 

PRINT @whereClause

但是,这给我带来了案例语句中单个代码的语法错误。

msg 102,15级,状态1,第4行
不正确的语法附近',') + trim(dataitem) +'。

我需要案例语句来添加围绕字符串操作员的单引号。

我还尝试了使用for xml Path('')的其他方法,但是消除了单个引号,并且“&lt;&gt;”标志更改为“ GTLT”,

真的很感谢任何帮助。

So the goal is to make a SQL query for table A but the where clause is stored in table B.

here is example

table B:

BatchNumber | DataItem | Operator | TValue 
12345       | Branch   | =        | 2700
12345       | StockType| =        |S
12345       | PRCode   | <>       |AD

from this table I need to make below where statement

select *
from testdata.TableA
where Branch = 2700 and StockType = 'S' and PRCode <> 'AD'

here are the different methods I tried using to form a sql

declare @whereClause varchar(400)
declare @schema varchar(10) = 'testdata'
declare @Batch varchar(15) = 12345

select @whereClause = N' COALESCE(@whereClause + ' ' ,'') +  trim(DataItem) + ' ' + trim(Operator) + ' ' + 
        case 
            when trim(TValue) LIKE '[A-Za-z]%' then '''' + trim(TValue) + '''' + ' AND' 
            when trim(TValue) = ' ' then  ''''  + '''' + ' AND' 
            else trim(TValue) + ' AND'
        end 
from' + @schema + '.TableB where BatchNumber =' +  @Batch  

--remove AND from the end of statement 
SET @whereClause = SUBSTRING(@whereClause,0, LEN(@whereClause) - 3) 

PRINT @whereClause

However this is giving me syntax error with single codes in the case statement.

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ' ,') + trim(DataItem) + '.

I need case statement to add single quote around string operators.

I also tried different method with FOR XML PATH(''), but with this single quotes are eliminated and "<>" signs are changed to "gtlt"

really appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江城子 2025-02-03 04:50:25

似乎您的某些单引号可能必须引用,或者您可以使用变量。例如:@whereclause = n'colesce(@whereclause +'','')...可能需要@whereclause = n'colesce(@whereclause +'''',',' '')...或这些行上的其他内容。您可以将Cocece(@whereclause +'''')的结果存储在变量中,然后以类似方式将其与其他变量进行限制。

Alternatively, you could write a query in SQL Server like this:

select 'select * '
union all select 'from testdata.TableA '
union all select 'where 1 = 1 '
union all
select concat(
  '   and ',
  dataitem, ' ', operator, ' ',
  case
  when tvalue like '%[0-9]%' then tvalue
  else concat('''', tvalue, '''')
  end
)
from test /* or tableB */
where batchnumber = 12345; /*optional where clause*/

You'll get the output like this:

select * 
from testdata.TableA 
where 1 = 1 
   and Branch = 2700
   and StockType = 'S'
   and PRCode <> 'AD'

Example:

It seems like some of your single-quotations may have to be quoted or you could use variables. For example: @whereClause = N' COALESCE(@whereClause + ' ' ,'')... may require @whereClause = N' COALESCE(@whereClause + '' '' ,'''')... or something on those lines. You could store there result of COALESCE(@whereClause + ' ' ,'') in a variable and then concat it with other variables in a similar way.

Alternatively, you could write a query in SQL Server like this:

select 'select * '
union all select 'from testdata.TableA '
union all select 'where 1 = 1 '
union all
select concat(
  '   and ',
  dataitem, ' ', operator, ' ',
  case
  when tvalue like '%[0-9]%' then tvalue
  else concat('''', tvalue, '''')
  end
)
from test /* or tableB */
where batchnumber = 12345; /*optional where clause*/

You'll get the output like this:

select * 
from testdata.TableA 
where 1 = 1 
   and Branch = 2700
   and StockType = 'S'
   and PRCode <> 'AD'

Example:

https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=12c3110a0d82e754bfc16f686779cddd

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文