Solved

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

Posted on 2016-07-20
7
85 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 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

739 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