Solved

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

Posted on 2016-07-20
7
69 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 69

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 69

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now