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()
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 have a procedure where I want to bring some data of one table (A) to an other (B), updating Bs rows if some of the columns are equal and inserting a new row, if this is not the case.
Afterwards I want to delete all of those rows from table A (leaving the ones which were not queried from the select statement and those which were added through another process between the update/insert and delete statement of the procedure).
For that use case I want to store the result of a select query in a variable and then working insert and update on it before I delete every entry from the result.
Thanks for your help.
Martin
UPDATE:
I did the following:
CREATE DEFINER=`martin`#`%` PROCEDURE `validateData`()
BEGIN
select * from write_data as temp_write_data;
UPDATE read_data AS r
LEFT JOIN
temp_write_data as w
ON [...]
SET [...]
WHERE [...];
INSERT INTO read_data AS r
SELECT [...]
FROM temp_write_data
WHERE [...];
DELETE write_data.* FROM write_data
LEFT JOIN temp_write_data
using(guid)
WHERE temp_write_data.somecolumn is not null;
end//
But that does not work, I fear.
SOLUTION:
I used:
CREATE TEMPORARY TABLE IF NOT EXISTS temp_write_data AS (SELECT * FROM write_data);
and
DROP TEMPORARY TABLE temp_write_data;
instead of a variable. That seems to work.
You might want to look into existing Object Relational Mapping tools if this is for a long term project. If you just need a quick and dirty way to save the results of the query you can either write your own Data Value Object (i.e. an object that represents a single row of data) and create lists of these objects (i.e. the variable representing the data is a List of Row objects). Or you could be even more streamlined about it and store the data returned by the query in a list of maps. In Java this would be something like List<Map<String,Object>>. In this case the Map represents a single row, the string represents the name of the column and the Object represents the value for that row. The list represents the collection of rows.
SOLUTION:
I used:
CREATE TEMPORARY TABLE IF NOT EXISTS temp_write_data AS (SELECT * FROM write_data);
and
DROP TEMPORARY TABLE temp_write_data;
instead of a variable. That seems to work.
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.
So far I have tried many different ways of accessing the data on three tables using a stored procedure. First I tried a simple select statement :
create procedure reportCodes () begin
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
end$$
(names changed to protect the guilty)
Running the stored procedure in phpmyadmin results in the first instance of the record (1 out of two ‘true’ in the test database). Running just:
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
in the phpmyadmin SQL tab returns both records. Then I tried a temp table:
create procedure reportCodes () begin
CREATE TEMPORARY TABLE used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id);
SELECT * FROM used_numbers; end$$
Returns 1 of 2 records as the procedure but both records in console. Finally I tried changing my table to a join:
CREATE PROCEDURE reportCodes()
begin
create temporary table used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers JOIN numOwner
ON Numbers.id=numOwner.Numbers_id
JOIN location ON
numOwner.Numbers_id=location.Numbers_id
WHERE Numbers.used = true
);
SELECT * FROM used_numbers; end$$
Same results as above. I’m at a loss as to why running just the SQL would show both test records but running the procedure with the exact same code only yields one.
Thanks
in your query, numOwners isn't a valid table being selected against, so something's wrong. Have you tried running your SQL in the Query window in phpMyAdmin to ensure that the EXACT same query is returning 2 rows?
I presume the "Owner" table is supposed to be "numOwner", so I've re-written the stored procedure call below. Also, I'm not sure what types of values you're storing in Numbers.used to evaluate to "TRUE". I will presume you're using a TINYINT(1), so I've altered that, as well. I hope this helps.
DELIMITER $$
USE `db`$$
DROP PROCEDURE IF EXISTS `reportCodes`$$
CREATE PROCEDURE `reportCodes`()
BEGIN
SELECT
n.serial_numb,
o.lName,
o.fName,
o.email,
l.long,
l.lat,
n.dateValidated
FROM Numbers n
INNER JOIN numOwner o ON n.id=o.Numbers_id
INNER JOIN location l ON n.id=l.Numbers_id;
WHERE n.used = 1
END$$
DELIMITER ;
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).