SQL: Oracle - Parameters in query - sql

I am trying to use the vs2008 query builder to create a query with a parameter.
I know that in sql server it would work with:
select col1,col2
from tbl
where col3=#myParam
How would it be typed in oracle or is it pl/sql?
I get the problem in the #myParam part.

Oracle SQL parameters are specified using :
SELECT col1, col2 FROM tbl WHERE col3=:myParam
You will have to be careful when specifying this as an OracleParameter though, as some libraries miss off the :, and some require it to bind correctly.

Related

Is it possible to add a multiple value parameter query in SSRS with left()?

I have a query in SSRS where I can search for a title with a certain prefix. The reason why I used left() is because there are some prefixes that have an underscore or a dash so I need my search results to be exact and it wasn't giving me that when I used the like operator.
Here's an example of what my query looks like:
select title
from table1
where left(title, len(#prefix)) = #prefix
This works if my prefix dropdown doesn't allow multiple values. Now, I have also tried this query:
select title
from table1
where left(title, len(#prefix)) in (#prefix)
But this just caused my report to generate a "Query execution failed for dataset" however, this query does work when I try it on SQL Server and when it isn't allowed for multiple values.
As I mentioned in my comment, I hate that SSRS allows the syntax IN (#Parameter); it causes misunderstandings and teaches people incorrect syntax.
When you have statement like SELECT * FROM dbo.MyTable WHERE MyColumn IN (#MyParameter); the SQL that SSRS runs is not that statement. Instead SSRS removes the parameter and then injects the multiple values into the statement. So the statement it actually runs would be something like SELECT * FROM dbo.MyTable WHERE MyColumn IN (N'MyValue1',N'MyValue2',N'MyValue3');
If you were to actually run the SQL SELECT * FROM dbo.MyTable WHERE MyColumn IN (#MyParameter); in SSMS, or any other product, it would be a synonym of SELECT * FROM dbo.MyTable WHERE MyColumn = #MyParameter;. The syntax that SSRS uses is, for all intensive purposes, wrong.
There are multiple problems with that, which I'm not going to cover here.
When you then have a query like you do LEFT(title, LEN(#prefix)) IN (#prefix) you end up with a problem; LEN(#prefix) can't have tuples injected into it (LEN(N'MyValue1',N'MyValue2',N'MyValue3') isn't valid syntax). This just results in a invalid query because SSRS teaches bad habits.
The solution to this is th force SSRS to not be (completely) dumb. Unfortunately SSRS is dumb and still (even in SSRS 2022!) doesn't support Table Table Parameters (TVPs) which would trivialise this. As such you'll need to have SSRS pass a delimited value to a Procedure, and then you can handle the logic in your procedure. As you're using SSRS 2019 then you're also using SQL Server 2019 and thus have access to STRING_SPLIT. Also we can switch to a LIKE here.
This means you need a procedure like the following:
CREATE PROC dbo.YourProc #prefix nvarchar(4000) --Assumed data type
AS
BEGIN
SELECT title
FROM dbo.table1 t1
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT(#prefix,',') SS
WHERE t1.title LIKE SS.value + '%');
END;
Then, instead, you execute the procedure from your report.

How to append table in SQL using R?

I am now using odbc/dbplyr R packages to control database. Though I know I could use the function union to bind two tables by row, I don't know how to append one table to another without loading the data into the local memory. Is there any solutions?
Thanks.
I approach this using a combination of dbplyr and DBI packages.
The idea is to insert a table query into an insert statement. The insert statement can then be executed on the database:
append_database_table <- function(db_connection, db, schema, tbl_name, table_to_append) {
query <- glue::glue("INSERT INTO {db}.{schema}.{tbl_name}\n{dbplyr::sql_render(table_to_append)}")
# execute
DBI::dbExecute(db_connection, as.character(query))
}
Notes:
This all assumes that the table you wish to append to already exists in the database. Different syntax is needed if you are creating a new table.
dbplyr::sql_render returns the query that defines the input table
query is a text string of valid database code
I am using SQL Server syntax. You may need to adjust this if you are using a different flavor of SQL.
The above is a minimal example, the full function I use for this contains additional checks and ensures column names are correct. You can find it here along with the other support functions I use for dbplyr work.
This approach works even if your input table has some processing. Consider the following example:
prepared_table = remote_table %>%
filter(!is.na(col1)) %>%
group_by(col1) %>%
summarise(col2sum = sum(col2))
show_query(prepared_table)
This will return SQL code equivalent to:
SELECT col1, SUM(col2) AS col2sum
FROM remote_table
WHERE col1 IS NOT NULL
Appending this using:
append_database_table(db_connection, "db", "my_schema", "existing_table", prepared_table)
Will create the following SQL query and execute it on the database:
INSERT INTO db.my_schema.existing_table
SELECT col1, SUM(col2) AS col2sum
FROM remote_table
WHERE col1 IS NOT NULL

Convert DB2 SQL query to SQL Server

I have to convert a DB2 query to SQL Server, but don't understand what exactly below query does:
SELECT
t.MyColumnA NAME(MyColumnA-01),
t.MyColumnA COLHDG("COA" "VALUE")
FROM
MyTable t
It looks like the NAME and COLHDG functions are just UI functions specific to HelpSystems. The actual query would be
SELECT t.MyColumnA AS "COA VALUE"
FROM MyTable t

How to pass parameters to Microsoft SQL Server query?

I am new to SQL Server 2012, and I found SQL Server Management Studio is a little bit difficult to use.
Here is one of the problems that I cannot solve: Is it possible to pass values to a query in SQL Server Management Studio ?
SELECT col1, col2
FROM table
WHERE col3 = :variable
If I run this query in the Studio, instead of prompting an input field to let me input value for variable, it will not execute the query because of an error
Incorrect syntax near ':'
I used to use the same method in SQL Developer for Oracle, thus I thought it would work the same.
Thanks for any useful advice.
DECLARE #someVariable varchar(n)
SET #someVariable = 'variable'
SELECT col1, col2
FROM table
WHERE col3 = someVariable

PL/SQL Procedure: How return a select statement?

I want to create a stored procedure on ORACLE database server and my problem is that, I don't know how can I return a select statement .
Here is the logic, which should within the procedure:
Input paramters: filter1 (int), filter2 (string)
with cte as
(
select val1, val2, stddev(val3) from tab1 where parameter1 = filter1 and paramter = filter1 group by val 1, val2
)
SELECT cte.*,
round(some calculation) as final_results FROM cte
Afterwards I want to use this procedure in a MS asp.net application, with help of the MS ADO.net and MS Entity Framework 4.2.
Lot of thanks, for your response!
In Oracle we have to use Ref Cursors to acheive this. The very latest version of ODP .Net supports Ref Cursor binding for Entity Framework 4.x. Find out more.
Of course if you're not using Oracle 11gR2 you're probably out of luck, and you'll need to use one of the other suggestions (such as Pipelined functions).
To return the result of a SELECT in Oracle you would use a "pipelined table function".
Please refer to the manual for a description and an example:
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/tuning.htm#i53109
Here are some more examples from other sites:
http://www.oracle-developer.net/display.php?id=207
http://www.oracle-base.com/articles/misc/PipelinedTableFunctions.php
http://psoug.org/reference/pipelined.html
Either create a VIEW or a FUNCTION. Stored procedures in Oracle do not return table results like in TSQL.