ASP.NET MVC - Using Oracle With Dapper ORM - asp.net-mvc

I have that method:
public User FindBySector(string userId)
{
var cn = _context.Database.Connection;
var sql = #"SELECT Sector FROM AspNetUsers nu where nu.Id = :userId";
return DbContext.Database.Connection.Query<User>(sql, new { userId }).FirstOrDefault();
}
I was using sql server, but I will have to switch to oracle.
When performing this search the return is that the table does not exist.
If I search using sql developer it returns what I need, but there I have to put double quotes in the table name and fields, but using dapper it doesn't accept.
Thats the query that work using sql developer
SELECT "Sector" FROM "AspNetUsers" where "AspNetUsers"."Id" = 'fab00ef6-cc2d-4021-9562-ff61bff9cdab';
How do I replace these double quotes inside the dapper code?

If you ported your sqlserver to oracle and when doing so you wrapped your table names in " " then you kinda let yourself in for a world of pain, because all your table names are now case sensitive in Oracle and you have to supply the " ";
SELECT * FROM "AspNetUsers" --returns rows
SELECT * FROM AspNetUsers --becomes ASPNETUSERS and there is no such table
You can sort this out(make your life easier) though by renaming them and leaving out the quotes on the to:
RENAME "AspNetUsers" TO AspNetUsers
Oracle will convert the name to uppercase and will now work in queries no matter what case you specify (omit " ). This will work fine after you rename:
SELECT * FROM asPnEtUSerS
This will work:
SELECT * FROM "ASPNETUSERS"
This won't work:
SELECT * FROM "asPnEtUSerS"
If you have hundreds of tables and columns to rename I suggest you write a program that reads from USER_TAB_COLUMNS to list all your columns and programmatically issue renames for all of them. You could also write an sql that writes an sql, perhaps like:
SELECT
'RENAME COLUMN "'||table_name||'"."'||column_name||'" TO '||column_name
FROM user_tab_columns
Copy the results of this sql out of the results grid and paste it in again as a query and run it. You might need to add semicolons or something to separate them

Related

Split database string column twice in postgresql

I am using ruby on rails with postgresql. I have a table called "teams" with column "team_name". This "team_name" contains values like "1(Team1)", "1(Team2)", "2(Team1)", etc. Now want to take Team1, Team2, Team3 from those team names using sql query. If I use split_part as follows
sql = "SELECT SPLIT_PART(team_name,'(',2) FROM teams where id=20"
result = ActiveRecord::Base.connection.execute(sql).to_a
In the result I am getting
[{"split_part"=>"Team1)"}]
but I want the following result
[{"split_part"=>"Team1"}]
You can use SUBSTRING, but special characters must be escaped (backslash in this case):
Team
.select("SUBSTRING(team_name FROM '(Team\\d+)') AS team_name")
.where(id: id)

Can I use a parameter for the IN clause in a MySQL query/stored proc

I want to retrieve data in Delphi using a stored procedure. I used the below SQL statement and Initial as a parameter:
SELECT * FROM "PackUser" where Initials in (:Initial)
It didn't select any records when the user types A,B in my Edit box, because it sends a single string 'A,B' to the stored procedure. I want to add extra quotes in the middle: 'A','B'.
How can I do this?
This can be done like this:
input_string=',A,B,C.D'
SELECT * FROM "PackUser" where locate(concat(',', Initials), input_string);

Joining two database sets with different datatypes

Trying to join two datasets, but the join is based on two different data types (numeric and text)
SELECT *
FROM D1.T1 c
INNER JOIN
D1.T2 d
on c.CNUMBER=INPUT(d.CNUMBER, 8.) ;
This is does not work.
I can create a new dataset (copy existing one and add a numerical column) like this:
CNUMBER1=CNUMBER*1;
run;
Then when I join using this copy, it works... but I actually want to try to figure out the way to do it with direct Oracle connection.
In Oracle I would do:
on to_char(c.CNUMBER)=to_char(c.CNUMBER)
Taking a wild guess at what you actually want:
PROC SQL;
CONNECT TO ORACLE (...);
CREATE TABLE oracle_results AS
SELECT * FROM CONNECTION TO ORACLE (
SELECT *
FROM D1.T1 c
INNER JOIN
D1.T2 d
on to_char(c.CNUMBER)=d.CNUMBER);
DISCONNECT FROM ORACLE;
QUIT;
Will connect your SAS session to Oracle, perform the explicit passthrough SQL query and pass the results back to the SAS table oracle_results. Replace the dots with your Oracle connection credentials.

Firebird: simulating create table as?

I'm searching a way to simulate "create table as select" in Firebird from SP.
We are using this statement frequently in another product, because it is very easy for make lesser, indexable sets, and provide very fast results in server side.
create temp table a select * from xxx where ...
create indexes on a ...
create temp table b select * from xxx where ...
create indexes on b ...
select * from a
union
select * from b
Or to avoid the three or more levels in subqueries.
select *
from a where id in (select id
from b
where ... and id in (select id from c where))
The "create table as select" is very good cos it's provide correct field types and names so I don't need to predefine them.
I can simulate "create table as" in Firebird with Delphi as:
Make select with no rows, get the table field types, convert them to create table SQL, run it, and make "insert into temp table " + selectsql with rows (without order by).
It's ok.
But can I create same thing in a common stored procedure which gets a select sql, and creates a new temp table with the result?
So: can I get query result's field types to I can create field creator SQL from them?
I'm just asking if is there a way or not (then I MUST specify the columns).
Executing DDL inside stored procedure is not supported by Firebird. You could do it using EXECUTE STATEMENT but it is not recommended (see the warning in the end of "No data returned" topic).
One way to do have your "temporary sets" would be to use (transaction-level) Global Temporary Table. Create the GTT as part of the database, with correct datatypes but without constraints (those would probably get into way when you fill only some columns, not all) - then each transaction only sees it's own version of the table and data...

SQL: Combine multiple dynamic resultsets in one stored procedure

We're having a small issue and could use some help - we have the need to combine multiple resultsets from one stored procedure into one resultset (the limitation of our Java reporting framework). We've looked at Union, etc. but the problem is that the stored procedure resultsets are multiple crosstab results and the (1) number of columns for each resultset are unknown and (2) the column names for each resultset are unknown.
Basically, if the sp_ has 3 results like:
ID Name
1 Sam
2 Time
ID FName LName
1 John Jacob
2 Tim Test
3 Sam Hopkins
ID Amount
1 1000
2 5000
The ideal result would basically return the above text as-is which our framework would print to the user. Also please note that these 3-4 resultsets are not related to each other.
We're using SQL Server 2000 and Java 1.4.
Any advice would be appreciated.
Thanks,
SP
PS: An alternative explaination in case the one above is not very clear.
In SQL Query Analyzer if we have 3 select statements:
select * from countries; {returns id,countryname,countrycode}
select * from people; {id,countryname,countrycode}
select * from balance; {id,countryname,countrycode}
Then the results are displayed in three separate resultset boxes. We need to these resultsets to be returned as one resultset by the stored procedure (while not knowing the number/name of the columns due to the crosstab-ing taking place).
Thanks.
Your question does not specify which database vendor, or which client application framework you are using, but most databases with stored procs have the capability to return multiple resultsets, and the few client frameworks I am familiar with (VB6, C++, .Net, etc.) all have the capability to retrieve these multiple resultsets from a single database access and manipulate them to do just about whatewver you might want...
based on your comment, if your reporting framework can be hard coded to generate the column headings (firstName, lastName, amount, etc) without getting these strings from the database, then you could do this:
Select ID, Name as value1, null as value2
From TableA
Union
Select ID, FName as value1, LName as value2
From TableB
Union
Select ID, Cast(Amount as VarChar(20)) as value1, null as value2
From TableC
The key is that the number of columns returned by each select must be the same (3 in this example) and their names (aliases) and datatypes must be the same as well...
if the ids from the different tables are related, then your t-SQL should be left joins.

Resources