Solved

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

Posted on 2014-09-25
4
69 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 47

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

809 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