動的 SQL とビューを使用する必要があります。
declare @MenuIDs varchar(max) = '1,2,3,4';
declare @SQL varchar(max);
set @SQL = 'create view vTab as
SELECT tMenuMain.MenuId, tMenuMain.MenuRank
FROM tMenuMain
WHERE tMenuMain.MenuId IN ('+@MenuIDs+')';
/*
Select in @SQL should look like:
create view vTab as
SELECT tMenuMain.MenuId, tMenuMain.MenuRank
FROM tMenuMain
WHERE tMenuMain.MenuId IN (1,2,3,4)
*/
exec(@SQL)
select *
into #TempRankTable
from vTab
drop view vTab
select * from #TempRankTable;
create table
または挿入する前にできれば
declare @MenuIDs varchar(max) = '1,2,3,4';
declare @SQL varchar(max);
create table #TempRankTable
(
MenuId ...
MenuRank ...
)
set @SQL = 'insert into #TempRankTable(MenuId,MenuRank)
SELECT tMenuMain.MenuId, tMenuMain.MenuRank
FROM tMenuMain
WHERE tMenuMain.MenuId IN ('+@MenuIDs+')';
/*
Select in @SQL should look like:
insert into #TempRankTable(MenuId,MenuRank)
SELECT tMenuMain.MenuId, tMenuMain.MenuRank
FROM tMenuMain
WHERE tMenuMain.MenuId IN (1,2,3,4)
*/
exec(@SQL)
select * from #TempRankTable;