I can use below code get int result in C#
new DataTable().Compute("1 * 2 / 2 + 3", string.Empty);//result:4
How can I do it in SQL Server? I expect convert and operation string to int.
My solution is CLR , but there is no any original solution?
Thanks.
There are a couple things you need to do.
Create the assembly with the CLR logic you already have.
On the SQL Server side, create a User-Defined Function that calls the assembly.
This is an example of what the UDF would look like from the documentation:
CREATE ASSEMBLY FirstUdf FROM 'FirstUdf.dll';
GO
CREATE FUNCTION CountSalesOrderHeader() RETURNS INT
AS EXTERNAL NAME FirstUdf.T.ReturnOrderCount;
GO
SELECT dbo.CountSalesOrderHeader();
GO
Just write like it is
select 1 * 2 / 2 + 3
Related
I'd like to select top {n} rows from sql-server
SELECT TOP 10 * FROM CUSTOMER; --> n should be configurable
How can I do this using JDBC?
Should I write a prepared statement and supply ? instead of 10?
Or Should I construct the SQL string and execute it as normal statement?
I just want to know if there's a de facto choice of supplying dynamic FETCH LIMIT (a configured value usually)
I suggest to use Pageable with JPA like this refer to https://www.baeldung.com/jpa-limit-query-results
Page<Passenger> page = repository.findAll(PageRequest.of(0, 1, Sort.by(Sort.Direction.ASC, "seatNumber")));
whats the STUFF equivalent in netezza, I am trying to do rows to column concatenation. I tried the GROUP_CONCAT()/ String_AGG as mentioned in the other question but i am not able to use both of them.
I've never used STUFF before, but looking at the description, there is not a native Netezza function that is its equivalent. However, it seems a simple thing to recreate with SUBSTR. Since I don't really use STUFF, take this with a grain of salt, but give it a try.
If we take an example of using STUFF:
SELECT STUFF('abcdef', 2, 3, 'ijklmn');
---------
aijklmnef
(1 row(s) affected)
If I dummy up the values and STUFF parameters into a subselect for demonstration purposes, here's how you can do it with SUBSTR.
SELECT
substr(orig_string ,0,start_pos)
|| replace_string ||
substr(orig_string,start_pos+del_length, LENGTH(orig_string)) new_string,
orig_string , replace_string
FROM
( SELECT
'abcdef' orig_string ,
2 start_pos ,
3 del_length ,
'ijklmn' replace_string ) foo
;
NEW_STRING | ORIG_STRING | REPLACE_STRING
------------+-------------+----------------
aijklmnef | abcdef | ijklmn
(1 row)
You can use GROUP_CONCAT to achieve similar results as with STUFF. The GROUP_CONCAT function must installed from nzlua examples directory on the Netezza Linux environment.
export NZ_PASSWORD='YOURADMINPASSWORD'
cd /nz/extensions/nz/nzlua/examples
../bin/nzl group_concat.nzl
After this group_concat is available in Netezza SYSTEM database. As ADMIN you can execute these commands to make them easily available to all users in a database.
grant execute on system..group_concat(varchar(128)) to public; -- once
create synonym group_concat for system..group_concat; -- in every user database
I'm working on a project that has a search engine. AS we know in MS ACCESS we could use "*" in Queries under Criteria field to retrieve all records.
In SQL Server I need the same technique. I have tried different LIKE with WHERE Clauses. But I still didn't get the exact result I want.
In this project I have 3 textboxes (Category, Item, Location). If the user leaves any of them empty. I want to retrieve all the records.
I need something like this:
string t1,t2,t3;
if(!String.IsNullOrEmpty(txtCategory.Text))
t1=txtCategory.Text;
else
t1="*";
if(!String.IsNullOrEmpty(txtItem.Text))
t2=txtItem.Text;
else
t2="*"
if(!String.IsNullOrEmpty(txtLoc.Text))
t2=txtLoc.Text;
else
t3="*";
-
-
-
// in a function i have this :
SELECT * FROM Table_Items WHERE Category='"+t1+"' AND Item='"+t2+"' AND Location='"+t3+"'"
in ms sql server you can use the same technique but instead of * you should use %.
for examples:
%: means any
a%: all strings that start with a
%z: all strings that end with z
SO, your code should look like something as below:
// codes here
t3="%";
WHERE ColumnName LIKE t3
or
Where ColumnName LIKE '%'
I hope that will help you.
Change your * to the %
... Where Category Like "'+t1+"' and Item Like '"+t2+"' ...
Say you have a stored procedure or function returning multiple rows, as discussed in How to return multiple rows from the stored procedure? (Oracle PL/SQL)
What would be a good way, using Scala, to "select * from table (all_emps);" (taken from URL above) and read the multiple rows of data that would be the result?
As far as I can see it is not possible to do this using Squeryl. Is there a scalaified tool like Squeryl that I can use, or do I have to drop to JDBC?
Functions that return tables are an Oracle specific feature, I doubt an ORM (be it Scala or even Java) would have support for such a proprietary extension.
So I think you're more or less on your own :).
Probably the easiest way is to use a plain JDBC java.sql.Statement and execute "select * from table (all_emps)" with the executeQuery method.
To address the second part of your question about a way to select from table in a more scala-esque way, I am using Slick. Quoting from their example documentation:
case class Coffee(name: String, supID: Int, price: Double)
implicit val getCoffeeResult = GetResult(r => Coffee(r.<<, r.<<, r.<<))
Database.forURL("...") withSession {
Seq(
Coffee("Colombian", 101, 7.99),
Coffee("Colombian_Decaf", 101, 8.99),
Coffee("French_Roast_Decaf", 49, 9.99)
).foreach(c => sqlu"""
insert into coffees values (${c.name}, ${c.supID}, ${c.price})
""").execute)
val sup = 101
val q = sql"select * from coffees where sup_id = $sup".as[Coffee]
// A bind variable to prevent SQL injection ^
q.foreach(println)
}
Though I am not sure how it's dealing (if at all) with stored procs/functions.
Using Simple.Data, I would like to get the result from an output parameter in a stored procedure. Let's say I have the following SP (disregard the uselessness of it):
CREATE PROCEDURE [dbo].[TestProc]
#InParam int,
#OutParam int OUTPUT
AS
BEGIN
SELECT #OutParam = #InParam * 10
END
When I call it with Simple.Data, I use the following code.
var db = Database.Open();
int outParam;
var result = db.TestProc(42, out outParam);
Console.WriteLine(outParam); // <-- == 0
Console.WriteLine(result.OutputValues["OutParam"]); // <-- == 420
It feels like outParam should contain the value, and not the OutputValues dictionary. So my question is: Is there a nicer way to get the result from OutParam in this particular case?
Unfortunately, out parameters are not supported by the dynamic binding infrastructure used by Simple.Data, as they are not supported in all CLR languages.
I am open to suggestions for better syntax, though.