请教Sql 存储过程?

来源:百度知道 编辑:UC知道 时间:2024/05/12 09:27:43
CREATE PROCEDURE [dbo].[Record_count]
(
@record int output,
@table nvarchar(100),
@keyword nvarchar(100)
)
As
set nocount on

if(@keyword="") or (@keyword is null)
select @record=count(id) from @table
else
select @record=count(id) from @table where type =@keyword
GO
点“检查语法”时为什么总是提示“必须声明@table变量”
我该怎么做

@table变量不能直接写在from后面,应该先构造一个sql语句字符串,然后连接上@table
改为:
declare @sql varchar(50)
set @sql='select '+@record+'=count(id) from '+ @table +' where type =@keyword '
exec(@sql)

参考这种格式来写,明白了吗?

表名是不能用变量的