declare @maxId int
set @maxId=0;
DECLARE @TempREFER TABLE(tablename varchar(100),colname varchar(100))
INSERT @TempREFER(tablename,colname) select d.name as tablename,a.name as colname from syscolumns a
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
and COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1
select * from @TempREFER;
declare @tempId int,@RowCount int,@Tname varchar(100),@CName varchar(100),@row int
set @row=1;
set @tempId=0;
set @RowCount=(select Count(*) from @TempREFER);
print @RowCount;
declare @sql nvarchar(200);
WHILE @row<=@RowCount
begin
SET ROWCOUNT @row
select @Tname = tablename,@CName=colname from @TempREFER;
set @sql=N'select @a=MAX('+@CName+') from '+@Tname;
print @sql;
exec sp_executesql @sql,N'@a int output',@tempId output
print 'id'+Convert(varchar(20),@tempId);
set @row=@row+1;
if @tempId>@maxId
begin
set @maxId=@tempId;
end
end
select @maxId
/****** 获取执行sql语句的返回值 ******/
declare @num int,@sqls nvarchar(4000)
set @sqls='select @a=MAX(UID) from SN'
exec sp_executesql @sqls,N'@a int output',@num output
select @num