USE [ANKA] GO /****** Object: StoredProcedure [piyango].[up_FazlaCikanSayilar] Script Date: 10/14/2011 17:30:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [piyango].[up_FazlaCikanSayilar] WITH EXECUTE AS CALLER AS --truncate table piyango.FazlaCikanSayilar -- Declare the variables to store the values returned by FETCH. DECLARE @Tname char(50) DECLARE @ColumName char(100) DECLARE @SchemaName char(10) DECLARE @string varchar(1024) DECLARE Tname_cursor CURSOR FOR SELECT s.name, OBJECT_NAME(ic.OBJECT_ID) AS TableName, COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName FROM sys.indexes AS i INNER JOIN sys.index_columns AS ic ON i.OBJECT_ID = ic.OBJECT_ID AND i.index_id = ic.index_id AND COL_NAME(ic.OBJECT_ID,ic.column_id) not like ('key%') INNER JOIN sys.tables as T on t.object_id=ic.object_id INNER JOIN sys.schemas S on s.schema_id=t.schema_id WHERE i.is_primary_key = 1 and t.is_replicated>0 ORDER BY i.name OPEN Tname_cursor; -- Perform the first fetch and store the values in variables. -- Note: The variables are in the same order as the columns -- in the SELECT statement. FETCH NEXT FROM Tname_cursor INTO @SchemaName,@Tname,@ColumName -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN set @string= 'select MAX('+@ColumName+')'+@Tname+ ' from '+@SchemaName+''+'.'+''+@Tname+'' execute (@string) ---- Concatenate and display the current values in the variables. --insert into piyango.hede (Column_Name) --Values (@Tname) -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM Tname_cursor INTO @SchemaName,@Tname,@ColumName END CLOSE Tname_cursor; DEALLOCATE Tname_cursor;