첫째, 좋은 일을하려고 밖으로 도달 : 플래그가
그리고 셋째
, 당신도 당신의
in
목록에 작은 따옴표를 추가하지 않는 설정되지 않은 경우
둘째, 당신은 취급하지 않습니다 비 동적 SQL 방식을 찾으십시오.
아래의 "속임수"는 룩업 테이블의 수를 고려한 것입니다.
IMHO, 동적 SQL 응답을 모두 포함하는 "try this"를 무시하십시오. 문제가 동적 SQL없이 해결 될 수 있다면 IMHO 여야합니다. 여기에서 몇 가지 문제점에 관해 읽을 수 있습니다 : http://sqlmag.com/database-performance-tuning/don-t-fear-dynamic-sql 사용하는 경우에도 사용하기에 알맞은 방법과 바보 같은 방법이 있습니다.
IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
drop table #TableOne
end
CREATE TABLE #TableOne
(
SurrogateKeyIDENTITY int not null IDENTITY (1,1) ,
MyLabel varchar(16)
)
Insert into #TableOne (MyLabel) select 'Clean Label' UNION ALL select 'Clean Label' UNION ALL select 'Key Interview' UNION ALL select 'Key Interview' UNION ALL select 'Special Report' UNION ALL select 'Special Report' UNION ALL select 'NotAMatch' UNION ALL select 'NotAMatch'
Declare @LookupMatchTable Table (KeyVal varchar(50))
if(1=1) /* use your bool flags here */
begin
Insert Into @LookupMatchTable (KeyVal) Select 'Clean Label'
end
if(1=1) /* use your bool flags here */
begin
Insert Into @LookupMatchTable (KeyVal) Select 'Key Interview'
end
if(1=1) /* use your bool flags here */
begin
Insert Into @LookupMatchTable (KeyVal) Select 'Special Report'
end
declare @LookupMatchTableCount int
select @LookupMatchTableCount = (select count(*) from @LookupMatchTable)
SELECT * FROM #TableOne t1 WHERE (@LookupMatchTableCount = 0 OR EXISTS (Select KeyVal from @LookupMatchTable lmt where lmt.KeyVal = t1.MyLabel))
/* now show if there are no entries in the look up table, everything comes back */
delete from @LookupMatchTable
select @LookupMatchTableCount = (select count(*) from @LookupMatchTable)
SELECT * FROM #TableOne t1 WHERE (@LookupMatchTableCount = 0 OR EXISTS (Select KeyVal from @LookupMatchTable lmt where lmt.KeyVal = t1.MyLabel))
/* now show that if there is something in the lookup table that doesn't match the "real" table......no rows returned */
delete from @LookupMatchTable
Insert Into @LookupMatchTable (KeyVal) Select 'No Match Whatsoever'
select @LookupMatchTableCount = (select count(*) from @LookupMatchTable)
SELECT * FROM #TableOne t1 WHERE (@LookupMatchTableCount = 0 OR EXISTS (Select KeyVal from @LookupMatchTable lmt where lmt.KeyVal = t1.MyLabel))
IF OBJECT_ID('tempdb..#TableOne') IS NOT NULL
begin
drop table #TableOne
end
귀하의 @where는 앞에 ','; SQL에 추가하기 전에 초기 쉼표를 제거하는 논리를 작성하십시오. – techspider
문자열 다음에 이동해야하며 값의 일부가 아니어야합니다. 문자열에 값을 넣으려면 '세 번 사용하십시오. 예를 들어, + N ', Clean Label'이 아니어야합니다. + '' 'Clean Label' '+', ''여야합니다. 또한 다른 사람이 말한대로 문자열을 어디에서나 끝내거나 끝내십시오. 찾으려면 CHARINDEX를 사용하고 줄을 자르기 위해서는 SUBSTRING을 사용하십시오. –