其中从另一个表子句
因此,目的是为表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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您的某些单引号可能必须引用,或者您可以使用变量。例如:
@whereclause = n'colesce(@whereclause +'','')...
可能需要@whereclause = n'colesce(@whereclause +'''',',' '')...
或这些行上的其他内容。您可以将Cocece(@whereclause +'''')的结果存储在变量中,然后以类似方式将其与其他变量进行限制。Alternatively, you could write a query in SQL Server like this:
You'll get the output like this:
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:
You'll get the output like this:
Example:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=12c3110a0d82e754bfc16f686779cddd