An existing full-Library query on the Web that does not recognize the database structure of a custom schema:
Declare @str nvarchar(Ten)Declare @tablename varchar( -)Declare @colname varchar( -)Declare @counts intDeclare @sql nvarchar( -)--Define variables aboveDeclareCur1cursor for SelectA.name Tablename,b.name colname fromsys.objects A,syscolumns bwhereA.object_id=b.ID andA.type_desc='user_table'--Defining CursorsSet @str='Sea' --set the character to findOpenCur1--Open CursorFetch Next fromCur1 into @tablename,@colname while(@ @Fetch_Status = 0)--LoopsBeginSet @sql=N'Select @counts =count (*) from' + @tablename + 'where CHARINDEX (" "+@str+" ",'+@colname+') >0'execsp_executesql@sqlN'@counts int Output',@countsOutput--Execute Dynamic SQLif @counts>0 --determine if a specific character exists for a field in a tablebeginPrint @tablename+','+@colname --Print table name and field nameEndFetch Next fromCur1 into @tablename,@colnameEndCloseCur1--Close CursorsdeallocateCur1--Releasing Cursors
Once you have added schema support, there are occasional errors that can be queried for the schema:
Declare @str nvarchar(Ten)Declare @schemasname varchar( -)Declare @tablename varchar( -)Declare @colname varchar( -)Declare @counts intDeclare @sql nvarchar( -)--Define variables aboveDeclareCur1cursor for SelectS.name schemasname,a.name Tablename,b.name colname fromsys.objects a,syscolumns b,sys.schemas swhereA.object_id=b.ID anda.schema_id=s.schema_id andA.type_desc='user_table'--Defining CursorsSet @str='pollution' --set the character to findOpenCur1--Open CursorFetch Next fromCur1 into @schemasname,@tablename,@colname while(@ @Fetch_Status = 0)--LoopsBeginSet @sql=N'Select @counts =count (*) from' + @schemasname + '.'+ @tablename + 'where CHARINDEX (" "+@str+" ",'+@colname+') >0'execsp_executesql@sqlN'@counts int Output',@countsOutput--Execute Dynamic SQLif @counts>0 --determine if a specific character exists for a field in a tablebeginPrint @schemasname+','+@tablename+','+@colname --Print table name and field nameEndFetch Next fromCur1 into @schemasname,@tablename,@colnameEndCloseCur1--Close CursorsdeallocateCur1--Releasing Cursors
SQL Server full library query with schema