EXECUTE the stored procedure - Using EXECUTE command by adding a WITH RESULT SETS statement.
How to write this?
WITH execute_option
CREATE OR ALTER PROCEDURE dbo.a
AS
SELECT Name FROM sys.tables
GO
EXEC dbo.a WITH RESULT SETS
(
(
Name sysname
)
)
Related
I am creating one store proc that will get some tablename as a parameter and it will do
"select #TableName from #TableName"
But Sybase ASE sql not allowing me to do that. I am getting this message "Cannot select from or insert/update variable '#TableName' because it is not a table variable."
Here is my store proc mentioned below
CREATE PROC Test_result #TableName VARCHAR(40)
as
BEGIN
CREATE TABLE #Results (TableName nvarchar(370))
INSERT INTO #Results select #TableName from #TableName
select * from #Results
END
EXEC Test_result 'sometablename'
This will simulate my actual problem. I want to insert a tablename into a Results table if it match some condition(I haven't mention that here because I don't want to confuse you).
**
**Note: I want to do a quick select query from a TableName which I passed to the store proc.I don't want to create again the table
structure because that store proc may get another tablename whose
table DDL is different **
**
Could anyone provide some alternative or any solution on it ?
Sorry for delay in response. I have found myself a workaround for that which I would like to share.
INSERT INTO #Results select #TableName from #TableName
To make this working, use a variable to store this query and execute using EXEC statement in sybase.
The workaround will be,
BEGIN
SET #sqlquery='INSERT INTO #Results select #TableName from #TableName '
EXEC(#sqlquery)
END
This solved my problem as #tablename variable we can't directly used to replace the value of a table.
If the objective is to insert the value of #TableName into #Results then either of the following should suffice:
INSERT INTO #Results select #TableName
INSERT INTO #Results values (#TableName)
If the intent is to insert #TableName into #Results but only if there's a user table with this name in the current database then try:
INSERT INTO #Results select name from sysobjects where type = 'U' and name = #TableName
If this doesn't answer the question then please update the question with more details as well as some examples of #TableName values that do and do not work.
I am trying to insert result of dynamic sql into temp table. Important thing is i dont know the column names in advance. As per the SO suggestion the following should work
INSERT into #T1 execute ('execute ' + #SQLString )
also, omit the EXECUTE if the sql string is something other than a procedure.
However this is not working on SQL 2017
CREATE TABLE Documents(DocumentID INT, Status NVARCHAR(10))
INSERT INTO Documents(DocumentID,Status)
VALUES
(1,'Active'),
(2,'Active'),
(3,'Active'),
(4,'Active')
DECLARE #SQLString NVARCHAR(MAX)
SET #SQLString = 'SELECT * FROM Documents'
INSERT into #T1 execute ('execute ' + #SQLString )
I get error `Invalid object name '#T1'.`
Then i tried by omitting execute
INSERT into #T1 execute (#SQLString)
with same error `Invalid object name '#T1'.`
I should be able to do
SELECT * FROM #T1
You cannot do an INSERT INTO without having the table predefined. But what I believe you are asking is to do a SELECT INTO. I am aware of two ways of doing it. The first uses OPENROWSET, but I believe this has some drawbacks for security purposes. You could do the following:
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
SELECT *
INTO #T1
FROM OPENROWSET('SQLNCLI',
'Server=localhost;Trusted_Connection=yes;',
'SELECT * from <YOURDATABASE>.dbo.Documents')
Your second option is to create an inline TVF that will generate the table structure for you. So you could do the following:
CREATE FUNCTION getDocuments()
RETURNS TABLE
AS
RETURN
SELECT * from Documents
GO
SELECT * into #T1 FROM getDocuments()
Simply, I have this SQL statment:
EXEC xp_cmdshell 'tasklist'
can we order or filter the results by using order by or where?
Thanks,
I checked jamietre link, and this is the complete answer:
Create table #MyTempTable
(output varchar(max))
INSERT INTO #MyTempTable
EXEC xp_cmdshell 'tasklist'
select * from #MyTempTable where output like 'ie%' order by output
Thanks for all...
You need to output the results into a temporary table first. This should show you how to do it
Insert results of a stored procedure into a temporary table
not directly. You can insert exec into a temp table or table variable and then sort that though
When running the above query multiple times, you might run into this error: There is already an object named '#MyTempTable' in the database.
To mitigate this you can use a DROP IF EXISTS statement as follows before creating the temp table.
IF OBJECT_ID(N'tempdb..#MyTempTable') IS NOT NULL
BEGIN
DROP TABLE #MyTempTable
END
CREATE TABLE #MyTempTable
(OUTPUT VARCHAR(max))
INSERT INTO #MyTempTable
EXEC xp_cmdshell 'tasklist'
SELECT * FROM #MyTempTable WHERE OUTPUT like 'ie%' ORDER BY OUTPUT
Due to a legacy report generation system, I need to use a cursor to traverse the result set from a stored procedure. The system generates report output by PRINTing data from each row in the result set. Refactoring the report system is way beyond scope for this problem.
As far as I can tell, the DECLARE CURSOR syntax requires that its source be a SELECT clause. However, the query I need to use lives in a 1000+ line stored procedure that generates and executes dynamic sql.
Does anyone know of a way to get the result set from a stored procedure into a cursor?
I tried the obvious:
Declare Cursor c_Data For my_stored_proc #p1='foo', #p2='bar'
As a last resort, I can modify the stored procedure to return the dynamic sql it generates instead of executing it and I can then embed this returned sql into another string and, finally, execute that. Something like:
Exec my_stored_proc #p1='foo', #p2='bar', #query='' OUTPUT
Set #sql = '
Declare Cursor c_Data For ' + #query + '
Open c_Data
-- etc. - cursor processing loop etc. goes here '
Exec #sql
Any thoughts? Does anyone know of any other way to traverse the result set from a stored proc via a cursor?
Thanks.
You could drop the results from the stored proc into a temp table and select from that for your cursor.
CREATE TABLE #myResults
(
Col1 INT,
Col2 INT
)
INSERT INTO #myResults(Col1,Col2)
EXEC my_Sp
DECLARE sample_cursor CURSOR
FOR
SELECT
Col1,
Col2
FROM
#myResults
Another option may be to convert your stored procedure into a table valued function.
DECLARE sample_cursor CURSOR
FOR
SELECT
Col1,
Col2
FROM
dbo.NewFunction('foo', 'bar')
You use INSERT ... EXEC to push the result of the procedure into a table (can be a temp #table or a #table variable), the you open the cursor over this table. The article in the link discusses the problems that may occur with this technique: it cannot be nested and it forces a transaction around the procedure.
You could execute your SP into a temporary table and then iterate over the temporary table with the cursor
create table #temp (columns)
insert into #temp exec my_stored_proc ....
perform cursor work
drop table #temp
I would like to get the out put of a stored proc call and create a permanent table with out specifying columns. Because the stored proc return lots of columns...
So.. how can I do this??
SELECT * INTO MYTABLE FROM MYSTOREDPROC #PARAM1 = 1, #PARAM2 = 'HELLO'
is it possible??
Example would help me alot
you can't generate the table from calling stored procedure. However, edit the called stored procedure and alter the select that generates the result set to have an INTO YourNewTable clause:
SELECT
columns....
INTO YourNewTable
from ...
where
order by...
Run the procedure 1 time, to generate the table. remove the changes (INTO YourNewTable clause) and then call the procedure as:
INSERT INTO YourNewTable
EXEC YourProcedure params...