Groups | Blog | Home
all groups > sql server new users > august 2005 >

sql server new users : Loop thru each column of a table


rocky20
8/29/2005 2:22:19 PM
Is there a way to loop thru each column of a specific table?

What I'm trying to do is get the datalength of each row of a specific table.
I was using the method of SELECT DATALENGTH(col1) + DATALENGTH(col1) FROM
table but I ran into the problem of nulls being in the table, so the output
was always NULL.

I thought if I could loop through each column for each row and determine if
it was NULL or not. If it was I could check the data type and use that to
add to my total.

Thanks

Stu
8/29/2005 6:15:12 PM
I'm not sure of a way to easily determine the datatype of a given
column (without going to the system tables), but if you know what each
of your columns are, you can use the COALESCE function to do this for
you:

SELECT COALESCE(DATALENGTH(col1), 4) +
COALESCE(DATALENGTH(col2), 8) +....

etc..

Stu
AddThis Social Bookmark Button