How do you do Automatic Select clauses if you dont have a WHERE statement? - petapoco

In the documentation it says you can decorate the POCO with the table name and primary key column so you dont have to specify the SELECT * FROM TABLENAME an can just use WHERE...
How do you use this feature if you dont have a WHERE clause?

Just use an empty string.
var data = db.Fetch<User>("");

Related

Select columns from table in schema different than public

In my PostgreSQL database I have table that is inside import schema. When I want to get all data from the column I do:
select * from import.master_plan
This query works fine. But when I try to for example get only title column values:
select import.master_plan.title from import.master_plan;
it returns:
ERROR: column master_plan.title does not exist
LINE 1: select import.master_plan.title from import.master_plan;
^
HINT: Perhaps you meant to reference the column "master_plan.  title".
I've also tried:
select title from import.master_plan;
but this also not works. I'm using PostgreSQL 10. How can I fix that?
I would suggest that you use a table alias instead:
select mp.title
from import.master_plan mp;
This is much easier to read and to type.
Judging from the error message, though, the name seems to have leading spaces. Something like:
select mp." title"
from import.master_plan mp;
might work. If this is the case, alter the table and rename the column.

Inserting new rows into table-1 based on constraints defined on table-2 and table-3

I want to append new rows to a table-1 d:\dl based on the equality constraint lower(rdl.subdir) = lower(tr.n1), where rdl and tr would be prospective aliases for f:\rdl and f:\tr tables respectively.
I get a function name is missing ). message when running the following command in VFP9:
INSERT INTO d:\dl SELECT * FROM f:\rdl WHERE (select LOWER(subdir)FROM f:\rdl in (select LOWER(n1) FROM f:\tr))
I am using the in syntax, instead of the alias based equality statement lower(rdl.subdir) = lower(tr.n1) because I do not know where to define aliases within this command.
In general, the best way to get something like this working is to first make the query work and give you the results you want, and then use it in INSERT.
In general, in SQL commands you assign aliases by putting them after the table name, with or without the keyword AS. In this case, you don't need aliases because the ones you want are the same as the table names and that's the default.
If what you're showing is your exact code and you're running it in VFP, the first problem is that you're missing the continuation character between lines.
You're definitely doing too much work, too. Try this:
INSERT INTO d:\dl ;
SELECT * ;
FROM f:\rdl ;
JOIN f:\tr ;
ON LOWER(rdl.subdir) = LOWER(tr.n1)

How to write pgsql update query with string aggregate?

I have update query that will manually change the field value as a unique string, the table already have a lost of data and the id as unique Pkey.
So I need the names should look like
mayname-id-1,
mayname-id-2,
mayname-id-3, etc
I tried to update with string_agg, but that doesn't work in update queries
UPDATE mytable
SET name = string_agg('mayname-id-', id);
How to construct string dynamically in an update query?
How about the following:
UPDATE mytable
SET name = 'mayname-id-' || CAST(id AS text)
Typically, you should not add such a completely redundant column at all. It's cleaner and cheaper to generate it as functionally dependent value on the fly. You can use a view or a "generated column" for that. Details:
Store common query as column?
You can even have a unique index on such a functional value if needed.
Use string concatenation
UPDATE mytable SET name = 'nayname-id-' || (id :: text);

Use result of query in a function (postgres 8.3)

I am trying to do something like this:
select char_length(select text_field from table where key = 1)
This won't work, and I presume, because the return type of a query is a table, not a string or text variable.
Is there a way to specify row,col of the result from a select statement?
edit: I overlooked to mention, that char_length is a function.
When passing the result of a query to a function, simply wrap the query in brackets:
select char_length((select text_field from table where key = 1));
The outer set of brackets is for the function, the inner set converts a query to a result.
This syntax is not specific to postgres - it applies to all SQL servers.
This link shows the above code executing correctly (thanks to #Fahim Parkar for this)
Although, you could re-factor your query to not require this syntax, nevertheless this is how you "pass the result of a query to a function".
select char_length(text_field) from "table" where key = 1
Assuming key is a primary key or unique key the first example below will work. It works only if the sub-query returns only 1 row. The second example will work for 1 or more rows.
select char_length((select text_field from table where key = 1));
select char_length(text_field) from table where key = 1;
It should be
select char_length(text_field) from "table" where key = 1
Also I believe, your table name is not table.

Select query to retrieve the value of primary key for a specific row in a table

I am struggling to retrieve the value of primary key for a table. We are using MS SQL Server 2005. The database was designed years back by somebody else (he didn't follow the normalization rules at all). He used Key (which is a keyword in sql server) as the column name for primary key of a table. So I cannot use query like this: select key from table_name where column2 = ?
Could anyone help to write a query to get the value of the primary key for a specific row something like this: select primary_key from tbale_name where column2 = ?
Yes you can, simply wrap column names in backticks:
select `key` from `table_name` where `column2` = ?
Alternatively, depending on your DB, you might use square brackets:
select table_name.[key] from table_name where table_name.[column2] = ?
Edit: I see you said "MS SQL". I think that one works with the square brackets. MySQL accepts the backtick syntax.