Solved

PROC to return table field names

Posted on 2014-11-23
9
222 Views
Last Modified: 2014-11-23
Table1        <-- starting with a table like this
===============================
Field1   Field2    Field3       etc.
-------    --------      ---------
x             x              x
x             x              x
x             x              x

The date in this table to be ignored. The return expected is:

FieldNames
============
Field1
Field2
Field3
etc.


Question: How to write a Proc or function to return the field names in Table1 as shown above?
0
Comment
Question by:Mike Eghtebas
  • 4
  • 4
9 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 250 total points
Comment Utility
Use INFORMATION_SCHEMA.COLUMNS system view for this:
@sql = 'select COLUMN_NAME 
from information_schema.columns 
where table name = ''' + @YourTable + 
''' order by ORDINAL_POSITION'
EXEC @sql

Open in new window

0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
wonderful. Is it a good idea to make a function or a proc, performance-wise?
0
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Depends on what you plan to do with it.
Functions can be used in-line in select statements, procedures cannot. But procedures perform better.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
performance-wise?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Like I said, procedures perform better. They're faster than functions.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
Comment Utility
Koen Van Wielink,
Like I said, procedures perform better.
Sorry, my bad.

chaau,
I ran this PROC, I got the following error::
Msg 2812, Level 16, State 62, Procedure spGetFieldNames, Line 14
Could not find stored procedure 'select COLUMN_NAME
from information_schema.columns
where table name = 'Search' order by ORDINAL_POSITION'.



ALTER PROCEDURE  [dbo].[spGetFieldNames]
@TableName varchar(100)

As
Begin
 
Declare @sql varchar(max);

-- I tried Select also
 Set @sql= 'select COLUMN_NAME 
from information_schema.columns 
where table name = ''' + @TableName + 
''' order by ORDINAL_POSITION'
exec @sql

end

Open in new window

0
 
LVL 12

Assisted Solution

by:Koen Van Wielink
Koen Van Wielink earned 250 total points
Comment Utility
There's an underscore missing in the where clause:

where table_name = ''' + @TableName + 

Open in new window


When working with dynamic SQL, try to print your generated statements before executing them. If you get an error, you can copy/paste the printed statement to see where things go wrong.

declare @TableName varchar(100)

select @TableName = 'WorkOrder'
 
Declare @sql varchar(max);

-- I tried Select also
 Set @sql= 'select COLUMN_NAME 
from information_schema.columns 
where table name = ''' + @TableName + 
''' order by ORDINAL_POSITION'
--exec @sql
print @sql

Open in new window

0
 
LVL 12

Expert Comment

by:Koen Van Wielink
Comment Utility
Oh, and you need to place () around the @sql variable in the exec statement:

exec (@sql)

Open in new window

0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
Comment Utility
Thank you.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 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

9 Experts available now in Live!

Get 1:1 Help Now