Solved

MS SQL - Using Stored Procedure in Select of another Stored Procedure?

Posted on 2014-09-25
4
66 Views
Last Modified: 2014-09-27
This is all I'm finding so far and it doesn't work, don't have permissions. This is much easier to do in Oracle.

Q. Is there a better way?

Select  column1,
            column2,
            (SELECT * FROM OPENROWSET('SQLNCLI','Server=(local);Trusted_Connection=Yes;Database=Database1','EXEC Get_Roles(272389)')) as [Roles]

From ....
0
Comment
Question by:WorknHardr
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 250 total points
ID: 40345346
Use OPENQUERY instead:
Select  column1,
            column2,
            (SELECT * FROM OPENQUERY(LOCALSERVER,'EXEC Database1.dbo.Get_Roles(272389)')) as [Roles]
From .... 

Open in new window

But it is not a better way. First of all it requires a loopback setup to the same server:
EXEC sp_addlinkedserver @server = 'LOCALSERVER',  @srvproduct = '',
                        @provider = 'SQLOLEDB', @datasrc = @@servername

Open in new window

Secondly, using it a new connection is generated.
This and a dozen other methods is thoroughly discussed here
I personally recommend creating a user defined function, or a view because a Stored Procedure is by design is intended for different uses
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 40345511
Strange. If server is local and you are using trusted connection, you don't need the OPENROWSET at all. Just give the full path to the object:
Select  column1,
             column2,
             (SELECT * FROM Database1.dbo.Get_Roles(272389)') as [Roles]
 From .... 

Open in new window

0
 

Author Comment

by:WorknHardr
ID: 40347089
I would like to use a Function, but I cannot get it to return a 'For XML Path' object.

I basically query some tables and group with a CTE then return a long string of characters, it's not actual xml format

Maybe you can give me a tip on how to make this SP a Function?

[Current SP]
ALTER PROCEDURE [dbo].[Get_Roles] 
(
    @ContactID int
)
As
....
;With CTE as (
Select [Name], [Affiliation], ROW_NUMBER() OVER (PARTITION BY [Name] ORDER BY [Name]) AS RN
From #Affiliations 

)
Select Reverse(Stuff(Reverse(Substring((SELECT [Name] + [Affiliation] + '; ' + CHAR(10)
							FROM CTE
                                                        Where RN = 1
							For XML PATH(''), elements), 1, 1000)), 1, 3, '')) as [Affiliations]

Open in new window

0
 

Author Closing Comment

by:WorknHardr
ID: 40347690
I agree that a Function is the answer, thx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 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

17 Experts available now in Live!

Get 1:1 Help Now