Solved

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

Posted on 2016-07-20
7
75 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

830 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