Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-25
4
Medium Priority
?
78 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 25

Accepted Solution

by:
chaau earned 1000 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 52

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

824 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