This is my first post to this website.
Here is my question: I want to use the same stored procedure for Insert and Update.
For Insert there is no problem. But at the time of update O want to update it based on my primary key, not on any unique column value.
Here is my current Procedure.
IF EXISTS(SELECT MemberID FROM MEMBER WHERE MEMBERSHIPNO=#MEMBERSHIPNO)
WHERE MEMBERSHIPNO=#Membershipno
I want to use WHERE MemberID=#MemberID //Memberid is primary key
Please Help.
I'm not sure if I understand you're question quite clearly, but from what I understand you're trying to do a IF clause that would UPDATE if said credentials exists.
Something like this would do the trick, just fill in the logic and it should all work out.
IF EXISTS (SELECT MemberID FROM MEMBER WHERE MEMBERSHIPNO=#MEMBERSHIPNO)
UPDATE [ADD UPDATE LOGIC HERE] WHERE MemberID=#MemberID
ELSE
INSERT [ADD INSERT LOGIC HERE]
Hope this helps.
Related
(See example schema image below)
I am attempting to query a single user from the users table using the email field, along with the id & key fields from the applications table. The results should contain the user found (if any), along with the application (referenced using the key & id fields) and the applications_users associated data.
I can easily write SQL manually to perform this operation:
SELECT
"users".*,
"applications_users"."scopes",
"jwt_applications".*
FROM
"users"
INNER JOIN
"applications_users" ON "applications_users"."user_id" = "users"."id"
INNER JOIN
"jwt_applications" ON "jwt_applications"."id" = "applications_users"."application_id"
WHERE
"users"."email" = 'rainbows#unicorns.net'
AND "jwt_applications"."id" = '01daafc9-2169-4c78-83e9-37ac0a473e3d'
AND "jwt_applications"."key" = 'follow_the_rainbow'
LIMIT 1
However, I cannot for the life of me get the query correct when using ActiveRecord.
These are the unsuccessful attempts I have made thus far:
user = User.where(email: args[:username]).joins(:applications).merge(
JwtApplication.where(id: args[:application][:id], key: args[:application][:key])
).take!
This gets the user correctly, however Rails performs a second SQL query when I attempt to access user.applications (and it also returns all applications associated with the user; so it appears to disregard the id & key conditions)
user = User.where(email: args[:username]).joins(:applications).merge(
JwtApplication.where(id: args[:application][:id], key: args[:application][:key])
).references(:applications_users).take!
This gets the user correctly and also the correct application (yay!), however Rails performs a second SQL query if I attempt to call user.applications_users -- it also returns a collection for all data inside the applications_users table (again, disregarding the id & key conditions)
user = User.where(email: args[:username]).joins(:applications).where(
jwt_applications: {
id: args[:application][:id],
key: args[:application][:key]
}
).take!
This gets the correct user, however Rails performs another SQL query when I attempt to access user.applications -- also returning all applications.
Anyway, hopefully a Rails genius can shed some light on this question! I will be the first to admit that I am by no means a Rails expert; I have spent the last 10 years of my professional career coding in PHP & C++, so please bear with me if this comes off as a stupid question :)
Not sure if this is something you're looking for but...
You can write ActiveRecord query like (join model should be implicitly added to your query):
User.joins(:applications).where(email: email).where(applications: { key: key, id: id})
Where email, key and id as params to pass to the query.
On top of that query you can use select fields to get everything you need:
user = User.joins(:applications).where(email: email).where(applications: { key: key, id: id}).select('users.*, applications.id as appid applications.key as appkey').first
That will give you back the user model (if present) or empty relation if nothing matches your criteria.
You can then call the fields like
user.appid
user.appkey
You can always call select ('users.*, application_users.scopes, applications.*) which will return you all the fields in single instance (still under User model) BUT duplicate fields like id will only be shown once, that's why it's better to grab just the fields you want and give them unique identifiers like I've shown with appid and appkey.
Again, might not be exactly what you're after, but hopefully it points you in the right direction!
I have an app which uses SQLite, i have some problems deleting a row because one of its value is unique.
I get the error :
Unknown error calling sqlite3_step (19: column myField is not unique)
DB Query: DELETE FROM "****" WHERE "myField" = '72645DA9-E95F-452A-94B8-C93A20D9A41C'
Is there a way to force the delete of this unique row ? Or can simply disable the unique constraint when i need to delete.
Of course this behavior should not appear, but it does so please do not tell to re-create my database :)
Thank you very much.
I can successfully create entries in contour programmatically(C#) but I am not able to update the created record using the record ID. After digging my head around can’t find a reason why the following code doesn’t work. It’s very basic and all That I am trying to do is get the record that exist in the contour.
RecordStorage recordStorage = new RecordStorage();
Record r = recordStorage.GetRecord(new Guid("15d654cb-a7c6-4f1f-8b55-0ecd7d19b0e3"));
recordStorage.Dispose();
Just to start with the update process, I am trying to get the record object using it’s id but can’t proceed further as it throws a weird error “An item with the same key has already been added.” I can’t understand while it’s trying to set the value when I call the “storage.GetRecord()”. Following is the stack trace
**An item with the same key has already been added.**
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Umbraco.Forms.Data.Storage.RecordFieldStorage.GetAllRecordFields(Record record)
at Umbraco.Forms.Data.Storage.RecordStorage.GetRecord(Object id)
at MauriceBlackburn.Service.ContourFormService.InsertRecord(ContourFormFields unionContourForm)
Any thoughts, have I missed something, I have been digging all day around and still not able to figure this out. Thanks in advance.
Much Appreciated.
First off, try deleting the workflow and re-adding it.
You could also create two simple workflows, one that will write the record and a second to manipulate it (using the id when written).
Make sure that there are no records with the same ID in database. You might have inserted them before.
I have a table called SubElement.
It has two primary keys : Element_Code, Sub_Element_Code
In my view, when i try to do something like the following
<%
sub_element_model = Condition::SubElement
sub_element = #sub_element_code.nil? ? sub_element_model.new : sub_element_model.find(#sub_element_code)
if sub_element.persisted?
#element_code = sub_element.Element_Code
f.object.Sub_Element_Code = #sub_element_code
end
%>
I got an error after selected the sub_element_value like
["4"]: Incorrect number of primary keys for Condition::SubElement: [:Element_Code, :Sub_Element_Code]
How can i use the find method for two composite primary keys.
Update:
In the form, I have element_code field, sub_element_code and material. But everything should visible once the parent selected. With the help of some javascript I try to finish this. The Main problem of what I could not explain in detail is the form fields are creating by some helper file. It's a very large file and i cant change that. So I am looking for the alternative solution to change the find method for the two composite primary keys to get the value.
Not exactly sure what you're trying to do with the code above but you could use where:
sub_element_model.where(first_id: 3, second_id: 5).first
Your code doesn't look very conventional-ish however, perhaps if you described what you're trying to do SOers could help you come up with a neater solution :)
I myself never try using a multiple attribute primary key, but I believe that ActiveRecord is telling you that you are not using the correct number of primary keys in your find search.
Try something like
Model.find([first_part_of_primary_key, second_part_of_primary_key])
You should not be using find (which gets a single record by primary key - which you don't completely have), but rather where (which gets you a collection of records by any field(s) you want):
sub_element = #sub_element_code ?
SubElement.where(sub_element_code: #sub_element_code).first :
SubElement.new
I am using CL-SQL with SQLite backend, and I can't quite get autoincremented primary keys to work. I declared a slot like (in def-view-class):
((id :accessor d-id :db-constraints :primary-key :type integer :db-type "INTEGER")
But if I create the class, the field is not updated, not even when I call update-records-from-instance, and if I call update-instance-from-records, it will get updated wrong. Is there a way to use autoincremented fields CL-SQL at all?
I think this might be caused by a bug. I've sent a patch to the clsql mailing list which fixes the problem for me: