Solved

Query sql table to display column, data type, lenght and value

Posted on 2016-07-20
7
73 Views
Last Modified: 2016-07-21
Is there a way to run a query on a table so it returns the column name, data type, lenght and value of first row ?

firmid |  int | 4  |  3
Name | nvarchar |  30  |   Lopez

To end up with something like that.

The query below gives me pretty much all I need minus the first record values.

SELECT 
    c.name 'Column Name',
    t.Name 'Data type',
    c.max_length 'Max Length',
    c.precision ,
    c.scale ,
    c.is_nullable,
    ISNULL(i.is_primary_key, 0) 'Primary Key'
FROM    
    sys.columns c
INNER JOIN 
    sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN 
    sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN 
    sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
    c.object_id = OBJECT_ID('LawFirm')

Open in new window

0
Comment
Question by:amucinobluedot
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:tliotta
ID: 41722221
Do you need it for a specific table or is it intended to be used against different unknown tables?
...and value of first row ?
What do you mean by the value of a row? Rows don't have values; columns have values. Do you want the value of each column from the first row?

How are you defining "first row"? Depending on an ORDER BY clause, different rows might be the "first row". Of course, without an ORDER BY clause, the first row can't be guaranteed.
0
 

Author Comment

by:amucinobluedot
ID: 41722975
I should have said
the value of each column from the first row
and I may add some 'where' caluse to it like "Where usertype = 'contact'"
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41723048
Nice puzzle this morning!

I have been able to achieve what you want by using a dynamic query:
DECLARE @tableName varchar(10)
SET @tableName = 'Fund'

DECLARE curFields CURSOR FOR
	SELECT 
		c.name AS ColumnName,
		t.Name AS DataType,
		c.max_length AS MaxLength,
		c.precision ,
		c.scale ,
		c.is_nullable,
		ISNULL(i.is_primary_key, 0) AS PrimaryKey
	FROM sys.columns c
	INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
	LEFT JOIN sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
	LEFT JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
	WHERE c.object_id = OBJECT_ID(@tableName)

DECLARE @ColumnName VARCHAR(100)
DECLARE @DataType VARCHAR(100)
DECLARE @MaxLength INT
DECLARE @Precision INT
DECLARE @Scale INT
DECLARE @IsNullable INT
DECLARE @PrimayKey INT 

OPEN curFields
FETCH NEXT FROM curFields INTO @ColumnName, @DataType, @MaxLength, @Precision, @Scale, @IsNullable, @PrimayKey

DECLARE @sql NVARCHAR(max)
SET @sql = ''

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @sql = @sql + CASE WHEN @sql = '' THEN '' ELSE ' UNION ALL 
' END 
+ 'select ''' + @ColumnName + ''' as ColumnName, ''' + @DataType + ''' as DataType, ''' + CAST(@MaxLength AS VARCHAR) + ''' as MaxLength, ''' + CAST(@Precision AS VARCHAR) + ''' as Precision, ''' + CAST(@Scale AS VARCHAR) + ''' as Scale, ''' + CAST(@IsNullable AS VARCHAR) + ''' as IsNullable, ''' + CAST(@PrimayKey AS VARCHAR) + ''' as PrimayKey, (SELECT TOP 1 cast(' + @ColumnName + ' as varchar(max)) FROM ' + @tableName + ') '

	FETCH NEXT FROM curFields INTO @ColumnName, @DataType, @MaxLength, @Precision, @Scale, @IsNullable, @PrimayKey
END 

CLOSE curFields
DEALLOCATE curFields

print @sql 
EXEC (@sql)

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Closing Comment

by:amucinobluedot
ID: 41723062
Thx!
0
 

Author Comment

by:amucinobluedot
ID: 41723065
Where can I add a 'where' clause of the row data ... like
'where usertype = 'contact'  ?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41723091
would be in the FROM clause:

FROM ' + @tableName + ' where usertype = ''contact'') '

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41723158
Perfect. Thank you !!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question