Is it possible to set column default to SQL Server's NewId()? - fluent-migrator

In fluentMigrator, is it possible to set a column default to a function?
Specifically, I'm adding a uniqueidentifier (Guid) column to a SQL Server 2008 table that already contains data, and it's going to be a required field - is it possible to set the default to the NewId() function?

Field defaults for NewId() and GetDate() are available (for SQL Server at least) by referring to either SystemMethods.NewGuid or SystemMethods.CurrentDateTime, for example:
Create
.Column("incomeIdGuid").OnTable("tblIncome")
.AsGuid()
.NotNullable()
.WithDefaultValue(SystemMethods.NewGuid);

Related

Providing default value for unmapped column in SQL Compare

Is it possible to provide a default value or a query to provide a value to an unmapped column in the target table using Redgate SQL Data Compare?
To explain the scenario I have a configuration database that holds settings data for several database instances. The data is all in the same shape, but the config database has an additional InstanceID field in most tables. This allows me to filter my compare to only compare against the InstanceID relating to the source Instance database. However if I generate Insert scripts they fail because the Target Instance ID fields are non nullable. I want to provide a default value that is then used in the Insert Scripts. Is this doable?
SQL Data Compare doesn't have an easy way of doing this I'm afraid.
There is one way to do it - you could create a view that selects everything from the source table along with a computed column, which just provides the "default value" that you want to insert. Then you can map the view to the table in the target database and compare them, deploying from the result.
I hope this helps.

Can we force SQL Server for case sensitivity

What if I want to authenticate user using Stored Procedure using ADO.Net? Is there any way to do this or directly authenticate or find other way?
You can use database collation for your idea.
you must use SQL_Latic1_General_CP1_CS_AS instead of you must use SQL_Latic1_General_CP1_CI_AS
Also you can apply your collation on the special columns.
For example :
CREATE TABLE yourtable
(Name NvarChar(100) COLLATE SQL_Latic1_General_CP1_CI_AS, ...

How to create a primary key as A1/D-M/100000 in SQL Server 2005?

How to create a primary key as A1/D-M/100000 in SQL Server 2005 in Visual Studio 2008 ?
Use varchar(20) as column type and generate the primary key value from the code.
Your request is not possible in SQL Server as requested, which makes me want to tell you to do some more reading.
However, you can achieve something similar by creating a primary key in Visual Basic, and storing that value in a char or varchar field in SQL, as suggested by Adrian Godong's answer.
That said, what I would do if this were my database, is create a normal PK using int or bigint (depending on how many rows I'm planning to store), and using a second column as char or varchar, with an appropriate index, to store the Ax/D-M/xxxxxx values.
What you are wanting to do is mix business rules with SQL Server's database rules, and that's a very bad idea. SQL does not care what your data looks like, so you should not force it to do so. Your business rules may change down the line, and this would be much easier to accommodate if you do a proper PK to start with, that does not rely on some arbitrary naming convention.
What parts of that key are fixed, which change from row to row??
E.g. where does the "A1" or the "D-M" come from? Are they the same for all rows? Do they change from row to row??
If those parts are fixed, and only the big number in the middle needs to change, you could:
define a column of type ID INT IDENTITY(100000,1) for the number
define a computed column like this:
ALTER TABLE dbo.YourTable
ADD YourPKField AS 'A1/D-M/' + CAST(ID AS VARCHAR(6)) PERSISTED
In that way, SQL Server would auto-increment your ID field, and YourPKField would contain the values:
A1/D-M/100000
A1/D-M/100001
A1/D-M/100002
A1/D-M/100003
....
and so on - automatically, without you doing anything more.
Use an INT IDENTITY(100000,1) as your Primary key.
Add the calculated (displayed) key wherever you need it (queries...). It's decoration, and as such, it's part of the front-end, not of your data.

Retrieve the PK using ##identity

I'm building a website using ASP.NET and SQL Server, and I use
SELECT PK FROM Table WHERE PK = ##identity
My question is which is better and more reliable to retrieve the last inserted PK for multiuser website, using ##identity or using this:
SELECT MAX(PK) FROM Table WHERE PK = Session ("UserID")
I'm not sure exactly what you want to achieve, but the recommended way to retrieve the primary key value of the last statement on a connection is to use SCOPE_IDENTITY()
##Identity is particularly risky where you are using triggers, since it returns the last generated identity value, including those generated by triggers flowing on from a statement.
MSDN has the following to say:
SCOPE_IDENTITY and ##IDENTITY return
the last identity values that are
generated in any table in the current
session. However, SCOPE_IDENTITY
returns values inserted only within
the current scope; ##IDENTITY is not
limited to a specific scope.
You should certainly use SCOPE_IDENTITY() in favour of the MAX(PK) approach - any number of possible future changes could invalidate this method.
For SQL Server 2005 and above...
You can do the INSERT and SELECT in one call using the OUTPUT clause...
INSERT MyTable (col1, col2, ..., coln)
OUTPUT INSERTED.keycol, INSERTED.col1, INSERTED.col2, ..., INSERTED.coln
VALUES (val1, val2, ..., valn)
Otherwise, you only use SCOPE_IDENTITY()
As mentioned by #David Hall the ##IDENTITY keyword returns the most recently created identity for your current connection, not always the identity for the recently added record in your query and may return an incorrect value. Using MAX(PK) there is a higher chance for an incorrect value and I'd strongly recommend against using it. To avoid the any race conditions I'd suggest that you use SCOPE_IDENTITY() to return the identity of the recently added record in your INSERT SQL Statement or Stored Procedure.
Depends on what you're trying to accomplish. If you want to return the just-generated ID to the ASP.NET code (a typical scenario), then ##identity is your friend. In a high-concurrency situation, mak(PK) is not even guaranteed to be the PK you're after.

In SQLite how does one define a column default to be the current user?

In SQLite database how does one define a column default to be the current user?
Are you talking about the default value for the column? The only valid default values for SQLite are as follows:
The DEFAULT constraint specifies a default value to use when doing an INSERT. The value may be NULL, a string constant or a number. The default value may also be one of the special case-independant keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
SQLite does not have the concept of a user. You don't "log in" with a username/password when you connect to SQLite.
If you need the current operating system user, you should specify this as a string when you insert data from your application.
Simply put: one doesn't. Intending to do so would mean your database has to be aware of the application state, which is kind of backwards.

Resources