?
Solved

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

Posted on 2016-07-20
7
Medium Priority
?
92 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

771 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