想读取每个表中的记录总数储存语句:ALTER PROCEDURE [dbo].[Get_Count]
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
ASDECLARE @TempID int
SELECT @TempID = count(@ID) FROM [@dtable]
IF @TempID = 0
RETURN 0
ELSE
RETURN @TempID但每次提示:对象名 '@dtable' 无效。 把[@dtable] 改成表名就可以正常
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
ASDECLARE @TempID int
SELECT @TempID = count(@ID) FROM [@dtable]
IF @TempID = 0
RETURN 0
ELSE
RETURN @TempID但每次提示:对象名 '@dtable' 无效。 把[@dtable] 改成表名就可以正常
必须声明表变量 "@dtable"。
declare @str varchar(5000);
set @str='DECLARE @TempID int;';
set @str=@str+'SELECT @TempID = count('+@id+') FROM ['+@dtable+'];';
set @str=@str+'IF @TempID = 0 ';
set @str= @str+' RETURN 0;ELSE RETURN @TempID;';
exec @str;
ALTER PROCEDURE [dbo].[Get_Count]
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
ASDECLARE @sql nvarchar(400)
set @sql=N'select count( '+ @ID +') from ' + @dtable
declare @count int;
exec sp_executesql @sql,'@count int output',@count output
if @count<0
begin
print '无记路'
end
else
begin
print @count
end
ps:在表名 或字段名作变量时候 要拼接语句 执行 要得到执行的结果 需调用sp_executesql
ALTER PROCEDURE [dbo].[Get_Count]
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
ASexec'(SELECT count('+@ID+') FROM ['+@dtable+']'
return exec('SELECT count('+@ID+') FROM ['+@dtable+']') 如果这样的话 返回值为0
你执行的时候
exec Get_Count 你要查询的列的名字,表的名字
例如 exec Get_Count ID,table1
你执行的时候
exec Get_Count 你要查询的列的名字,表的名字
例如 exec Get_Count ID,table1
你执行的时候
exec Get_Count 你要查询的列的名字,表的名字
例如 exec Get_Count ID,table1
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
AS
exec('SELECT count(['+@ID+']) FROM ['+@dtable+']')select number,type into TB from master..spt_values exec [dbo].[Get_Count] 'type','TB'
drop proc [dbo].[Get_Count]
drop table TB
修改存储过程ALTER PROCEDURE [dbo].[Get_Count]
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
ASDECLARE @sql nvarchar(400)
set @sql=N'select count( '+ @ID +') from ' + @dtable
declare @count int;
exec sp_executesql @sql,N'@count int output',@count output
if @count<0
begin
print '无记路'
end
else
begin
print @count
end建立测试表CREATE TABLE tb_Menu(
id int NULL,
parentid int NULL,
menuname varchar (50)
)
测试存储过程exec Get_Count 'id',tb_Menu
执行结果
5
好了 楼主结贴吧 ~
这些语句都是写在 sql 数据库里面程序里面只传递2个对应的参数@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
public static int Getcount(string FieldName, string TableName)
{
int rowsAffected;
SqlParameter[] parameters = {
new SqlParameter("@ID", SqlDbType.NVarChar,50),
new SqlParameter("@dtable", SqlDbType.NVarChar,100)};
parameters[0].Value = FieldName;
parameters[1].Value = TableName;
return DbHelperSQL.RunProcedure("Get_Count", parameters, out rowsAffected);
}
怎么调用啊?
using System.Data;
using System.SqlClient;
public static int Getcount(string FieldName, string TableName)
{
SqlParameter[] parameters = {
new SqlParameter("@ID", SqlDbType.NVarChar,50),
new SqlParameter("@dtable", SqlDbType.NVarChar,100)};
parameters[0].Value = FieldName;
parameters[1].Value = TableName;
SqlConnection conn=new SqlConnection("你的数据库连接字符串");
SqlCommand cmd=new SqlCommand("Get_Count",conn);
cmd.CommandType=CommandType.StoreProcedure;
return cmd.ExecuteScalar();
conn.Close();
}pS;你用的那个类库我不知道里面啥结构 所以就用我写的这个吧
using System.SqlClient;
public static int Getcount(string FieldName, string TableName)
{
SqlParameter[] parameters = {
new SqlParameter("@ID", SqlDbType.NVarChar,50),
new SqlParameter("@dtable", SqlDbType.NVarChar,100)};
parameters[0].Value = FieldName;
parameters[1].Value = TableName;
SqlConnection conn=new SqlConnection("你的数据库连接字符串");
SqlCommand cmd=new SqlCommand("Get_Count",conn);
cmd.CommandType=CommandType.StoreProcedure;
int result=Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
return result;
}
@ID nvarchar(50), --表的主键
@dtable nvarchar(100) --表名
AS Declare @sql nvarchar(100) begin
declare @TempID int
set @sql=N'select @n=count('+@ID+') from '+@dtable
exec sp_executesql @sql,N'@n int output',@n=@TempID output
if @TempID=0
return 0
else
return @TempID
end