动态SQL语句和动态插入表
存储过程如下,
CREATE PROCEDURE Book_PageGetBooks1
(
@ClassIDs varchar(500)
)
as
set nocount on
begin
declare @indextable table(id int identity(1,1),nid int)
insert into @indextable(nid) select Bookid from Book_Books
exec('select O.BookID from Book_Books O,@indextable t where O.BookID=t.nid and BookID in (' +@ClassIDS +') order by t.id')
end
set nocount off
GO
提示的错误是"必须声明变量 '@indextable'."
因为@ClassIDS是动态生成的字符串,所以必须要用Exec执行,但是我上面很明显的是插入了表@indextable的,但是我在insert into 前面加入exec的话,它也会提示错误