您必须使用动态 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;