want to display zero when entered something in PL SQL - plsql

hi guys I am looking for a query that I want to display M0321 when I entered M321
for eg: if I entered M321 and I want to display it as M0321

I think you can use LPAD to add 0 before your number. here is my solution:
select SUBSTR('M321',1,1) || LPAD(SUBSTR('M321',2),4,'0') from dual

Related

MS access query not finding table and field

I have been working with access for many years and writing queries.
Now I am hitting a wall. My query is simple and has 2 tables.
Table No.1 "fraud_IP_Blocks_country" and fields are "IP Block" and "Country"
Table No.2 "web_Cdr_export" Fields are "Originating TG" and "ANI"
My Query is as follows
SELECT web_Cdr_export.[Originating TG]
, web_Cdr_export.[Inpulsed Digits]
, web_Cdr_export.ANI
FROM web_Cdr_export
WHERE (((Left([fraud_IP_Blocks_country]![IP Block],1) & ".")
=Left([web_Cdr_export]![Originating TG],1) & "."));
When I run query I get
"Enter parameter value" for field "fraud_IP_blocks_country!IP BLOCK
I am having trouble figuring out what could be wrong.
You are missing "fraud_IP_Blocks_country" reference in your FROM clause. Please try this:
SELECT web_Cdr_export.[Originating TG], web_Cdr_export.[Inpulsed Digits],
web_Cdr_export.ANI
FROM fraud_IP_Blocks_country, web_Cdr_export
WHERE
(((Left([fraud_IP_Blocks_country]![IP Block],1) & ".") =
Left([web_Cdr_export]![Originating TG],1) & "."));
Kind people please disregard my issue I found the answer after pulling it apart. I for got to add the 2nd table to the query. Very stupid mistake as I said I am getting to OLD

How to get the numbers of rows from a pl/sql cursor without opening it?

Imagine declaring a pl/sql cursor. Usually you would open it, fetch the records and in the end ROWCOUNT will tell about the number of records retrieved. But is there a way to get this number of records only when having declared a cursor? Or do I always have to go through all the records to reach my goal?
I' m asking because I need something like that: Go through the cursor and fetch one record after the other but right from the beginning I have to know how much records it will retrieve in complete. This should answer a question like: right now I' m working with record 1 of 5. I' ve solved this problem by using a SELECT COUNT(*) INTO (with the same SELECT-clause the Cursor is build of) and it works but isn' t there a better way to do so? Any help would be greatly appreciated:)
It's not exactly what you're looking for but this might do the trick:
SELECT column1, column2, ..., columnN,
rownum current_row,
count(1) over () total
FROM table
WHERE ....;
This way, when you're going through the cursor you will always know the number of the record you're working with and the total of records.

Order by Sum Field

I try to write a SQL-Statement in X++. It should do something like this:
SELECT
Table.field1, SUM(Table.field2) AS SumField
FROM
Table
WHERE
Table.FieldX = Something
GROUP BY
Table.Field1
ORDER BY
SumField;
The problem i have in X++ is that it orders the records before calculating the sum of them. I know that i could make a While Select in X++ and order them by code, but that is not the way i want to do it.
Can someone tell me how i could handle this?
Sorry, but you cannot both do a sort by and group by in a X++ select or query.
The solution is to make a view (without the sort), then do a select on the view with the order by.

Increase performance on insert cursor?

I would like to ask you how would you increase the performance on Insert cursor in this code?
I need to use dynamic plsql to fetch data but dont know how to improve the INSERT in best way. like Bulk Insert maybe?
Please let me know with code example if possible.
// This is how i use cur_handle:
cur_HANDLE integer;
cur_HANDLE := dbms_sql.open_cursor;
DBMS_SQL.PARSE(cur_HANDLE, W_STMT, DBMS_SQL.NATIVE);
DBMS_SQL.DESCRIBE_COLUMNS2(cur_HANDLE, W_NO_OF_COLS, W_DESC_TAB);
LOOP
-- Fetch a row
IF DBMS_SQL.FETCH_ROWS(cur_HANDLE) > 0 THEN
DBMS_SQL.column_value(cur_HANDLE, 9, cont_ID);
DBMS_SQL.COLUMN_VALUE(cur_HANDLE, 3, proj_NR);
ELSE
EXIT;
END IF;
Insert into w_Contracts values(counter, cont_ID, proj_NR);
counter := counter + 1;
END LOOP;
You should do database actions in sets whenever possible, rather than row-by-row inserts. You don't tell us what CUR_HANDLE is, so I can't really rewrite this, but you should probably do something like:
INSERT INTO w_contracts
SELECT ROWNUM, cont_id, proj_nr
FROM ( ... some table or joined tables or whatever... )
Though if your first value there is a primary key, it would probably be better to assign it from a sequence.
Solution 1) You can populate inside the loop a PL/SQL array and then just after the loop insert the whole array in one step using:
FORALL i in contracts_tab.first .. contracts_tab.last
INSERT INTO w_contracts VALUES contracts_tab(i);
Solution 2) if the v_stmt contains a valid SQL statement you can directly insert data into the table using
EXECUTE IMMEDIATE 'INSERT INTO w_contracts (counter, cont_id, proj_nr)
SELECT rownum, 9, 3 FROM ('||v_stmt||')';
"select statement is assembled from a website, ex if user choose to
include more detailed search then the select statement is changed and
the result looks different in the end. The whole application is a web
site build on dinamic plsql code."
This is a dangerous proposition, because it opens your database to SQL injection. This is the scenario in which Bad People subvert your parameters to expand the data they can retrieve or to escalate privileges. At the very least you need to be using DBMS_ASSERT to validate user input. Find out more.
Of course, if you are allowing users to pass whole SQL strings (you haven't provided any information regarding the construction of W_STMT) then all bets are off. DBMS_ASSERT won't help you there.
Anyway, as you have failed to give the additional information we actually need, please let me spell it out for you:
will the SELECT statement always have the same column names from the same table name, or can the user change those two?
will you always be interested in the third and ninth columns?
how is the W_STMT string assembled? How much control do you have over its projection?

SqlDatasource SQL query help needed?

HI
I am using a webform and a SQLDataSource, I am using query builder its for a grid view. I want to get a certain amount of characters frm a column in the table just say 1 line from a paragraph. How do I do it in query builder? is it possibe if no, how would i do it?
Thanks in Advanced!
NewbieProgrammer
To get first 100 characters modify your SQL to look like this:
SELECT SUBSTRING ( myColumn , 1 , 100 )
FROM mytable

Resources