SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;', 'set fmtonly off; exec sp_who') AS aAs this is like a regular SELECT statement, data merge (JOIN, UNION), filter condition (WHERE) and sorting (ORDER BY) can be used. The result may also be inserted into a table using the INSERT INTO...SELECT statement or to create a new table by including a INTO new_table> statement on the SELECT statement. The new_table may also be a temporary table. Here's another sample. Although the following code may be implemented in some other ways, it was created to illustrate how the stored procedure be executed inside a SELECT statement and return the result. Here's the stored procedure:
create proc tproc (@dbname as varchar(50)) as begin declare @sql varchar(500) set @sql = 'select * from ' + @dbname + '.dbo.sysobjects' exec (@sql) end exec dbo.tproc 'model' exec dbo.tproc 'master'To call in a SELECT statement and return as a table:
SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;', 'set fmtonly off; exec workdb.dbo.tproc ''master''') AS a; SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;', 'set fmtonly off; exec workdb.dbo.tproc ''model''') AS a;As always, comments are welcome ....
~~CK
No comments:
Post a Comment