Executing a stored procedure from the fields list - sql

ALTER PROCEDURE [dbo].[HO_GetListOfLeaguesPerLocation]
AS
BEGIN
SELECT DISTINCT LeagueID, (EXEC dbo.HO_GetLeagueNumOfMatches LeagueID)
FROM Games
END
I am getting an error:
Incorrect syntax near the Keyword EXEC.
When I run this query alone, without anything else, it goes well.
I also tried:
SELECT DISTINCT LeagueID, dbo.HO_GetLeagueNumOfMatches(LeagueID)
AND:
SELECT DISTINCT LeagueID, SELECT * FROM dbo.HO_GetLeagueNumOfMatches(LeagueID)
AND:
SELECT DISTINCT LeagueID, SELECT dbo.HO_GetLeagueNumOfMatches(LeagueID)
None worked.
Any idea how should I run this query?

You need to use a (scalar) function for this. Stored procedures cannot be used here.
(You can not use APPLY with a stored procedure.)

Related

Execute stored procedure before select

I am trying to reset my random number seed before executing a SELECT FROM. However, every time I run it, I have to run both statements separately. Ideally, I would like to have the following work:
BEGIN
exec dbms_random.seed(6);
SELECT * FROM myTable
ORDER BY dbms_random.value()
END
I get an error Encountered the symbol DBMS_RANDOM when expecting one of the following :=.(#%; however if I only run exec dbms_random.seed(6); it works.
EXEC[UTE] is a SQLPLUS command, you can not use it into a PLSQL block.
Also, you need an INTO to use a SELECT query within a PLSQL block. If your query can give more than one row, you woud have to use a BULK COLLECT INTO.
Your code could be something like:
DECLARE
something myTable%rowtype;
BEGIN
dbms_random.seed(6);
SELECT *
INTO something
FROM myTable
ORDER BY dbms_random.value();
END;
However, I do not recommend using things like select *; it would be better to explicitly write the columns you need to get.
Also what's the use of an order by in a select into statement which is defined to only return 1 row?
It seems you want to select 1 random row of many which needs a cursor like so:
DECLARE
CURSOR something_cur IS
SELECT *
FROM myTable
ORDER BY dbms_random.value();
something_rec something_cur%ROWTYPE;
BEGIN
dbms_random.seed(6);
OPEN something_cur;
FETCH something_cur INTO something_rec;
CLOSE something_cur;
END;
/

I cannot execute sql statment dynamically which has into clause

I am executing this sql statement dynamically in oracle using EXECUTE IMMEDIATE statement. But when I Do this I get an error 'missing keyword'. I have declare the RULECOUNT variable as NUMBER. When I remove the INTO statement, the sql statement appears to get executed properly.
SELECT COUNT(DISTINCT RULE_ID) INTO RULECOUNT FROM(
SELECT
distinct a.RULE_ID, Rule_Name, Applicability,
Rule_Type, KPI_NAME, BT, DT, Authorised_User,
Rule_Date_of_Creation
from vw_rule_detail_search a WHERE a.Applicability = 'No' order by a.BT
desc);
I don't know what is happening, can anyone good in oracle help me find what I am missing.
I found solution to my problem. I should not have used INTO statement in select statement while executing with execute immediate.
I should have used like this
EXECUTE IMMEDIATE statement INTO RuleCount;

How to select query result inside stored procedure

I'm writing a stored procedure in SQL Server 2008 which contains a SELECT DISTINCT statement and another simple Select statement which is based on the result of first statement.
How to use the table returned by the SELECT DISTINCT statement i.e the UnitNumber column value in second Select statement?
Stored procedure:
CREATE PROCEDURE ExtractPacket
AS
BEGIN
SET NOCOUNT ON;
-- Select statements to check the number of unit
SELECT DISTINCT UnitNumber from dbo.CP_TemplateHandler
END
GO
you can create a temp table and fill it by first SELECT DISTINCT and then in second Select use that.
Excuse me for answer in order comment(i can comment yet :( )
I suggest that use First Select Distinct as a sub query of second Select Distinct query.

Odd Stored Procedure error - syntax?

I am trying to create a simple stored procedure in SQL Server 2005, and am puzzled by a syntax error I am getting.
Both tables have identical structure. tbl_Users_non_61 is empty and ready to receive the data.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Make_WEEKLY_SUMMARY
AS
BEGIN
SET NOCOUNT ON;
Select (tbl_Users.UserID
, tbl_Users.RegistrationType
, tbl_Users.Datecompleted)
INTO tbl_Users_non_61
FROM
SELECT tbl_Users.UserID
, tbl_Users.RegistrationType
, tbl_Users.Datecompleted
FROM tbl_Users;
END
GO
Resulting error:
Msg 102, Level 15, State 1, Procedure Make_WEEKLY_SUMMARY, Line 5
Incorrect syntax near ','.**
Since you just want to insert records into one table from another, there is a cleaner syntax:
INSERT INTO tbl_Users_non_61(UserId, RegistrationType, DateCompleted)
SELECT u.UserID, u.RegistrationType, u.Datecompleted
FROM tbl_Users AS u
To answer your question though, you do not need the ( ) around your first select, but you DO need them around your sub select, then you need to alias the sub-query you are referencing with the FROM:
Select t.UserID, t.RegistrationType, t.Datecompleted
INTO tbl_Users_non_61
FROM
(
SELECT u.UserID, u.RegistrationType, u.Datecompleted
FROM tbl_Users AS u
) as t;
Try this on:
SELECT
tbl_Users.UserID
, tbl_Users.RegistrationType
, tbl_Users.Datecompleted
INTO tbl_Users_non_61
FROM tbl_Users;
When you SELECT ... INTO, if the columns come from one table only, there's no need to SELECT those same columns again. Even if you had to take data from multiple columns, you still wouldn't need to reselect them, and perform a join instead.

SQL Server 2005: Call a stored procedure from a WHERE clause

I need to make a SELECT with a call of a stored procedure in the WHERE clause.
It should be something like that....
SELECT distinct top 10 i.x, d.droit
FROM v_droit d, v_info i
WHERE d.nomdroit='yy'
AND i.id<>2
AND (select val from (exec up_droits(i.x, d.droit)) <>3
But it does not work...
Any idea?
Don't say to replace the stored procedure with a function because is not possible to use the existing code in a function. So the function is not a valid option. I really need to be able to use a stored procedure
This is achieved by first executing the stored procedure, capturing the output into a #temp table or a #tabel variable, then running your query against the table. Something like this:
declare #droits_table (val ,... );
insert into #droits_table
exec up_droits(param, param);
SELECT distinct top 10 i.x, d.droit FROM v_droit d, v_info i WHERE d.nomdroit='yy' AND i.id<>2 AND (select val from #droits) <>3
Of course this will not work for you because the up_droits needs the i.x and d.droit parameters from the query. This indicates that your stored procedure should probably be a a view or table valued function.
Sorry but, make it a table valued function rather than stored procedure.
Eg:
Scalar - SELECT id, name FROM test WHERE id < (SELECT dbo.mytestfunction())
Table - SELECT id, name FROM test WHERE id = (SELECT col1 from dbo.mytestfunction())
You can't. The content of the WHERE clause must be a search expression.
Is the reason that the code doesn't work as a function because it modifies some data? If so, then you're out of luck, functions used in where clauses must be immutable.
If the stored procedure doesn't modify any data, you may be able to wrap it inside of a function.
If you are on SQL Server I don't think you can do what you propose.
But one thing you can do is build dynamic queries, but be careful doing it because they open up many interesting problemareas.
The syntax is :
EXEC #<query>
But anotherthing you can do, which is probably much better for you, is to make the up_droits function deliver it's results in a temp table, if you select into a #table it is temporary for the duration of your function/procedure scope
declare procedure up_droits() as
select val .. into #temp
So what you do is create a procedure
create procedure Top10FromDroit
begin
exec up_droits
SELECT distinct top 10 i.x, d.droit FROM v_droit d, v_info i WHERE d.nomdroit='yy' AND i.id2 AND (select val from (#temp) 3
Hopefully that will give you the results you want to achieve.
If at first you don't succeed, code around it^^
Could anyone of you explain reasons for executing dynamic SQl inside stored procedure. I know very few situations when you need them - but really very few. 99.9% (or 999 of a 1000) of execute strings could be rewritten as normal sql statements with parameters.
The very same is with Selects that have functions inside select or where clauses.
Try to think about your sets of data, not about procedural ways how to solve it.