CREATE PROCEDURE wvo_showmorenew(
@tblName varchar(255), -- 表名
@v_tags varchar(255), -- 关键字
@PageIndex int, -- 页码
@recordcounts int output --输出记录总数
)
AS
set @strWhere=' tags like ''%'+@v_tags+'%'''begin
if @strWhere !=''
begin
select @recordcounts=count(*) from @tblName where tags like '%'+@v_tags+'%'
--select @recordcounts=count(*) from vo_woman where tags like '%'+@v_tags+'%'
end
else
begin
select @recordcounts=count(*) from vo_woman
end上面这段是不完整的存储过程代码,其中
select @recordcounts=count(*) from @tblName where tags like '%'+@v_tags+'%'
这句话提示@tblName变量为定义,但这个变量是输入参数,需要单独定义吗,如果将@tblName直接换为存在的表名就正确了,请问我应该怎么改才能达到动态的表名的效果.
谢谢.
@tblName varchar(255), -- 表名
@v_tags varchar(255), -- 关键字
@PageIndex int, -- 页码
@recordcounts int output --输出记录总数
)
AS
set @strWhere=' tags like ''%'+@v_tags+'%'''begin
if @strWhere !=''
begin
select @recordcounts=count(*) from @tblName where tags like '%'+@v_tags+'%'
--select @recordcounts=count(*) from vo_woman where tags like '%'+@v_tags+'%'
end
else
begin
select @recordcounts=count(*) from vo_woman
end上面这段是不完整的存储过程代码,其中
select @recordcounts=count(*) from @tblName where tags like '%'+@v_tags+'%'
这句话提示@tblName变量为定义,但这个变量是输入参数,需要单独定义吗,如果将@tblName直接换为存在的表名就正确了,请问我应该怎么改才能达到动态的表名的效果.
谢谢.
@tblName varchar(255), -- 表名
@v_tags varchar(255), -- 关键字
@PageIndex int, -- 页码
@recordcounts int output --输出记录总数
)
AS
declare @strWhere varchar(1000)
set @strWhere=' tags like ''%'+@v_tags+'%'''
declare @s nvarchar(4000)
begin if @strWhere !=''
begin
set @s='
select @recordcounts=count(*) from '+@tblName+' where tags like ''%'+@v_tags+'%'''
--select @recordcounts=count(*) from vo_woman where tags like '%'+@v_tags+'%'
exec sp_executesql @s, N'@recordcounts int out',@recordcounts out
end
else
begin
select @recordcounts=count(*) from vo_woman
end
end