[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

SQL Adding & Editing Proc Resultset

I need to call a stored proc & both add a column to the resultset and rename the columns. Is there a cleaner way to do this apart from dumping the resultset in a temp table or variable table & writing a select off of that?
0
Lensolo
Asked:
Lensolo
  • 4
  • 3
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>I need to call a stored proc
exec the_proc_name

> both add a column to the resultset and rename the columns.
Explain in non-technical terms what exactly you're trying to pull off here, and more important why.
0
 
LensoloAuthor Commented:
I have to write a stored proc that bridges 2 systems. It calls a vendor written (unchangeable) stored proc and has to customize the resulting recordset to return it to another process that will call it. The changes are simple. I have to rename 3 columns & add a new column with a static value.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
I'm sure there are multiple ways to pull this off, but the one that comes to mind is:
Create a #temp table with the columns named exactly how the SP returns it
Execute code like this to insert the results of the SP into that temp table
INSERT INTO #tmp (col1, col2, col3)
exec the_proc_name

Open in new window

ALTER #tmp to change the column names, add new column, then UPDATE #tmp with new column's static value
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LensoloAuthor Commented:
Thanks. My question above is: "Is there a cleaner way to do this apart from dumping the resultset in a temp table or variable table & writing a select off of that? "

There may not be. I am just trying to find out if there is. If no one posts anything I'll accet your response as the solution.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>My question above is:
Missed that.  You could play with something like this...
SELECT col1, col2, col3 as looky_here_a_new_column_name, column4, 'banana' as new_column_name
FROM EXEC the_proc_name

Open in new window

0
 
LensoloAuthor Commented:
That sounds ideal, buit I can't get it to work.
Create proc dbo.p_Test
As
Select 1 as Col1, 2 as Col2, 'Bananas' as Col3
GO

select Col1,Col2,Col3
from exec p_Test

Tells me "incorrect syntax near exec". and other tests with select * from exec p_test and various similar combinations aren't fruitful at the moment.
0
 
LensoloAuthor Commented:
I'll close this. Thank for your help Jim Horn. I really appreciate it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now