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)? –