Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-09-25
4
Medium Priority
?
75 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

715 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