I have a select statement that returns a table full of SELECT statements (it goes through each column in each table and creates a selection to determine if this column contains any bad data).
I need to take this table with SELECT statements, execute them, and see if any of them return rows. If count (*)> 0, then I want to print some data.
I thought I should have used a cursor, but I have no idea how to do this.
Here is my code to get the amount of bad data.
SELECT 'SELECT count(*), '' '+sysobjects.name + ' - ' + syscolumns.name + ' '' FROM [' +sysobjects.name + '] WHERE UNICODE(SUBSTRING(['+syscolumns.name+'],Len(['+syscolumns.name+']),1)) = 0' FROM sysobjects JOIN syscolumns ON sysobjects.id = syscolumns.id JOIN systypes ON syscolumns.xtype=systypes.xtype WHERE sysobjects.xtype='U' and systypes.name IN ('varchar', 'nvarchar') ORDER BY sysobjects.name,syscolumns.colid
This returns a table with rows like:
SELECT count(*), ' All_MW_Users - LastName ' FROM [All_MW_Users] WHERE UNICODE(SUBSTRING([LastName],Len([LastName]),1)) = 0
I need to make this selection, and if count (*)> 0, then print the second column. I do not want to show anything in the results or messages, unless there is data to show.
sql sql-server sql-server-2005 dynamic-sql
Martin
source share