DECLARE @row SET =(SELECT COUNT(*)FROM grade) @rows
时间: 2024-02-10 12:12:40 浏览: 146
这是一段 SQL 语句,其中 @row 和 @rows 是变量名,该语句的作用是查询 grade 表中的数据行数,并将结果赋值给 @row 变量。语句中的 SET 关键字用于给变量赋值。但是这段代码似乎有问题,因为 SET 后面应该跟一个表达式或值,而不是一个 SQL 查询语句。应该修改为:
DECLARE @row INT
SET @row = (SELECT COUNT(*) FROM grade)
SELECT @row AS rows
相关问题
create procedure SearchSumBySno (@sno char(5)) as begin declare @sum int,@avg numeric(5,1),@grade tinyint,@count tinyint select @sum=0 , @count=0 declare myCursor cursor for select grade from SC where sno=@sno open myCursor fetch myCursor into @grade while(@@fetch_status=0) begin select @sum=@sum+@grade select @count=@count+1 fetch myCursor into @grade end close myCursor deallocate myCursor if @count=0 select 0 '总分', 0 '平均分' else begin select @avg=@sum/@count select @sum '总分',@avg '总分' end end
这是一个用于按学号搜索成绩总和的存储过程。它首先声明了一些变量,包括总和(@sum)、平均值(@avg)、成绩(@grade)和计数器(@count)。然后,它打开一个游标(myCursor),并根据给定的学号从SC表中选择成绩。在循环中,它将每个成绩累加到总和中,并增加计数器的值。最后,它关闭游标并释放资源。如果计数器为0,则返回总分和平均分都为0;否则,返回总分和平均分的值。
【简答题】 请阅读下面分页存储过程的代码,并且为每一行代码加上注释。 create procedure pagequery ( @sqlTable nvarchar(200), ----待查询表名 @sqlColumns nvarchar(500) , ----待显示字段 @sqlWhere nvarchar(1000) , ----查询条件,不需where @sqlSort nvarchar(500) , ----排序字段,不需order by @pageIndex int, ----当前页 @pageSize int, ----每页显示的记录数 @rowTotal int = 1 output ----返回总记录数 ) as begin set nocount on; -- 获取记录总数 declare @sqlcount nvarchar(1000) ; set @sqlcount = N' select @rowTotal=count(*) from '+@sqlTable +' where 1=1 '+ @sqlWhere; exec sp_executesql @sqlcount,N'@rowTotal int out ',@rowTotal out ; -- 返回数据查询 declare @sqldata nvarchar(1000) ; set @sqldata=' select '+ @sqlColumns + ' from (select *,Row_number() over(order by '+ @sqlSort +') as RN from '+ @sqlTable +' where 1=1 '+ @sqlWhere+') as TR where RN>'+ cast(@pageSize*@pageIndex as varchar(20))+' and RN<'+ cast((@pageSize*(@pageIndex+1)+1) as varchar(20)); exec sp_executesql @sqldata ; end --调用如下: declare @totalrow int; exec pagequery 'employee','*','','ID asc',1,10,@totalrow output; select @totalrow; 2)MySQL 版本 create procedure pagequery ( in sqlTable varchar(256), in sqlColumns varchar(512), in sqlWhere varchar(1024), in sqlSort varchar(512), in pageIndex int, in pageSize int, out rowTotal int ) begin declare start_row int default 0; set start_row = pageIndex* pageSize; -- 获取记录总数 set @sqlcount = concat('select count(*) into @rows_total from ', sqlTable, ' where 1=1 ', sqlWhere); prepare count_stmt from @sqlcount; execute count_stmt; deallocate prepare count_stmt; set rowTotal = @rows_total; -- 返回数据查询 set @sqldata = concat('select ', sqlColumns, ' from ', sqlTable, ' where 1=1 ', sqlWhere, ' order by ', sqlSort,' limit ', start_row, ',', pageSize); prepare main_stmt from @sqldata; execute main_stmt; deallocate prepare main_stmt; end --调用如下: call pagequery('emp
1)SQL Server 版本
```
create procedure pagequery
(
@sqlTable nvarchar(200), -- 待查询表名
@sqlColumns nvarchar(500) , -- 待显示字段
@sqlWhere nvarchar(1000) , -- 查询条件,不需where
@sqlSort nvarchar(500) , -- 排序字段,不需order by
@pageIndex int, -- 当前页
@pageSize int, -- 每页显示的记录数
@rowTotal int = 1 output -- 返回总记录数
)
as
begin
set nocount on; -- 不返回行数
-- 获取记录总数
declare @sqlcount nvarchar(1000);
set @sqlcount = N'select @rowTotal=count(*) from '+@sqlTable +' where 1=1 '+ @sqlWhere;
exec sp_executesql @sqlcount,N'@rowTotal int out ',@rowTotal out;
-- 返回数据查询
declare @sqldata nvarchar(1000);
set @sqldata='select '+ @sqlColumns + ' from (select *,Row_number() over(order by '+ @sqlSort +') as RN from '+ @sqlTable +' where 1=1 '+ @sqlWhere+') as TR where RN>'+ cast(@pageSize*@pageIndex as varchar(20))+' and RN<'+ cast((@pageSize*(@pageIndex+1)+1) as varchar(20));
exec sp_executesql @sqldata;
end
-- 调用如下:
declare @totalrow int;
exec pagequery 'employee','*','','ID asc',1,10,@totalrow output;
select @totalrow;
```
2)MySQL 版本
```
create procedure pagequery
(
in sqlTable varchar(256), -- 待查询表名
in sqlColumns varchar(512), -- 待显示字段
in sqlWhere varchar(1024), -- 查询条件,不需where
in sqlSort varchar(512), -- 排序字段,不需order by
in pageIndex int, -- 当前页
in pageSize int, -- 每页显示的记录数
out rowTotal int -- 返回总记录数
)
begin
declare start_row int default 0;
set start_row = pageIndex * pageSize;
-- 获取记录总数
set @sqlcount = concat('select count(*) into @rows_total from ', sqlTable, ' where 1=1 ', sqlWhere);
prepare count_stmt from @sqlcount;
execute count_stmt;
deallocate prepare count_stmt;
set rowTotal = @rows_total;
-- 返回数据查询
set @sqldata = concat('select ', sqlColumns, ' from ', sqlTable, ' where 1=1 ', sqlWhere, ' order by ', sqlSort,' limit ', start_row, ',', pageSize);
prepare main_stmt from @sqldata;
execute main_stmt;
deallocate prepare main_stmt;
end
-- 调用如下:
call pagequery('employee', '*', '', 'ID asc', 1, 10, @totalrow);
select @totalrow;
```
注释已经添加在代码中。
阅读全文