January 22, 2009 at 8:17 pm
Hi,
I would like to get all records with any column has empty value in a table. I understand that by using the following SQL will return the result.
SELECT * FROM table WHERE col1 = '' OR col2 = ''
But my problem is, the table has about 100 fields. Is there a faster way to do this instead of specifying all the fieldname?
If there is, can it be applied on table variable?
Any advice would be much appreciated, thanks.
January 22, 2009 at 9:06 pm
here's a cursor based solution that finds all char/varchar columns for a given table, then queries each for empty /nulls, aqnd provides the results, along with the query to find the data.
[font="Courier New"]
ALTER PROCEDURE EMPTYSEARCH
-- EXEC EMPTYSEARCH 'WorldAirports2006'
@WHICHTABLE VARCHAR(50)
AS
SET NOCOUNT ON
DECLARE @SQL VARCHAR(500),
@TABLENAME VARCHAR(60),
@COLUMNNAME VARCHAR(60)
CREATE TABLE #RESULTS(TBLNAME VARCHAR(60),COLNAME VARCHAR(60),SQL VARCHAR(600))
SELECT
SYSOBJECTS.NAME AS TBLNAME,
SYSCOLUMNS.NAME AS COLNAME,
TYPE_NAME(SYSCOLUMNS.XTYPE) AS DATATYPE
INTO #FKFINDER
FROM SYSOBJECTS
INNER JOIN SYSCOLUMNS ON SYSOBJECTS.ID=SYSCOLUMNS.ID
WHERE SYSOBJECTS.XTYPE='U'
AND SYSOBJECTS.NAME = @WHICHTABLE
AND TYPE_NAME(SYSCOLUMNS.XTYPE) IN ('VARCHAR','NVARCHAR','CHAR','NCHAR')
ORDER BY TBLNAME,COLNAME
DECLARE C1 CURSOR FOR
SELECT TBLNAME,COLNAME FROM #FKFINDER ORDER BY TBLNAME,COLNAME
OPEN C1
FETCH NEXT FROM C1 INTO @TABLENAME,@COLUMNNAME
WHILE @@FETCH_STATUS <> -1
BEGIN
--SET @sql = 'SELECT ''' + @TABLENAME + ''' AS TABLENAME,''' + @COLUMNNAME + ''' AS COLUMNNAME,* FROM ' + @TABLENAME + ' WHERE ' + @COLUMNNAME + ' LIKE ''%' + @WHICHTABLE + '%'''
SET @SQL = 'IF EXISTS(SELECT * FROM [' + @TABLENAME + '] WHERE ISNULL([' + @COLUMNNAME + '],'''') ='''') INSERT INTO #RESULTS(TBLNAME,COLNAME,SQL) VALUES(''' + @TABLENAME + ''',''' + @COLUMNNAME + ''','' SELECT * FROM [' + @TABLENAME + '] WHERE ISNULL([' + @COLUMNNAME + '],'''''''') ='''''''''') ;'
PRINT @SQL
EXEC (@SQL)
FETCH NEXT FROM C1 INTO @TABLENAME,@COLUMNNAME
END
CLOSE C1
DEALLOCATE C1
SELECT * FROM #RESULTS
[/font]
Lowell
January 22, 2009 at 9:17 pm
Using a cursor in 2k5 when VARCHAR(MAX) is available? Lowell? :hehe:
--Jeff Moden
Change is inevitable... Change for the better is not.
January 22, 2009 at 9:28 pm
ok...i'll bite...how do you find each column that hass null/empty string as a report without a cursor?
at least in this case, i only saw dynamic sql and a cursor....i can't envision a single statement to do this, especially with the @WhichTable requirement.
is it possible, Jeff or are you just pork chopping me for no reason?
I admit i was lazy and tweaked a previous example, but still.
Lowell
January 22, 2009 at 9:39 pm
wait, i think i get it....still use a loop or FOR XML to construct the complete set of statements, and run the massive varchar(max) as a batch, instead of line by line...i'm properly chastised now.
Lowell
January 22, 2009 at 9:43 pm
I agree that dynamic SQL is the way to go here. You could concatenate the same dynamic SQL you have in your cursor using the ol' SELECT @sql = ISNULL(@SQL+CHAR(10),'') + bigstringyouused FROM sametableyoudid where @sql is a VARCHAR(MAX). Or, you could do it in a single INSERT with a bazillion OR's, again formed in a similar fashion.
Heh... and no... you're one of the good guys... no pork chops for the sake of slinging them.
Despite my feelings about them, the cursor isn't really a bad idea for something like this. Compared to the rest of the code, the performance impact of the cursor will be near nil. I just worry about some newbie seeing that a good guy used a cursor and that'll give 'em reason to use them anytime they can't think of something else.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 22, 2009 at 11:44 pm
Lowell (1/22/2009)
wait, i think i get it....still use a loop or FOR XML to construct the complete set of statements, and run the massive varchar(max) as a batch, instead of line by line...i'm properly chastised now.
Sorry about my other post. Didn't see this one while I was typing. And, yes Sir, you've got it! 🙂 Except that you don't need to use a loop.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 2, 2009 at 9:29 pm
Thanks Lowell, I managed to get an idea based on your sample.
😀
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply