简体   繁体   中英

Check if a Stored Procedure returns only one column and one row

In this query :

DECLARE @TempPerformance TABLE(FeatureTitle VARCHAR(50), FeatureText VARCHAR(50))

INSERT INTO @TempPerformance 
EXEC [usp_GetPerfomance] @Value

It some times gives 2 columns and sometime one column only..It does not work when it returns only 1 column as the temporary table is not designed so..

How can I differentiate between these 2 results.

EDIT

If the SP fails..it returns one column - one row as :

Standard
--------
No Records Found

There is one way. Using OPENROWSET you can get the result of stored procedure into dynamicly created (temp) table. More about it in this question . There could be issues with security and login problems.

Afterwards you can easily check for number of columns in your temp table using sys.columns

SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local);Trusted_Connection=yes;',
        '[usp_GetPerfomance] ' + CAST(@Value AS VARCHAR(MAX)));

SELECT COUNT(*) FROM tempdb.sys.columns WHERE object_id=object_id('tempdb..#MyTempTable');

You can use OPENROWSET to execute a query and get the result as a table. Then you can use that table to verify whether you should handle it one way or the other.

If you go to the link and look at the section:

SELECT a.*
FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
     'SELECT GroupName, Name, DepartmentID
      FROM AdventureWorks.HumanResources.Department
      ORDER BY GroupName, Name') AS a;

then you can replace the SELECT with the EXEC yourDatabase.Schema.Procedure.

It however requires you to reconfigure the server to Ad Hoc Queries, and you might not have the proper rights to do that. It's generally not a solid solution for a production environment, so I'd not personally recommend the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM