Question: So I have a database with many tables that have a column that contains a GL Account value (for financial purposes). The column name varies by table (i.e. in one table the column is called "gldebitaccount" and in another table it's called "glcreditaccount"). I was able to find all combinations of table / column pairs using the following query:

```
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%gl%acc%'

```

This query returns close to 100 pairs of tables/columns. I am trying to find any value in any of those table/column pairs that exceeds 25 chars in length. For an individual table/column, I'd typically use:

```
SELECT *
FROM tableName
WHERE LEN(columnName)>25

```

I want to avoid having to run that query 100 times with each pair. Is there any way I can do a "for each" (which I know is frowned upon in SQL since everything should be set-based). I've done sub-SELECT statements before, but not any that involved change the table in the FROM clause. Any ideas or help would be greatly appreciated!

Thanks in advance!

Answer: 
As the previous answer, the solution will need dynamic SQL. Here is a way that uses both dynamic SQL and cursors, and you can expect slow performance, so use at your own risk:

```
DECLARE @TableName NVARCHAR(128), @ColumnName NVARCHAR(128) 
DECLARE @Query NVARCHAR(4000)

DECLARE CC CURSOR LOCAL FAST_FORWARD FOR
SELECT QUOTENAME(t.name), QUOTENAME(c.name)
FROM sys.columns c
INNER JOIN sys.tables t 
    ON c.object_id = t.object_id
WHERE c.collation_name IS NOT NULL
AND c.max_length > 25 AND c.name LIKE '%gl%acc%';

CREATE TABLE #Results(TableName NVARCHAR(128), ColumnName NVARCHAR(128));

OPEN CC
FETCH NEXT FROM CC INTO @TableName, @ColumnName
WHILE @@FETCH_STATUS = 0
BEGIN
    SET @Query = 'IF EXISTS(SELECT 1 FROM '+@TableName+'
                            WHERE LEN('+@ColumnName+') > 25)
                  INSERT INTO #Results
                  VALUES(@TableName,@ColumnName)'
    EXEC sp_executesql  @Query, 
                        N'@TableName NVARCHAR(128),@ColumnName NVARCHAR(128)', 
                        @TableName, 
                        @ColumnName;
    FETCH NEXT FROM CC INTO @TableName, @ColumnName
END
CLOSE CC
DEALLOCATE CC

SELECT *
FROM #Results

```
I wasn't sure if you needed to do anything with the results, but this will return the records that meet the criteria you posted in your question

```
Declare @TableName sysname
Declare @ColName sysname
Declare @dynamic_SQL varchar(MAX)

Declare some_cursor CURSOR FOR
SELECT c.name AS ColName, t.name AS TableName
FROM sys.columns c
    JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%gl%acc%'

OPEN some_cursor
FETCH NEXT FROM some_cursor INTO @ColName, @TableName

WHILE @@FETCH_STATUS = 0
Begin
    select @dynamic_SQL = '
    Select *
    From ' + @TableName + '
    Where LEN('+ @ColName +') > 25
    '
    exec (@dynamic_SQL)

    FETCH NEXT FROM some_cursor INTO @ColName, @TableName
End

CLOSE some_cursor
DEALLOCATE some_cursor

```