I'm trying to run the following Dynamic SQL statement:
@Tbl, @Fld, and @LookupValue have all been set according to Table to search, Field (Or Column) to search and column value to compare.
DECLARE @Sql AS VARCHAR(500)
SET @Sql = 'SELECT COUNT(*)
FROM ' + @Tbl +
' WITH (NOLOCK)
WHERE ' + @Fld + ' = ''' + @LookupValue + ''''
EXEC(@Sql)
I want to store the result into a variable so I can check to see if there are any returned rows. This statement is in the middle of a WHILE construct that is checking several tables and fields.
If records are found, then I want to display:
SET @Sql = 'SELECT ' + @Fld +
' FROM ' + @Tbl +
' WITH (NOLOCK)
WHERE ' + @Fld + ' = ''' + @LookupValue + ''''
EXEC(@Sql)
NOLOCKtable hint in MySQL.