Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View schema of resultset in SQL Server Management Studio

Is there any way in Sql Server Management Studio (2008) whereby I can view the data types of each field in the result of a query?

In this case, I am running a stored procedure which returns a result set, and I would like to know the lengths of the nvarchar columns and precision of decimals.

In the past, I have created a view which contains the underlying query in the stored procedure, and then viewed the column list, but the query within the procedure is much too complex to do so in this case.

Any ideas?

like image 573
Neil Moss Avatar asked Jun 02 '11 15:06

Neil Moss


People also ask

How do I find schema in SQL Server Management Studio?

You can get a list of the schemas using an SSMS or T-SQL query. To do this in SSMS, you would connect to the SQL instance, expand the SQL database and view the schemas under the security folder. Alternatively, you could use the sys. schemas to get a list of database schemas and their respective owners.

How do you view the schema of a table in SQL?

To show the schema, we can use the DESC command. This gives the description about the table structure.

How do I view results in SQL Server Management Studio?

Using Shortcut Keys If we just use Ctrl+R we can toggle between showing and hiding the results pane and therefore you can see more of the Editor section when you are using SQL Server Management Studio.


2 Answers

Quick and dirty snippet, requires all the fields in the result set are named or aliased;

select * into #T 
from 
  openrowset('SQLNCLI', 'Server=.;Trusted_Connection=yes;', 'exec thedb.dbo.sp_whatever')
exec('use tempdb exec sp_columns #T drop table #T')
like image 94
Alex K. Avatar answered Oct 27 '22 12:10

Alex K.


Your best bet may be to use OPENROWSET to store the output of the procedure into a table, then examine that table. Something like:

SELECT * INTO YourHoldingTable 
    FROM OPENROWSET('SQLNCLI', 'Server=YourServerName;Trusted_Connection=yes;', 'EXEC YourDatabase.YourSchema.YourProcedureName')
GO

sp_help 'YourHoldingTable'
GO

DROP TABLE 'YourHoldingTable'
GO
like image 31
Joe Stefanelli Avatar answered Oct 27 '22 12:10

Joe Stefanelli