ユーザーが変数に渡された値を提供した場合、または誰かが特別に細工された悪意のあるコードを渡してストアド プロシージャを実行する方法を見つけた場合、プロシージャはインジェクションに対して無防備です。これに基づいた面白い漫画の私のユーザー名をググってください。
ストアド プロシージャを使用しているため、変数を確認してからSELECT
、指定された変数に基づいてステートメントを実行できます。
IF @featuretype = 'mobile'
BEGIN
select TOP 3 *
from featuredtypes_v
where featuredtypes_v.MobilePage=1
order by featuredtypes_v.priority desc
END
IF @featuretype = 'login'
BEGIN
select TOP 3 *
from featuredtypes_v
where featuredtypes_v.LoginPage=1
order by featuredtypes_v.priority desc
END
-- etc...
WHERE
または、条件を 1 つのクエリの句に入れることもできます。
select TOP 3 *
from featuredtypes_v
where (featuredtypes_v.MobilePage=1 AND @featuretype = 'Mobile') OR
(featuredtypes_v.LoginPage=1 AND @featuretype = 'Login') OR
(featuredtypes_v.IndexPage=1 AND @featuretype = 'Index')
order by featuredtypes_v.priority desc