I wish to do the following:
select * into tmptbl from sometable
EXCEPT 'sometable' is a stored procedure that returns a result set AND editing the stored procedure to suit my goal is not an option. ALSO i may or may not know the columns and types of what the procedure returns.
Basically i am looking for a proper way of doing this:
select * into tmptbl from exec someSP
Is this even possible, if so, how?
yes it is possible with a loopback query like this
SELECT * INTO #tmptbl
FROM OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;'
,'set fmtonly off exec DatabaseName.dbo.someSP')
More example here: Store The Output Of A Stored Procedure In A Table Without Creating A Table
Be aware that this has to be turned on first, see here: How to enable xp_cmdshell and Ad Hoc Distributed Queries on SQL Server 2005
Related
I've seen in Oracle SQL that there is something called SYS_REFCURSOR which is used in a SP to get a recordset from a table
I want to do the same thing in SQL Server so I can a SP to get a recordset of a specific table. I've searched a lot looking for similar solutions but all what I've seen are all related to Oracle SQL.
Any suggestions please?
P.S. To be more clear, what do I want is just using an SP to select all records instead of the normal query.
i.e. instead of using:
SELECT * FROM EMP_DEPARTMENT
use SP to do the same thing.
Assuming you mean stored procedure with SP you can to this :
create procedure dbo.spMyProcedure as
begin
set nocount on
select * from emp_department
end;
than you can do this :
exec dbo.spMyProcedure
Is this what you mean ?
In SQL Server, you can simply encapsulated the SELECT query in a stored procedure (using * only as an example since an explicit column list is the best practice):
CREATE PROC dbo.GetEmployeeDepartments
AS
SELECT * FROM EMP_DEPARTMENT;
GO
The SQL Server client API will stream the result as a fast-forwared read-only resultset, where individual rows can be used.
I have a temp table where I want to store the results of a stored procedure. To execute the stored procedure, I need to use variable values for the Database name, Database Server, and Stored Procedure name that are stored in a table. I need to work out how to write the SELECT * INTO using the OPENROWSET with my variables. This is what I have so far but there are syntax errors:
SELECT * INTO #tmpAccountsRetrieved
FROM OPENROWSET(#TempDbName, 'Server=' + #TempDbServer + ';Trusted_Connection=yes;', 'EXEC ' + #TempStoredProcName)
I'm pretty rusty on SQL so any help is much appreciated!
Try to put these configurations in place before using openRowset:
-- FOR USING OPENROWSETS
EXEC sp_configure 'Ad Hoc Distributed Queries'
,1
RECONFIGURE
I think your syntax is correct.
So just Try to Go through this POST for more help
Is there a way to cause the result set of a SQL Server stored procedure (or any result set, after the fact) to be encoded in XML format?
I want the result set to be encoded in XML as if the FOR XML RAW clause was used during selection.
However the complex stored procedure logic and its internal SELECT statements should not be modified to return XML because the procedure is used for its standard/non-XML result set most of the time.
Update: Emphasis on the fact I'm looking for an answer in the SQL Server environment - the
results should be returned as if SQL Server has directly encoded them itself, as XML, just like it does when using the built-in XML features like the FOR XML clause.
You would insert the data from the SP into a temp table, then select from that FOR XML
This won't work if the SP itself already does a INSERT .. EXEC SPROC because you cannot nest them
Working examples
use tempdb;
create proc giveme
as
select a = 1, b = GETDATE()
union all
select 2, b = '20100101'
Using INSERT.. EXEC
declare #t table (a int, b datetime)
insert #t
exec giveme
select * from #t for xml raw
Using OPENQUERY
exec sp_addlinkedserver 'localhost'
exec sp_serveroption #server = 'localhost'
,#optname = 'DATA ACCESS'
,#optvalue = 'TRUE'
select *
from openquery(localhost, 'exec tempdb..giveme')
for xml raw
You could try using OPENROWSET in cooperation with FOR XML to do the transformation.
By 'after the fact', do you mean still within the SQL Server environment? Or are you talking about a client program?
Within SQL, you could probably write a sproc that acts as a wrapper for your other sprocs, along these lines. The wrapper sproc would handle the FOR XML work.
In .NET, there are a number of ways to do this.
You can try inserting the result set from the stored procedure into a table variable( or temporary table) and selecting the table rows with the FOR XML clause.
Here is an example:
DECLARE #MyDataTable AS TABLE ( col1 int,...., colN int)
Make sure that the #MyDataTable has the same columns as the stored procedure result set(s).
INSERT INTO #MyDataTable
EXECUTE mysp_GetData #param1=value,....,#paramN;
SELECT * FROM #MyDataTable
FOR XML AUTO
Is it possible to do something like this in sql server 2005?
WITH tmpTable AS (EXEC spWhatever)
Or any other way I can query the data returned from the sp? Thanks!!!
Temp table:
CREATE TABLE #foo (col1 int, col2 char(10), ...)
INSERT #foo
EXEC myproc
Or loopback (not sure if this still works). Edit: Could be OPENROWSET as per SQLMenace's answer
SELECT * FROM OPENQUERY (MyServername, 'USE MyDB EXEC myproc')
only with a loopback query if you don't first want to create the table, see here: Store The Output Of A Stored Procedure In A Table Without Creating A Table
example
SELECT * INTO #TempSpWho
FROM OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;',
'set fmtonly off exec master.dbo.sp_who')
SELECT * FROM #TempSpWho
as far as i know you can not. But you can try using User Defined Functions (UDF) instead of SP, if you do that you can you use it like a table.
I had the same question. I solved my situation by changing the SP to a VIEW. Now it acts just like a table. That's fine if you don't need to pass any parameters to the SP.
If you do need to pass parameters, make it a table-valued function.
I have a query in a stored procedure that needs to be executed on different servers and databases according to some parameters.
How can I do this without using neither exec, nor sp_executesql?
I'm using SQL Server 2008.
Thank you.
UPDATE
I've found some links
http://www.eggheadcafe.com/software/aspnet/29397800/dynamically-specify-serve.aspx
http://www.sommarskog.se/dynamic_sql.html
Is using SYNONYM possible solution? If yes, than how?
UPDATE 2
I forgot to mention that all this servers are linked to the server where stored procedure is stored.
UPDATE 3
OPENROWSET or OPENDATASOURCE are not accessible either. I need a solution without building query string concating server name, schema name, db name.
It surely can be done by using if or case in stored procedure, but if we have 37 variations, then it's not a good solution.
Any other suggestions?
Nobody wants to answer, so I'll do it myself, just to have accepted answer.
There's isn't any way to do this. You need to use one of specified suggestions, anyway the query must be generating by concatenating.
Does OPENROWSET or OPENDATASOURCE help?
EDIT: If it works, you can change the database at runtime & execute the query using the present connection. I cannot see any other way of executing query the way you want.
What is wrong with running query using string i.e dynamic query?
/* DO THIS FOR EACH TABLE IN THE PROCEDURE*/
--BEGIN TABLE_1
DECLARE #LINKEDSERVER AS VARCHAR(50)
SET #LINKEDSERVER = DBO.FN_RETURN_SERVER('SBROUBLES')
DROP SYNONYM MYLINKEDSERVER
EXEC (' CREATE SYNONYM MYLINKEDSERVER FOR ' + #LINKEDSERVER + 'ANYDB.DBO.ANYTABLE')
--- END TABLE_1
-- UTILIZATION
SELECT COUNT(*) FROM MYLINKEDSERVER
--AND FN_RETURN_SERVER COULD BE ANY SELECT CASE ON SQL AS WELL