Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL Server 2012, how do I get the column name and data type from a view, function, or stored procedure?

I am able to obtain column names and data types from a database table with:

SELECT COLUMN_NAME, DATA_TYPE 
FROM information_schema.columns
WHERE TABLE_NAME = 'xxx'

How can I get the column name and data type from a view, function, or stored procedure? I imagine I'd have to obtain them using the results of each but I'm unsure.

Thanks for the help

like image 297
Carlos Mendieta Avatar asked Oct 25 '25 12:10

Carlos Mendieta


1 Answers

Columns for view:

SELECT * FROM sys.columns c where c.object_id = OBJECT_ID('<schema>.<view name>')

Columns for table valued function:

SELECT * FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS rc WHERE rc.TABLE_NAME = '<udf name>'

Columns for stored procedure

For SQL server 2012 and later:

SELECT name, system_type_name
FROM sys.dm_exec_describe_first_result_set_for_object
(
  OBJECT_ID('<shcema>.<sp name>'), 
  NULL
);

Taken from Retrieve column names and types of a stored procedure?. Read answers there for possible ways to do this (for example with pre 2012).

like image 120
tobypls Avatar answered Oct 28 '25 01:10

tobypls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!