Stored Procedures – SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Wed, 09 May 2012 11:11:18 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 How to call or execute a Stored Procedure from inside a Select Statement https://sqlserverlearner.com/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/ https://sqlserverlearner.com/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/#comments Wed, 09 May 2012 11:11:18 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/ How to call or execute a Stored Procedure from Select Statement in SQL Server?

It is really simple to call a stored procedure from a select statement Using OPENROWSET.

Below TSQL Query calls the procedure sp_who from the select statement.

[sql]
SELECT * FROM
OPENROWSET(‘SQLNCLI’
,’Server=(local);Trusted_Connection=Yes;Database=Master’
,’EXEC dbo.sp_Who’)
[/sql]

Syntax:
[sql]
SELECT * FROM
OPENROWSET(‘SQLNCLI’
,’Server=(local);Trusted_Connection=Yes;Database=Master’
,’EXEC [procedurename]’)
[/sql]

Additional Information:
The data that is coming from the procedure can also be filtered in the where clause.

[sql]
SELECT * FROM
OPENROWSET(‘SQLNCLI’
,’Server=(local);Trusted_Connection=Yes;Database=Master’
,’EXEC dbo.sp_Who’)
where dbname = ‘master’
[/sql]

The data from the execution of a select statement can be directly pushed into a table.

[sql]
SELECT * INTO who2_table FROM
OPENROWSET(‘SQLNCLI’
,’Server=(local);Trusted_Connection=Yes;Database=Master’
,’EXEC dbo.sp_Who’)
where dbname = ‘master’
[/sql]

If the openrowset is disabled in the system, learn how to enable it using this link enable openrowset

Related to SQL Server 2005,SQL Server 2008,SQL Server 2012

]]>
https://sqlserverlearner.com/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/feed/ 2