declare @pid int
declare @mid int
declare @tableName varchar(10)
declare @query nvarchar(1000)
declare @subquery nvarchar(300)
set @pid = 1
set @mid = 2
set @query = 'Select * from '[email protected]+' where'
if(@pid is not null)
begin
set @query = @query+' pid ='[email protected]+' and'
end
if(@mid is not null)
begin
set @query = @query+' mid ='[email protected]+' and'
end
如果@pid和@mid不空,都会进入这个query.If @pid和@mid空如何comapre 2串,我想从query.Same删除“其中”为'和',如果两者都被选中。在执行查询
set @subquery = select right('''[email protected]+''',5)
if(@subquery = 'where')
begin
print @query
-- execute sp_executesql @query
end
但无法比拟的,在如果clause.Does我需要EXCUTE @ subquery.If是,如何把该值。
我忘了告诉TableName也是动态的,为什么我选择执行sp_executesql @query。 – pinky
@pinky,你也忘了回复[mocha的评论](http://stackoverflow.com/questions/12245489/how-to-comapre-2-string-while-executing-the-query#comment16414042_12245489)? –