{"id":195,"date":"2012-05-09T11:11:18","date_gmt":"2012-05-09T11:11:18","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/05\/09\/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement\/"},"modified":"2012-05-09T11:11:18","modified_gmt":"2012-05-09T11:11:18","slug":"how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/05\/09\/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement\/","title":{"rendered":"How to call or execute a Stored Procedure from inside a Select Statement"},"content":{"rendered":"
How to call or execute a Stored Procedure from Select Statement in SQL Server?<\/strong><\/p>\n It is really simple to call a stored procedure from a select statement Using OPENROWSET.<\/p>\n Below TSQL Query calls the procedure sp_who from the select statement.<\/p>\n [sql] Syntax: Additional Information:<\/strong> [sql] The data from the execution of a select statement can be directly pushed into a table.<\/p>\n [sql]
\nSELECT * FROM
\n OPENROWSET(‘SQLNCLI’
\n ,’Server=(local);Trusted_Connection=Yes;Database=Master’
\n ,’EXEC dbo.sp_Who’)
\n[\/sql]<\/p>\n
\n[sql]
\nSELECT * FROM
\n OPENROWSET(‘SQLNCLI’
\n ,’Server=(local);Trusted_Connection=Yes;Database=Master’
\n ,’EXEC [procedurename]’)
\n[\/sql]<\/p>\n
\nThe data that is coming from the procedure can also be filtered in the where clause.<\/p>\n
\nSELECT * FROM
\n OPENROWSET(‘SQLNCLI’
\n ,’Server=(local);Trusted_Connection=Yes;Database=Master’
\n ,’EXEC dbo.sp_Who’)
\nwhere dbname = ‘master’
\n[\/sql]<\/p>\n
\nSELECT * INTO who2_table FROM
\n OPENROWSET(‘SQLNCLI’
\n ,’Server=(local);Trusted_Connection=Yes;Database=Master’
\n ,’EXEC dbo.sp_Who’)
\nwhere dbname = ‘master’
\n[\/sql]<\/p>\n