I'm trying to write a query to select the results of a stored procedure into a temp table. However the stored procedure has been set up so that when it runs its returns 1 result with no alias. (See below)
obviously I get an error when I try and select the data into a temp table.
An object or column name is missing or empty. For SELECT INTO
statements, verify each column has a name. For other statements, look
for empty alias names. Aliases defined as "" or [] are not allowed.
Change the alias to a valid name.
Is there any way round this as I will be unable to update the procedure to output an alias! Basically Im after a way of doing a
SELECT * INTO #tmptable
FROM OPENROWSET ('SQLNCLI', 'Server=ServerName;Trusted_Connection=yes;','SET FMTONLY OFF EXEC sp_name')
If you know how many columns you will get back from the OPENROWSET, then you can create the temporary table before inserting values; this allows you to give the columns a name.
CREATE TABLE #tmptable (Value INT NOT NULL)
INSERT #tmptable
SELECT * FROM OPENROWSET ('SQLNCLI','Server=ServerName;Trusted_Connection=yes;','SET FMTONLY OFF EXEC sp_name')
-- DROP TABLE #tmptable
If you do not know how many columns you are returning... I do not know that it is possible.
Related
I am trying to insert values from one table to another one using the following insert sql query
INSERT INTO [dbo].[table2]
SELECT Exec [StoredProcedure],
[Column1]
,[Column2]
FROM [dbo].[table1]
[table2] has the following columns:
RecNo, <-- INT
Column1, <--VARCHAR(50)
Column2 <--VARCHAR(50)
[StoredProcedure] main purpose is that every time a new row to be inserted in table2 it selects the maximum value from RecNo from table2 and adds 1 to that number to create the next number (sequential).
Here is the script for the stored procedure .
GO
ALTER PROCEDURE [dbo].[UpdateRcnoNumbers]
#MaxRcno INT OUTPUT
AS
BEGIN
SELECT #MaxRcno=MAX(Recno) FROM [table2]
SELECT #MaxRcno=#MaxRcno+1
RETURN #MaxRcno
END
But I am getting an error and I am not able to call the stored procedure ? Any suggestion please .
Thank you in advance
You should write a function for this purpose , read here
The problem is, Stored Procedures don't really return output directly. They can execute select statements inside the script, but have no return value.
MySQL calls stored procedures via CALL StoredProcedureName(); And you cannot direct that output to anything, as they don't return anything (unlike a function).
Here
MySQL Call Command
I want to execute, in a stored procedure, a certain set of statements if, in table my_table there is exactly one row with value value in column column_name. I have tried the following, but I get a syntax error:
IF ((SELECT COUNT(*) FROM my_table WHERE column_name = value) = 1) THEN
BEGIN
END;
END IF;
For context: In my procedure I create a temporary table at some point, where I store a list of values. Then later on in the procedure, I want to check if a given value is present in that temporary table.
I think you might be better to structure it more like this
BEGIN
DECLARE myCOUNT INTEGER;
SELECT COUNT(*)
INTO myCount
FROM my_table
WHERE column_name=value;
IF (myCount = 1) THEN
-- do stuff
END IF;
END;
I'm not sure what you are trying to do, but I'll guess an "upsert" -- update a record if it exists, otherwise insert a new record.
In any case, if you are trying to ensure that name is unique in my_table, then this is not the right approach at all. Instead, declare a unique index/constraint so the database ensures the data integrity:
create unique index unq_my_table_name on my_table(name);
You can then use insert . . . on duplicate key update to modify the records in the database.
I have a routine that is called using
SET #p0='55'; SET #p1='-6'; SET #p2='100'; CALL `distanceSearch`(#p0, #p1, #p2);
Basically i pass in latitude, longitude and a distance to search for users, e.g 100 miles. The routine creates a temp table and inserts results into it. When i execute it, a result set is returned.
When i try execute it like this i run into a syntax error
SET #p0='55'; SET #p1='-6'; SET #p2='100';
select foo.* from (CALL `distanceSearch`(#p0, #p1, #p2)) as foo
What am i doing wrong? How do use the results from this to join on another table?
NO, you can't perform a select from procedure like that way but as you said The routine creates a temp table and inserts results into it
in that case if you already know the temporary table name then do a SELECT from that table
select * from temporary_table_name
Else, convert your routine to a table valued function rather than a stored procedure and then you can say
select * from fn_runRoutine_Job()
Im using SQL Server 2008. I have a result set from the stored procedure and i want to fire some more queries on the result set for e.g order by desc /asc and some querying. So what is the best way to do it. Using views or by using OPENQUERY.
Please help.
You can save results of stored procedure calls in any table or table variable that has same number and type of fields as the result set returned by the stored procedure, for example:
CREATE TABLE #temp(col1 INT, col2 VARCHAR(10))
INSERT INTO #temp(col1, col2)
EXEC usp_Proc1(#param1)
SELECT *
FROM #temp
Condition is that usp_Proc1 returns rows consisting of columns of type INT and VARCHAR(10) (in that order).
Is it possible to get stored procedure multiple result set to temp table variable?
I wrote a stored procedure like,
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name from Table1 where column1=#param1;
Select Id, Age, Address from Table2 where column1=#param2;
End
When i execute this sp it will return 2 tables(record set's).
Now, i want to get this 2 record sets to 2 temp table variables.
How can i achieve this?
I don't believe a stored procedure can return multiple result sets like you want it to. What I might suggest is to have the stored proc store the results in two global temp tables and then have the calling process (whatever is calling your stored proc) query the two global temp tables and put the results into your temp table variables.
I know this is an old question, and maybe I am misunderstanding what you want, but if you just want to take those two recordsets and put them into temp tables, couldn't you do this:
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name
into #temp1
from Table1 where column1=#param1;
Select Id, Age, Address
into #temp2
from Table2 where column1=#param2;
End
now, I dont know what you're trying to achieve with this, but depending on how you're calling that sp, the #temp tables may not be accessible from outside of the sp.