I'm trying to print the list of fields in each schema and table dynamically. since I need a print for further references not a table, I used a cursor to print them out, as follows:
DECLARE AutoFields CURSOR
FOR
SELECT TABLE_SCHEMA,TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
OPEN AutoFields
DECLARE @TableSchema NVARCHAR(100)
DECLARE @TableName NVARCHAR(100)
FETCH NEXT
FROM AutoFields
INTO @TableSchema,@TableName
WHILE @@FETCH_STATUS = 0
BEGIN
EXECUTE('
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA=@TableSchema
AND TABLE_NAME=@TableName
')
FETCH NEXT
FROM AutoFields
INTO @TableSchema,@TableName
END
CLOSE AutoFields
DEALLOCATE AutoFields
But i get this error over and over again:
Must declare the scalar variable "@TableSchema".
What am I missing here?