What is the difference between newsequentialid, scope_identity and ##identity? [duplicate] - sql

This question already has answers here:
What is the difference between Scope_Identity(), Identity(), ##Identity, and Ident_Current()?
(7 answers)
Closed 4 years ago.
I would like to make my UserID column a GUID type and I found these three options. I do not know exactly what is the main difference between them.

I think the best version is NEWSEQUENTIALID().
->##IDENTITY returns the last auto_incremented(identity) value (even if it is created by a trigger/user defined function).
->SCOPE_IDENTITY() returns the last identity value created (that is not created by a trigger or an udf).
->NEWSEQUENTIALID() creates a GUID that is greater than any GUID previously generated by this function on a specified computer since Windows was started. (For more information, see https://learn.microsoft.com/en-us/sql/t-sql/functions/newsequentialid-transact-sql?view=sql-server-2017)

Related

Does PostgreSQL only support STORED generated columns? [duplicate]

This question already has answers here:
Computed / calculated / virtual / derived columns in PostgreSQL
(8 answers)
Closed 2 years ago.
Forewords
I've given some examples from PHP just to point my development environment. The question is not about PHP, it's purely about PostgreSQL.
PostgreSQL documentation about generated column states that:
There are two kinds of generated columns: stored and virtual. A stored generated column is computed when it is written (inserted or updated) and occupies storage as if it were a normal column. A virtual generated column occupies no storage and is computed when it is read.
However, it only shows an example of stored column in the example of that page and not a virtual one. Also it says:
The keyword STORED must be specified to choose the stored kind of generated column. See CREATE TABLE for more details.
...where it links to CREATE TABLE page. In that page, the documentation clearly points the pattern is GENERATED ALWAYS AS (expression) STORED.
GENERATED ALWAYS AS ( generation_expr ) STORED
This clause creates the column as a generated column. The column cannot be written to, and when read the result of the specified
expression will be returned.
The keyword STORED is required to signify that the column will be computed on write and will be stored on disk.
The generation expression can refer to other columns in the table, but not other generated columns. Any functions and operators used must be immutable. References to other tables are not allowed.
I actually try to implement a virtual field in Laravel (PHP) and this is what I've came up with so far in my migrations:
DB::statement('ALTER TABLE entries ADD COLUMN do_hint BOOLEAN GENERATED ALWAYS AS (hint_hash OR hint_tags OR hint_due_date OR hint_created_at OR hint_updated_at) VIRTUAL');
As you can see in this statement, there are hint_hash, hint_tags and other several boolean columns (named in hint_* glob pattern) in entries table. I'd like to compute do_hint on the fly so that:
I can actually query it from the database. Sure, I could also generate it on PHP but I'd like to query, that's the point I'd like to use a virtual column.
I'd like to rely on the performance of PostgreSQL rather than PHP.
And considering stored columns only generate once (when the row is inserted), this is not the behavior I desire. I'd like to have a virtual column so that I can read it on the fly.
I get statement errors from my code.
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "VIRTUAL"
LINE 1: ...hint_due_date OR hint_created_at OR hint_updated_at) VIRTUAL
^ (SQL: ALTER TABLE entries ADD COLUMN do_hint BOOLEAN GENERATED ALWAYS AS (hint_hash OR hint_tags OR hint_due_date OR hint_created_at OR hint_updated_at) VIRTUAL)
It points at VIRTUAL part is the problem. So I thought "Maybe, virtual generated column is the default behavior." so I removed it and tried again, yet it failed again, complaining the type of the generated column must be defined:
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at end of input
LINE 1: ...tags OR hint_due_date OR hint_created_at OR hint_updated_at)
^ (SQL: ALTER TABLE entries ADD COLUMN do_hint BOOLEAN GENERATED ALWAYS AS (hint_hash OR hint_tags OR hint_due_date OR hint_created_at OR hint_updated_at))
So, my question is in the title: Does PostgreSQL, at this point in time, only support stored virtual columns? Is there no way to get virtual columns at this point?
I mean, the documentation is confusing me when it says "There are two kinds of generated columns: stored and virtual.". Is it (i) only trying to introduce the concept of virtual and stored generated column here or (ii) advertising a new feature it has on PostgreSQL 12? I mean, if there is no way to create virtual generated columns, they could add a warning saying "Virtual generated columns are not possible at this point. We only support stored ones yet." but there is not such a warning anywhere in the documentation. I'm confused.
Thanks in advance.
Environment
PHP 7.4.5
Laravel 7
PostgreSQL 12.2
The documentation clearly states that virtual generated columns are not supported.
PostgreSQL currently implements only stored generated columns.
This appears just one sentence after the part of the documnetation that you quoted.
Yes, currently PostgreSQL only supports stored generated columns.
You can use a view to get the functionality of virtual generated columns.

How to generate SQL WHERE IN or = ANY() condition in Firestore Database [duplicate]

This question already has answers here:
Google Firestore - How to get several documents by multiple ids in one round-trip?
(17 answers)
Closed 5 years ago.
I have tried to figure out how to return a query based on whether the values are in an array I have client side. I so far have found nothing regarding the issue. Is this possible?
Firestore now supports "IN" queries:
Announcement
Documentation
Example:
let citiesRef = db.collection("cities")
citiesRef.whereField("country", in: ["USA", "Japan"])
Before November 2019
In Firestore, there is no "where in" like you might be used to with SQL.
If you know the values you want to query, perform different queries for each one, and call getDocument() on each of the DocumentReference objects. You typically would do this in a loop an collect the results yourself.

How to decrement dates? [duplicate]

This question already has answers here:
Difference of two date time in sql server
(21 answers)
Closed 7 years ago.
I have 2 dates: startDate= 6/22/2015 and endDate= 6/20/2015.
I need to decrement these 2 dates, i meen endDate-startDate, so in this example I will get 2.
How can I do this in sql?
The native way to do this would be to use the setDate method. There is an existing Stackoverflow post here: How to Add Dates
However, I always pass along the advice to use a date library, something like moment.js. In most languages, managing dates (timezones, leap years, etc ets) is incredibly difficult, and javascript can be even more so with browser time zone behaviors.

id as a name of property in NSManagedObject-subclass [duplicate]

This question already has answers here:
What is your alternative name for ID variable in Objective-C?
(6 answers)
Closed 9 years ago.
I am making some improvements in project made by my predecessors on current job. I found property id in NSManagedObject class as shown below:
Also Xcode highlights this property name as id type. Can this cause some problems in future? I mean, does system smart enough to understand that this is a property name? Do I need to rename it through refactor or just let it stay as is?
id is a reserved word in objective-c, do not use any reserved words as property names on an object.
Don't use id in your model prefer uid for example even if the compiler can distinguish the difference it is not a good practice.

SQL Server parsing function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Split Function equivalent in tsql?
I have a column that contains data in the form:
CustomerZip::12345||AccountId::1111111||s_Is_Advertiser::True||ManagedBy::3000||CustomerID::5555555||
Does SQL have any sort of built in function to easily parse out this data, or will I have to build my own complicated mess of patindex/substring functions to pull each value into its own field?
I don't believe there is anything built in. Look at the comments posted against your original question.
If this is something you're going to need on a regular basis, consider writing a view.