Solved

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

Posted on 2014-09-25
4
74 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 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 51

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

635 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