So Im new to databases and Im trying to learn the ropes. I have a DB2 database that Im getting familiar with. I was assigned a task where I need to write a method that does a search on the database. The search will take in two parameters, a username and a user id number. If the user and the user id number does not match or if one or the other turns out null then It needs to throw a error. If its valid then it will continue with spitting out information about the user.
I was told to use the findall() function or something similar to it. I was looking online and what I have found is examples that deal with like or ilike and im not sure how something like that will work in my situation. What would be a decent example of how I would start to go about this?
any help is appreciated. Ill post back if I make any progress.
note: Im using groovy/grails. Domain,Controller,View setup.
Is this some homework assignment from school?
findall() is usually a method in regular expressions which I don't think is relevant in here. If you have a SQL database, that means you have a RDBMS which uses SQL as query language. You need to learn about the SELECT command which can look daunting when you look the first the time to the manual but it's actually simple for your case. You need something like:
SELECT userfield1, userfield2,..
FROM myusertable
WHERE myusertable.username = 'uname' AND myusertable.userid = userid
uname and userid are your search parameters. Please note that this SQL query should be done with a PREPARED statement for security reasons.
When you run this query using your database library you get back an array of results which you have to analyze. If it is empty, no user found.
Edit: updated to take into account the use of hibernate
Hibernate uses HQL which is like SQL and has indeed a findAll method. See http://grails.org/doc/latest/ref/Domain%20Classes/findAll.html
Related
I know this works on the workbench:
SELECT Id,bizible2__BizibleId__c FROM Task
where bizible2__BizibleIid__c is on the activity table.
This works too:
SELECT Id,bizible2__BizibleId__c,owner_manager__c FROM Task
but it doesn't work in simple_salesforce. No clue why. So I tried to simulate it because owner_manager__c is a calculated field that equals:
Owner:User.Manager.FirstName &" " & Owner:User.Manager.LastName
the owner is a standard relationship for the task table and presumably the activity table. My attempt:
SELECT Id,bizible2__BizibleId__c,Owner.name FROM Task
works, but
SELECT Id,bizible2__BizibleId__c,Owner.Manager.FirstName FROM Task
Didn't work. Manager is a hierarchy thingy. I thought it could be because the owner relationship is user,calendar,something else and not just user, so I tried
SELECT Id,bizible2__BizibleId__c,LastModifiedBy.Manager.FirstName FROM Task
and that didn't work.
I figured it out. You have to go to the directory simple_salesforce is in: site-packages\simple_salesforce\api.py. then in the line DEFAULT_API_VERSION = '42.0', change 42.0 to 51.0. That, of course, is a terrible hack, so I asked this question:
How do I change the api version of Simple Saleforce
But so far there is no answer other than what I did.
From what I remember "simple salesforce" uses REST and this API respects field level security. Are you sure you have access to the field? Just because you're sysadmin and bypass stuff in UI doesn't mean your Profile is all right. Workbench might be using SOAP API which is bit old and doesn't enforce the fields (yet).
What does this do for you?
SELECT Id,
TYPEOF Owner
WHEN User THEN Username, Manager.Name
END
FROM Task
On mutant fields sometimes you need to use polymorphic SOQL to get the fields you want. But still, the formula should work OK.
Our company is setting up a new Snowflake instance, and we are attempting to migrate some processing currently being done is MS SQL Server. I need to migrate a Table-Valued SQL Function into snowflake. The source function has procedural logic in it, which is not allowed to my knowledge in snowflake UDTFs. I have been searching for a workaround with no success.
To be as specific as I can, I need a function that will take a string for input, decode that string, and return a table with the keys and their corresponding values. I cannot condense all of the logic to split the string and decode the keys into one SQL statement, so Snowflake SQL UDTFs will not work.
I looked into whether a UDTF can call a procedure and somehow I could simply return a result, but it does not look like that will work. Please let me know if there is any way to work around this.
I think Javascript UDTF is what you're looking for in Snowflake:
https://docs.snowflake.com/en/sql-reference/udf-js-table-functions.html
funny I just stumbled onto this as I'm running into the same thing myself. I found there is a SPLIT_TO_TABLE function that may be able to accomplish this. As Greg suggested nesting this together in the form of a CTE combined with a JOIN may allow you to accomplish what you need to do.
I have a client with many integrations to a SQL Server. All the integrations are doing simple calls such as getting an account name.
Due to migration, the SQL Server is being replaced with a different data source that I can communicate via API.
I've written both OLE and CLR type comms to get the data required, that's the easy part, the problem is that in SQL (SSMS) this is done as a function with a parameter such as getCustomerName(id).
I am, of course, simplifying this for this question but you get the idea.
The issue I have is that the integrations cannot be changed to consume a function with parameter and only do a select, eg. select name from customer where id = 123
So my question is this... can I capture the where clause being passed to view so that I can then call the function with that detail and produce a dataset?
Every search I've done focuses on the don't do this, SQL injection etc. etc. instead of the how and ifs, I just want to know if I can capture the where clause and how.
So based on our discussion in the comments, I suggest you get a way to change the query posted from the integration, then use the following:
select
dbo.getCustomerName(id)
from customer
where id = 123
This way, you utilize the view & function simultaneously...
I am having a problem with django subqueries. When I fetch the original QuerySet , I specify the database that I need to use. My hunch is that the later subquery ends up using the 'default' database instead of what the parent query used.
My models approximately look like so (I have several):-
class Author(models.Model):
author_name=models.CharField(max_length=255)
author_address=models.CharField(max_length=255)
class Book(models.Model):
book_name=models.CharField(max_length=255)
author=models.ForeignKey(Author, null = True)
Now I fetch a QuerySet representing all books that are called Mark like so:-
b_det = Book.objects.using('some_db').filter(book_name = 'Mark')
Then later somewhere in the code I trigger a subquery by doing something like:-
if b_det:
auth_address = b_det[0].author.author_address
My problem is that arbitrarily in some cases , on my live server, the subquery fails even though there is valid data for that author's id. My suspicion is that the subquery is not making use of the same database 'some_db'. Is this possible? Is it so that the database that needs to be used is not sticky in subqueries? It is just a hunch that this might be a problem, it is happening in the context of a celery worker, is it possible that the combination of celery with django ORM has some bug?
I have solved this each time this occurred by doing a full fetch by invoking select_related like so.
b_det = Book.objects.using('some_db').select_related('author').filter(book_name = 'Mark')
So right now, the only way for me to solve the problems is determine beforehand all the data that I will need, and make sure that the top level fetch has all those inner model references using select_related. Any ideas why something like this would fail?
I am unable to recreate this locally else I would have debugged it. Like I said, it is pretty random.
Ok, I have an handle on this now. My assumption that the subqueries would remain sticky to the original database is wrong. What django does is that first it hits the database router that is configured. If that does not return anything only in that case it makes use of the original database.
So, if the configured database router returns some database to be used then that gets used. In my opinion this is wrong and we need to use the original database first and then check the database router.
I have a simple Ms-Access database with one table named Student and it has two columns ID and Name.
When I the database in Access and enter the query
select * from Student where Name like 'J%'
in its SQL view, it gives an empty resultset.
But the table has a Name called John.
I tried with other databases and tables also with like-queries, but none works.
Can anyone please tell if there is any special reason for this???
Thank you
Edit:
The same query works with c sharp code
What you need is
select * from Student where Name like 'J*'
or possibly (because I don't have access handy to check, possibly either will work)
select * from Student where Name like "J*"
The * is the wild card character for MsAccess
From my experience in the past... yes access syntax has some minor difference that make even simple things a trouble.
I don't remember how but check around a way to make access show the sql from some results you retrieved in a graphical way, there must be some show sql button somewhere.
Once there examine carefully the sql syntax, then test your sql in access' editor.
So the main idea is let access show you the way!