PL/SQL. Is it possible to use SELECT INTO from table dual? - plsql

Is it possible to use SELECT INTO from the dual table like this?
SELECT * INTO myTable
FROM ('val1' COLUMN1, 'val2' COLUMN2 FROM dual) dualTable
I would use this, because I need to insert values and get them back in a same statement.

There's really not much to it other than just making two variables.
Below is the code
SELECT
'val1' COLUMN1, 'val2' COLUMN2
INTO
variable1, variable2
FROM
DUAL
Regarding the insertion part, I don't think it's possible to be done in the same statement. You can always write a procedure in the back to handle this.

Related

SELECT NULL in OpenEdge Query

I would like to be able to SELECT a NULL value in my SELECT list so I can use it later in a UNION (for example). When I try SELECT NULL AS my_null_value FROM some_table it gives me a syntax error. I can work around it by doing the following.
SELECT CASE WHEN 1=1 THEN NULL END AS my_null_value
FROM some_table
Is there a cleaner way to accomplish this? I feel like I have done this in less characters and in a simpler manner in the past than using a CASE statement.
I ending up finding it.
A simpler (or more shorthand) way to accomplish this is to use the following:
SELECT NULLIF(0,0) AS my_null_value
FROM some_table

SQLite: How to UPDATE column using count(*) range?

I'm not sure I'm using right terminology here.
Basically I want to update entire "id" column using count(*) [485] as a delimiter, in an ascending order, so the resulting row value will correspond with rownumber (not the rowid).
If I understand you correctly, this should work for you:
UPDATE tbl_name SET id=rowid
EDIT
If that's is the case -> then it's a lit bit more tricky, since SQlite doesn't support variables declaration.
So what I suggest is,
To create temporary table from select of your original table which makes it's rowids to be as row numbers 1,2,3 etc...
Set it's rowNum (the needed row number column) as each rowid
Then replace the original table with it.
Like this: (assume original table called orig_name)
CREATE TABLE tmp_tbl AS SELECT rowNum FROM orig_name;
UPDATE tmp_tbl SET rowNum=rowid;
DROP TABLE orig_name;
CREATE TABLE orig_name AS SELECT rowNum FROM tmp_tbl;
DROP TABLE tmp_tbl;
Try this: http://www.sqlite.org/lang_createtable.html#rowid
You can use some inner database variables such as rowid, oid etc to get what you need.
[edit]
Think I just understood what you meant, you want for each insert action, add a value that is the total count of rows currently in the table?
If so, try something like this:
UPDATE tbl_name
SET id = (select count(*) from tbl_name)
WHERE yada_yada_yada

rownum equivlent in teradata

How to convert rownum in following query(oracle) to teradata equivalent:
and not exists(select 1
from CSE, SPD
WHERE cse.id=spd.id
AND ROWNUM = 1
AND CSE.STATUSID IN(6,7,8,13)
thanks.
There's no ROWNUM in Teradata, but you can usually rewrite it using ROW_NUMBER plus QUALIFY.
In your case there's no need for ROWNUM at all (at least logically, maybe Oracle prefers it to do a better plan), this is exactly the same:
and not exists(select *
from CSE, SPD
WHERE cse.id=spd.id
AND CSE.STATUSID IN(6,7,8,13)
Teradata specifically does not have any rownumber attached to the rows in a table as in Oracle.
But it has two analytical functions such as ROW_NUMBER() and RANK() which will give a number to your row and then you can accordingly pick you data.
You may use something like below:
QUALIFY ROW_NUMBER()(Partition by Id order by date)=1
Here, Partition by with a column or few columns can be used to group your data, like suppose some id column in your table and order by will sort the data for that id in your table according to the order by column you provide and =1 means it then chooses the row for that id which is given row number as 1.
Use somethin like below:
row_number() over(partition by '' order by statusid asc) as rownum_1
qualify rownum_1 = 1
The above statement imitates the rownum functionality of oracle.
you can use row_number. keep in mind the columns you want to take into consideration while calculating the row number. You can use qualify for the same.
other options are dense_rank, rank etc. In your case you can use rank in the sub query and put the condition rank = 1. if you want the syntax do let me know.
As you using this tweak for efficiency, I suppose, QUALIFY ROW_NUMBER() and other window functions will not suite you during their heavy use of CPU.
You can simply remove this part, Teradata should be fine.

Select from dual as one row?

To select many strings from dual as one column. I have tried the solution below and i can live with it, its relatively easy with PL/SQL macros to add "select from dual". I'm wondering is there any other way to achive this?
select 'AAA' as code
from dual
union all
select 'ABQ'
from dual
union all
select 'ACA'
from dual
union all
from dual
For a single column query there is. You need a database type that is a table of VARCHAR2, and some always exist in oracle including:
SYS.KU$_VCNT
SYS.DBMS_DEBUG_VC2COLL
(and of course you could create your own if you prefer).
Then you can query like this:
select * from table (SYS.KU$_VCNT ('AAA','ABQ','ACA'));
For a query with more than one column, you would need to create a type specifically for that query, which isn't such a useful option. But just for completeness this is how you could do it:
create type my_obj_t as object(n number, d date, c varchar2(100));
create type my_tab_t as table of my_obj_t;
select * from table (my_tab_t(my_obj_t(1,sysdate,'aaa'),
my_obj_t(2,date '2014-12-31','bbb'),
my_obj_t(3,sysdate+2,'bbb')));

How do I turn row data into column data?

I have a simple table of data that needs to be transposed from row data into column data. For example let me create a simple employee table:
I need to create a side-by-side comparison report structured like this using the above sql table:
Can someone show me the sql code using the sample table above? Or can it be done automatically using a built in ASP.net or DevExpress control?
Your feedback is always appreciated!
Thanks!
What you're looking for is a "pivot" function.
You can do them by hand inside of SQL, but it also looks like devexpress has a control for this...
http://www.devexpress.com/Products/NET/Controls/ASP/Pivot_Grid/
-- EDIT --
Like the commenter above posted, here's an introduction to the pivot function in SQL Server... What makes this tricky, is that unless you know exactly what values will comprise your columns, you'll have to use dynamic SQL to build the pivot.
Because it can be a little tricky to do in SQL, I'd try to stick to DevExpress since you already have it...
I think that if you were to add multiple data points to your pivot grid, that it would look like what you're expecting. Here's a screen shot from DevExpress that resembles what you're looking for...
Here's the page that shows this technique... In your case, instead of a row grouping, you could just do a "grand total", and then hide that column...
There it´s using SQL:
WITH FieldValueCte AS(
SELECT Name,
'Department' Field,
Department Value
FROM Table1 UNION ALL
SELECT Name,
'Sex' Field,
Sex Value
FROM Table1 UNION ALL
SELECT Name,
'HireDate' Field,
HireDate Value
FROM Table1 UNION ALL
SELECT Name,
'Salary' Field,
Salary Value
FROM Table1 UNION ALL
SELECT Name,
'Comments' Field,
Comments Value
FROM Table1)
SELECT [Field], [John Doe], [Jane Smith], [Peter Parker], [Jessica James]
FROM
(SELECT Field, Name, Value
FROM FieldValueCte) AS SourceTable
PIVOT
(
MIN(Value)
FOR Name IN ([John Doe], [Jane Smith], [Peter Parker], [Jessica James])
) AS PivotTable;
The solution is provided here
http://www.aspsnippets.com/Articles/Rotate-ASPNet-GridView---Convert-GridView-Columns-to-Rows-and-Rows-to-Columns.aspx

Resources