PGSQL seems to ignore param passed in [closed] - postgresql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Issue: If I call with valid parameter I get a whole lot of results I should not.
So, while it seems aware of parameters, they're not used as I would expect.
If I call with no parameter at all I get an error there is no such function, so that's expected.
If I call with valid parameter I get a whole lot of results I should not.
So, while it seems aware of parameters, they're not used as I would expect.
Workaround: I can be redundantly specific with a where clause, but I know there's something I'm missing here. I expect to pass a territory id and get ONLY results for that territory id. I'm getting all territories right now.
select * from by_territory_id('TER-123') where territory = 'TER-123';
Of course I want to simply do the following, as that is what wrapping this in a function is for:
select * from by_territory_id('TER-123');
If anybody can see what is wrong here please point it out? The parameter is supposed to be used in the WHERE clause of this function:
CREATE OR REPLACE FUNCTION by_territory_id (territory_id char)
RETURNS TABLE (customer_id char, territory char, scan_id int, scan_status iol.consignment_audit_scan_status) AS $$
select distinct con.customer_id, cus.primary_territory stm_territory, cas.id, cas.status
from iol.consignment con
join iol.products prod
on con.description = prod.description
join iol.customers cus
on con.customer_id = cus.customer_id
left join iol.territories_and_roles tr
on cus.primary_territory = tr.territory_id
left outer join iol.consignment_audits_scan cas
on cus.customer_id = cas.customer_id
where prod.lvl3_id in ('PREMIUM', 'STANDARD')
and tr.role = 'STM' and cus.primary_territory = territory_id
order by cas.id
$$ LANGUAGE SQL;
EDIT: Two down votes saying I'm not clear enough at the point of this edit. All the same, the one answer is correct. I hope it is okay that I really just needed eyes on this and I believe somebody else will run into the exact same issue, so rather than expand on the above I'm going to leave it be because the issue was indeed the name of my parameter (and PG gave no complaint or indication).

Looks like one of your tables has a column named territory_id, and that is what is getting invoked by you unqualified reference. You can qualify it with the function name:
and tr.role = 'STM' and cus.primary_territory = by_territory_id.territory_id
Or better yet change the spelling of the parameter so that it is distinct.

Related

How i get the original variable name for a parameter inside a function call? (Python 3.7)

First, i want to say there are a few questions related to this topic but the answers are not what I'm looking for, those questions are related to files or something else so it shouldn't be count as a duplicated, but if you find a duplicated question makes me know an i will change this.
Description:
I need to print the original variable name passed to a function.
This code is an example to show what i want (Take this code as a starting point if you want):
def print_var_name(var):
original_var_name = do_something_here
print(original_var_name)
var_1 = 'foo'
print_var_name(var_1)
The output should be:
var_1
Thanks you guys!

How can I get user responses in surveygizmo?

I want to get user's response along with question from surveyGizmo. I am getting list of questions and possible answers.
But I want specific answer which user has given during survey.
https://restapi.surveygizmo.com/v5/survey/123456/surveyresponse?api_token='some token'
I can suggest solution, but only on python and missing some profit for you, becasue your task is not exact. It's a general way to print all answers. And you can choose that you need.
#needed packages
from surveygizmo import SurveyGizmo
import json
#auth
sg = SurveyGizmo(
api_version='v4',
response_type='json',
api_token = api_token,
api_token_secret = api_token_secret
)
#get data from your account as json (as I know json is optional)
answers = json.loads(sg.api.surveyresponse.list(surveyid, resultperpage=resultperpage, page=page)
#in answers['data'] are saved onlu responses
list_of_answers = answers['data']
#using loop you can get all answers on all questions
for answer_index in list_of_respondents:
for question_index, question_value in enumerate(list_of_answers[answer_index])
#SurveyGizmo data structure contains answers with labels like '[question(question_id), [option(option_id)' — that's why we need this 'if'
if question_value.startswith('[question'):
print(list_of_answers[answer_index][question_value])
Actually I'm working with it now and try to transfer all sg-data to sql like database and it makes me to do find simpliest ways to work. So f you give more information, I can make more profit for you.

Expanded example using distance() function

Note, I'm aware of OrientDB geolocation (though I have some trouble with the poor formatting of the accepted answer, as it makes huge assumptions and in fact may be syntactically incorrect, but cannot yet submit edits).
I have a Vertex of Bank with a property of latLong as a comma-delimited string. I'm having some trouble using the provided distance() function with OrientDB. Since distance(x,y,lat,long) is expecting discrete values we wrote a split function that returns the latLong as an array:
{ ..., values: [xx.xxx,yy.yyy], ... }
However, I (we) can't seem to make either the provided answer or the document example to work. I suspect the answer is simple and we're over thinking it, but we're stuck.
split(input,delimiter)
return input.split(delimiter)`
Pretty simple really.
What's wrong in the original question?
In the cited question above, the query
select distance(x,y,originx,originy) <= 30 as distance from Bank order by distance
generates the below error
com.orientechnologies.orient.core.sql.OCommandSQLParsingException: Error on parsing command at position #0: Error parsing query: select distance(x,y,originx,originy) <= 30 as distance from Bank Encountered " "<=" "<= ""
and should be
select distance(x,y,lat,long) as distance from Bank where distance <= 30 order by distance;
which returns nothing.
If Bank were to have discrete properties for lat and long, how are they passed to distance()? Specifically, is OrientDB expecting specific property names?
You have to insert in Bank the properties lat and long like double

SQL query with XML parameter

EDIT: I have found a relevant answer already on stack overflow here:
XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'
I have not dealt with XML in T-SQL before, and I am modifying an existing legacy stored proc, and picking most if it up through trial and error.
however I have hit a problem where trial and error is proving fruitless, and very slow. Think it's time to appeal to stack overflow gurus!
Here is some XML
<?xml version=\"1.0\"?>
<Notification xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<NotificationId>0</NotificationId>
<UserNotifications>
<UserNotification>
<UserNotificationId>0</UserNotificationId>
<NotificationId>0</NotificationId>
<UserId>13514</UserId>
<MessageTypeId>1</MessageTypeId>
</UserNotification>
<UserNotification>
<UserNotificationId>0</UserNotificationId>
<NotificationId>0</NotificationId>
<UserId>13514</UserId>
<MessageTypeId>2</MessageTypeId>
</UserNotification>
</UserNotifications>
</Notification>
The Stored Proc in question accepts the above XML as a parameter:
CREATE PROCEDURE [dbo].[Notification_Insert]
#ParametersXml XML
AS
BEGIN
The XML contains child "UserNotification" elements. I would like to select the UserId, MessageTypeId of each UserNotification, into a table like this
UserId | MessageTypeId
13514 | 1
13514 | 2
Obviously the size of the collection is not fixed.
My current attempt (which doesn't work - is along these lines:
DECLARE #UserDetails TABLE ( UserId INT, MessageTypeId INT);
INSERT INTO #UserDetails (UserId, MessageTypeId)
SELECT Tab.Col.value('#UserId','INT'),
Tab.Col.value('#MessageTypeId','INT')
FROM #ParametersXml.nodes('/Notification/UserNotifications[not(#xsi:nil = "true")][1]/UserNotification') AS Tab(Col)
But this never inserts anything..
I have been playing around with this for a while now and not had any joy :(
I would suggest going through the links below. I found them short and quick to go through:
http://blog.sqlauthority.com/2009/02/12/sql-server-simple-example-of-creating-xml-file-using-t-sql/
http://blog.sqlauthority.com/2009/02/13/sql-server-simple-example-of-reading-xml-file-using-t-sql/
I found the solution to this problem through further searching stack overflow.
The query I need (thanks to XQuery [value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *')
INSERT INTO #UserDetails (UserId, MessageTypeId)
SELECT UserNotification.value('UserId[1]','INT'),
UserNotification.value('MessageTypeId[1]','INT')
FROM #ParametersXml.nodes('//Notification/UserNotifications') AS x(Coll)
cross apply #ParametersXml.nodes('//Notification/UserNotifications/UserNotification') as un(UserNotification)

What text (in English) should I use when asking the user to overwrite a document? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I write a lot of applications in C# and I'm trying to sew up some holes in my standard practices.
Specifically, I'm trying to decide on the best text to use in a message box, and I thought I'd ask the StackOverflow community since I believe that many opinions are always better than one.
What I have currently is:
"Document XXX.docx already exists. Okay to overwrite?"
Buttons for; Yes, No and Cancel
I'm really interested to see which examples turn out to be the most popular.
There are no limits on the style used; formal, casual, humourous, etc. All suggestions are welcome. Aim to err safely within political correctness though.
On a small side note: It would also be great, but by no means essential, to consider that the same text could also be suitable for a command line program.
Please note: English language only please. For other languages, please raise a new question.
Personally, I like to see a bit more context and slightly different wording. Something like:
"<existing document name>" already exists in "<destination path>".
Would you like to replace it (Y/N)?
or perhaps with even more information:
"<existing document name>" (<bytes>) (<date modified>) already exists in
"<destination path>".
Would you like to replace it with file of size <new bytes>, last modified
<new date modified> (Y/N)?
I think "replace" is a bit more clear than "overwrite" - and (speculation) may translate into other languages, and maintain the intended meaning more often.
...and one last option with new file name/location info:
"<existing document name>" (<bytes>) (<date modified>) already exists in
"<destination path>".
Would you like to replace it with file "<new file name>" in "<new path>" of
size <new bytes>, last modified <new date modified> (Y/N)?
This last one would probably just show a temp file / buffer location for an initial file save--but it is reusable, and more meaningful when doing a file copy.
Hope you find one of these useful.
Cheers,
Hans
Title: Overwrite?
"XXX.docx exists.
Would you like to overwrite XXX.docx?"
Buttons: Overwrite, Keep, Panic
Simple solutions are usually best, for example windows user might find it familiar to see a message :
He has 3 options :
Replace
Abort action x3
Create with a new name