2015-05-05 111 views
0

我想在動態查詢中使用in運算符。目前,我需要某人指出正確的方向,以便如何構造用於分隔值的撇號,以便它們不會干擾動態查詢結構的撇號。這是查詢中出現問題的一部分,所以我需要知道如何放置分隔符,以便所有值都可以包含在動態查詢格式中,而不是我現在使用的常規查詢格式。如何在動態查詢中使用IN運算符

WHERE ai.ImNumber != me.Company 
    AND me.ID IN ('value1','value2','value2','value3','value4','value5')' 

我的查詢是

SET @SQLString = 'select me.EventID , ai.AggregateInfo, '''[email protected]+''' AS DateBegin, '''[email protected]+''' AS DateEnd 
FROM tbl_MajorEarners me 
INNER JOIN tbl_currentearners e ON e.EventID = me.EventID 
INNER JOIN tbl_AggregateInfo ai ON ai.AggregateID = cs.ItemID 
WHERE ai.ImNumber != me.CompanyNUmber 
AND me.ID IN ('value1','value2','value2','value3','value4','value5')' 
+0

這是用於mysql還是sql-server? – ughai

+0

添加您的問題的更多詳細信息?你會有最多5個動態值嗎?你的問題不清楚 – ughai

+0

我正在使用sqlserver 2008,這是我的動態查詢 \t'SET @SQLString ='select me.EventID,ai.AggregateInfo, \t'''+ @ DateBegin +'''AS DateBegin,'' '+ @ EDateEnd +' '' AS DateEnd \t FROM tbl_MajorEarners我 \t INNER JOIN tbl_currentearnersË \t ON e.EventID = me.EventID \t INNER JOIN tbl_AggregateInfo艾 \t ai.AggregateID = cs.ItemID 其中,Ai .ImNumber!=我。公司紐約 AND me.ID IN('value1','value2','value2','value3','value4','val ue5')'' – anchor

回答

0

我認爲這是你在找什麼

SET @SQLString = 'select me.EventID , ai.AggregateInfo, ''@DateBegin'' AS DateBegin, ''@EDateEnd'' AS DateEnd 
FROM tbl_MajorEarners me 
INNER JOIN tbl_currentearners e ON e.EventID = me.EventID 
INNER JOIN tbl_AggregateInfo ai ON ai.AggregateID = cs.ItemID 
WHERE ai.ImNumber != me.CompanyNUmber 
AND me.ID IN (''value1'',''value2'',''value2'',''value3'',''value4'',''value5'')' 

這臺@SqlString

select me.EventID , ai.AggregateInfo, '@DateBegin' AS DateBegin, '@EDateEnd' AS DateEnd 
FROM tbl_MajorEarners me 
INNER JOIN tbl_currentearners e ON e.EventID = me.EventID 
INNER JOIN tbl_AggregateInfo ai ON ai.AggregateID = cs.ItemID 
WHERE ai.ImNumber != me.CompanyNUmber 
AND me.ID IN ('value1','value2','value2','value3','value4','value5') 
相關問題