一个存储过程实现如下要求:
列出当前数据库下,所有内容记录不为空(就是有记录)的用户表表的内容。
比如:
use database
create proc list_table_record
as
declare @count int
select @count=count(*) from 用户表--得到用户表总数
if @count>0
begin
while @count>0
begin
select * from 当前用户表 where (select count(*) from 该表)>0(就是保证该表中有记录),依次下去,取出所有用户表的记录内容
set @count=@count-1
下一个用户表
end
end类似上面的要求,请高手帮忙解答。不胜感谢。
列出当前数据库下,所有内容记录不为空(就是有记录)的用户表表的内容。
比如:
use database
create proc list_table_record
as
declare @count int
select @count=count(*) from 用户表--得到用户表总数
if @count>0
begin
while @count>0
begin
select * from 当前用户表 where (select count(*) from 该表)>0(就是保证该表中有记录),依次下去,取出所有用户表的记录内容
set @count=@count-1
下一个用户表
end
end类似上面的要求,请高手帮忙解答。不胜感谢。
取当前用户表select @a = count(*) from sysobjects where xtype='U'
循环
while @i < @a
begin
set @i = @i + 1
end
declare @cur cursor---游标
declare @i int--数目
declare @s varchar(20)--表名
declare @sql varchar(100)--查询语句
set @cur=cursor LOCAL SCROLL for select name from sysobjects where xtype='U'
open @cur
set @i= @@CURSOR_ROWSfetch next from @cur into @s
while @@FETCH_STATUS=0
begin
set @sql='select * from '+@s
exec (@sql)
fetch next from @cur into @s
end
declare @i int--数目
declare @s varchar(20)--表名
declare @sql varchar(100)--查询语句
set @cur=cursor LOCAL SCROLL for select name from sysobjects where xtype='U'
open @cur--打开游标
set @i= @@CURSOR_ROWS
select @i as 表数目
fetch next from @cur into @s
while @@FETCH_STATUS=0
begin
set @sql='select * from '+@s
exec (@sql)
fetch next from @cur into @s
end
close @cur
DEALLOCATE @cur
如果我希望结果中含有每个表的名字就是把name字段读出来,应该怎么写?因为你的代码能够得出答案,但是我不知道是那个表的内容
creat proc re_count
@s varchar (20)
as
declare @sql varchar(200)
set @sql='select count(*) from'+@s
exec(@sql)
go
就是先判断一下这个表是否有记录,如果有在显示,如何返回我写的这个存储过程的count值呢?
set @sql='select @a=count(*) from tableName '
exec sp_executesql @sql,N'@a int output',@num output
select @num
declare @i int--数目
declare @s varchar(20)--表名
declare @sql nvarchar(100)--查询语句
declare @num int
set @cur=cursor LOCAL SCROLL for select name from sysobjects where xtype='U'
open @cur--打开游标
set @i= @@CURSOR_ROWS
select @i as 表数目
fetch next from @cur into @s
while @@FETCH_STATUS=0
begin
set @sql=N'select @a=count(*) from '+@s
exec sp_executesql @sql,N'@a int output',@num output
if @num>0
begin
select @s 表名
set @sql=N'select * from '+@s
exec (@sql)
end
fetch next from @cur into @s
end
close @cur
DEALLOCATE @cur
1.set @sql=N'select @a=count(*) from '+@s
大写的N'是什么意思?
2.exec sp_executesql @sql,N'@a int output',@num output
这句话是什么意思?
如上语句 因为前边声明的unicode 字符串 所以赋值的时候要加上N'sp_executesql是一个执行sql语句的存储过程
语法
sp_executesql [ @stmt = ] stmt
[
{, [@params=] N'@parameter_name data_type [ OUT | OUTPUT ][,...n]' }
{, [ @param1 = ] 'value1' [ ,...n ] }
]
其中@stmt 就是那个语句
@stmt参数要用unicode 字符串