I got stored function which returns query. In ASP project I got GridView which I bind to SqlDataSource element (it named SqlDataProjectWells).
So, when I try to call it there are error appears which says something like
"error while trying to execute query"
But in pgAdmin select command works perfectly
Code calls on page load
void bindToTable(){
SqlDataProjectWells.SelectCommand = "SELECT get_zonetable()";
SqlDataProjectWells.Select(DataSourceSelectArguments.Empty);
myGridView.DataBind();
}
Stored procedure code
CREATE OR REPLACE FUNCTION get_ZoneTable()
RETURNS SETOF RECORD
AS
$$
BEGIN
return QUERY SELECT "WELLS".well_name, "ZONES".id_zones, "ZONES".top, "ZONES".botom FROM "WELLS" LEFT JOIN "ZONES" ON "WELLS".well_id = "ZONES".id_well;
/*RETURN;*/
--return result_record;
END
$$ LANGUAGE plpgsql;
Whats wrong?!
UPD:
If I use stored procedure - there are such error appears
ERROR [0A000] Error while executing the query
If I use select like this
SqlDataProjectWells.SelectCommand = "SELECT \"WELLS\".well_name, \"ZONES\".id_zones, \"ZONES\".top, \"ZONES\".botom FROM \"WELLS\" LEFT JOIN \"ZONES\" ON \"WELLS\".well_id = \"ZONES\".id_well;"
It caused error which says
A field or property with the name 'id_well' was not found on the
selected data source
Shouldn't you be calling DataBind on the GridView instead of the SqlDataSource?
Well, kinda solved problem this way. Stored procedure creates all_zones view which I use aftel in ASP code like SqlDataProjectWells.SelectCommand = "SELECT * FROM all_zones";
Stored procedure text
CREATE OR REPLACE FUNCTION get_zones_view()
RETURNS void AS
$BODY$
DECLARE
BEGIN
execute 'CREATE OR REPLACE VIEW "all_zones" AS SELECT "WELLS".well_name, "ZONES".id_zones, "ZONES".top, "ZONES".botom FROM "ZONES" JOIN "WELLS" ON "WELLS".well_id = "ZONES".id_well';
END;
$BODY$
LANGUAGE plpgsql
but I assume that it's not okay to do like that
Related
I don't know how to pass the custom table names (eg prepAccTN) so that the SP gets executed w/o ERROR 1146 (42S02): Table '[my-schema-name].prepacctn' doesn't exist
Tried executing it like so
CALL Bill_UpdateUnbalancedPrepaidAccounts('correct_table_name', 'boo', 3);
CALL Bill_UpdateUnbalancedPrepaidAccounts("correct_table_name", 'boo', 3);
CALL Bill_UpdateUnbalancedPrepaidAccounts("my-schema-name.correct_table_name", 'boo', 3);
All failed!
CREATE PROCEDURE Bill_UpdateUnbalancedPrepaidAccounts(prepAccTN VARCHAR(100), wtrTN VARCHAR(100), prepAccId INT)
BEGIN
UPDATE prepAccTN SET `unbalanced` = (
SELECT SUM(`chargesecs`) FROM wtrTN WHERE `prepaidaccount` = prepAccId AND `prepaccsettl` = 0
) WHERE `id` = prepAccId;
END//
DELIMITER ;
If you really need to pass ther table name as a parameter to a stored procedure you need to use prepared statements. You cannot use the parameter directly in a query.
Tried out the prepared statements but unfortunately they cannot be used within the stored procedure! So I do not use a SP now and put the whole necessary code (back) into the trigger. Not the best and preferred way but it's ok to me and my little sql code I have to execute
I just start using apex from few time. So I hope you'll forgive me if I ask things that are very simple.
I have a select list populated by a SQL Query, my SQL instruction is SELECT NUM, ID FROM TABLE.
I'd like to change the query dynamically adding a " WHERE NUM LIKE %myVar%", where "MyVar" is the test of a text Item, so I'd like to change the content of the select list pressing the button.
Is it possible?
thanks in advance for any answer.
I find a partial solution. I bind to select list a PL/SQL function returning a SQL Script and I add an text item called filter.
My function is:
declare
q varchar2(4000);
begin
q:='select numero, ';
q:=q||'id from t_doc ';
q:=q||'where numero = :FILTROPT';
return q;
end;
But if I use like with a percent in the function instead of "=", apex raise me an error.
Any suggestion?
I have a store procedure which show single row value like
(x.CLTotalAc, x.CLTotalAmt,x.CLAc, x.CLAmt,x.CL,
y.GB_CLTotalAc,y.GB_CLTotalAmt, y.GB_CLAc, y.GB_CLAmt, y.GB_CL).
I want these value to my asp.net C# page under text box (txtCLTotalAmt,
txtCL, txtGB_CLTotalAmt, txtGB_CL)
Store Procedure
ALTER proc [dbo].[Cr_CL_Report]
#ProductName text,
#BranchName text
as
begin
Select x.CLTotalAc,x.CLTotalAmt,x.CLAc, x.CLAmt,x.CL,
y.GB_CLTotalAc,y.GB_CLTotalAmt, y.GB_CLAc, y.GB_CLAmt, y.GB_CL
from
(
SELECT COUNT(dbo.DDBranchName.BCode) AS BrCode,
dbo.TblTotalCL.CLProductName AS ProductName,
SUM(dbo.TblTotalCL.CLTotalAc) AS CLTotalAc,
SUM(dbo.TblTotalCL.CLTotalAmt) AS CLTotalAmt,
SUM(dbo.TblTotalCL.CLAc) AS CLAc, SUM(dbo.TblTotalCL.CLAmt) AS CLAmt,
SUM(dbo.TblTotalCL.CLAmt)/SUM(dbo.TblTotalCL.CLTotalAmt) * 100 AS CL
FROM dbo.DDBranchName INNER JOIN
dbo.TblTotalCL ON dbo.DDBranchName.BCode = dbo.TblTotalCL.CLbrCode
WHERE (dbo.TblTotalCL.CLAsOnDate IN
(SELECT MAX(CLAsOnDate) AS Expr1 FROM dbo.TblTotalCL AS TblTotalCL_1))
AND (dbo.TblTotalCL.CLProductName LIKE #ProductName)
AND (dbo.DDBranchName.BCode LIKE #BranchName)
GROUP BY dbo.TblTotalCL.CLProductName
) x
inner join
(
SELECT COUNT(dbo.DDBranchName.BCode) AS GB_BrCode,
dbo.TblTotalCL.CLProductName AS GB_ProductName,
SUM(dbo.TblTotalCL.CLTotalAc) AS GB_CLTotalAc,
SUM(dbo.TblTotalCL.CLTotalAmt) AS GB_CLTotalAmt,
SUM(dbo.TblTotalCL.CLAc) AS GB_CLAc,
SUM(dbo.TblTotalCL.CLAmt) AS GB_CLAmt,
(SUM(dbo.TblTotalCL.CLAmt) / SUM(dbo.TblTotalCL.CLTotalAmt))*100 AS GB_CL
FROM dbo.DDBranchName INNER JOIN
dbo.TblTotalCL ON dbo.DDBranchName.BCode = dbo.TblTotalCL.CLbrCode
WHERE (dbo.TblTotalCL.CLAsOnDate IN
(SELECT MAX(CLAsOnDate) AS Expr1 FROM dbo.TblTotalCL AS TblTotalCL_1))
AND (dbo.TblTotalCL.CLProductName LIKE #ProductName)
GROUP BY dbo.TblTotalCL.CLProductName
) y on
x.ProductName = y.GB_ProductName
end
Blockquote
how can I solve this?
Getting return value from stored procedure in C#
In the link you can see how to get the values form a stored procedure. Then assign the value to the text box
Start by reading up on Ado.net. Use for instance the example here,under adonet data provider examples/sqlclient. When you have this working you have retrieved the data from the database. (imho avoid table adapters, entity framework and linqtosql if you want to grok it. instead go with manually writing for SqlCommand. or dapper to ease some of the burden.)
Now you have to get it to the web page. First decide on Webforms or Aspnetmvc. Then googlewithbing what the syntax is like.
Wash.
Rinse.
Repeat.
HTH
Current select statement:
SELECT *
FROM vw_svc200_open
WHERE (CUSTNMBR = '::CUSTNMBR::')
ORDER BY ::sortcolumn::
This works and all is well. But now I need to modify this select string to apply an extra filter.
SELECT *
FROM vw_svc200_open
WHERE
CASE
WHEN ::CUSTNMBR:: = 'ABC123'
THEN (CUSTNMBR = '::CUSTNMBR::' AND CNTCPRSN = '::CNTCPRSN::')
ELSE (CUSTNMBR = '::CUSTNMBR::')
END
ORDER BY ::sortcolumn::
So basically I need to have my select filter on customer number and if customer number is ABC123 then I also need it to filter on Contact person... The problem with the second SELECT (using the CASE statement) is that it throws an "error near =" on the THEN line.
The ::CUSTNMBR:: and ::CNTCPRSN:: are url string variables (What are those called again?).
Ex.
www.mywebsite.com/mypage.asp?Custnmbr=ABC123
Am I going to have to add some logic to the asp page (i.e. IF/Then) to set a variable, and then pass that variable to the *fp_sQry=* line?
I ended up using nested if statements to set the select statement.
When executing the following code, it just says the procedure is completed and doesn't print the infomation i want it to (firstName, lastName) and then the other values from the select query in a table below.
CREATE OR REPLACE PROCEDURE PRINT_ACTOR_QUOTES (id_actor char)
AS
CURSOR quote_recs IS
SELECT a.firstName,a.lastName, m.title, m.year, r.roleName ,q.quotechar from quote q, role r,
rolequote rq, actor a, movie m
where
rq.quoteID = q.quoteID
AND
rq.roleID = r.roleID
AND
r.actorID = a.actorID
AND
r.movieID = m.movieID
AND
a.actorID = id_actor;
BEGIN
FOR row IN quote_recs LOOP
DBMS_OUTPUT.PUT_LINE('a.firstName' || 'a.lastName');
end loop;
END PRINT_ACTOR_QUOTES;
/
When setting server output on, I get
a.firstNamea.lastName
a.firstNamea.lastName
a.firstNamea.lastName
a.firstNamea.lastName
multiple times!
What is "it" in the statement "it just says the procedure is completed"?
By default, most tools do not configure a buffer for dbms_output to write to and do not attempt to read from that buffer after code executes. Most tools, on the other hand, have the ability to do so. In SQL*Plus, you'd need to use the command set serveroutput on [size N|unlimited]. So you'd do something like
SQL> set serveroutput on size 30000;
SQL> exec print_actor_quotes( <<some value>> );
In SQL Developer, you'd go to View | DBMS Output to enable the DBMS Output window, then push the green plus icon to enable DBMS Output for a particular session.
Additionally, assuming that you don't want to print the literal "a.firstNamea.lastName" for every row, you probably want
FOR row IN quote_recs
LOOP
DBMS_OUTPUT.PUT_LINE( row.firstName || ' ' || row.lastName );
END LOOP;
Ensure that you have your Dbms Output window open through the view option in the menubar.
Click on the green '+' sign and add your database name.
Write 'DBMS_OUTPUT.ENABLE;' within your procedure as the first line.
Hope this solves your problem.
Set Query as below at first line
SET SERVEROUTPUT ON
this statement
DBMS_OUTPUT.PUT_LINE('a.firstName' || 'a.lastName');
means to print the string as it is.. remove the quotes to get the values to be printed.So the correct syntax is
DBMS_OUTPUT.PUT_LINE(a.firstName || a.lastName);
For SQL Developer
You have to execute it manually
SET SERVEROUTPUT ON
After that if you execute any procedure with DBMS_OUTPUT.PUT_LINE('info'); or directly .
This will print the line
And please don't try to add this
SET SERVEROUTPUT ON
inside the definition of function and procedure, it will not compile and will not work.
In Oracle SQL Developer, you can follow steps by steps as the below image:
I am using Oracle SQL Developer,
In this tool, I had to enable DBMS output to view the results printed by dbms_output.put_line
You can find this option in the result pane where other query results are displayed.
so, in the result pane, I have 7 tabs. 1st tab named as Results, next one is Script Output and so on. Out of this you can find a tab named as "DBMS Output" select this tab, then the 1st icon (looks like a dialogue icon) is Enable DBMS Output. Click this icon. Then you execute the PL/SQL, then select "DBMS Output tab, you should be able to see the results there.
All of them are concentrating on the for loop but if we use a normal loop then we had to use of the cursor record variable. The following is the modified code
CREATE OR REPLACE PROCEDURE PRINT_ACTOR_QUOTES (id_actor char)
AS
CURSOR quote_recs IS
SELECT a.firstName,a.lastName, m.title, m.year, r.roleName ,q.quotechar from quote q, role r,
rolequote rq, actor a, movie m
where
rq.quoteID = q.quoteID
AND
rq.roleID = r.roleID
AND
r.actorID = a.actorID
AND
r.movieID = m.movieID
AND
a.actorID = id_actor;
recd quote_recs%rowtype;
BEGIN
open quote_recs;
LOOP
fetch quote_recs into recs;
exit when quote_recs%notfound;
DBMS_OUTPUT.PUT_LINE(recd.firstName||recd.lastName);
end loop;
close quote_recs;
END PRINT_ACTOR_QUOTES;
/