Laravel migration SQL Server default value as function is inserted as string - sql-server

I have this migration for a SQL Server where the default value NEWID() is a function, but once the migration is executed, it is inserted as string 'newid()'.
<?php
//...
Schema::create('event', function (Blueprint $table) {
$table->string('uuid', 100)->nullable()->default('newid()');
});
How do I let know the migration to insert the default value as NEWID() and not as 'newid()'?
Otherwise, I have to go ahead and insert the default value manually as NEWID().

Related

Add existing DB column to model with EF migration

In database there is a column Created, it has a default GETDATE() so it is assigned on insert automatically. This column is currently not in the model class. When I try to add the property to the model class:
[Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime Created { get; set; }
and run update-database, it results in the message:
Unable to update database to match the current model because there are
pending changes and automatic migration is disabled. Either write the
pending model changes to a code-based migration or enable automatic
migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to
true to enable automatic migration. You can use the Add-Migration
command to write the pending model changes to a code-based migration.
When I tried to allow automatic migration, EF tries to create the DB column Created, but this fails, because the column is already there.
Is there a way to correct the model class?
Create the empty migration with -IgnoreChanges and apply to the database.
Add-Migration AddsCreatedProperty -IgnoreChanges
Update-Database
You would also want to add the column for any further databases you would bootstrap from your migrations. So in your empty migration add the sql statement in Up method
SQL(#"
IF NOT EXISTS (
SELECT * FROM sys.columns WHERE
object_id = OBJECT_ID(N'[dbo].[MyTable]') AND name = 'Created'
)
BEGIN
ALTER TABLE MyTable
ADD Created DATETIME NOT NULL DEFAULT (GETDATE());
END
");

Preventing execution of a query if a specific parameter is not present in T-SQL

Using T-SQL and Microsoft SQL Server 2014, I am attempting to find a way to restrict access to a table if a specific parameter is not present.
I can do this on SQL Server 2016 by creating a function which utilises the SESSION_CONTEXT() function and then applying that to my schema.
Since I do not have access to SESSION_CONTEXT() in SQL Server 2014, I am unable to get the value of the specified key in the current session context.
Can anybody suggest a solution which would enable me to circumvent the use of SESSION_CONTEXT() for this scenario (or suggest a better solution)
SQL Server 2016 Compatible T-SQL
CREATE SCHEMA RestrictSchema
GO
CREATE FUNCTION RestrictSchema.DenyWithoutParameter (#Parameter INT)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
SELECT 1 AS VALID_PARAMETER_SUPPLIED
WHERE #Parameter = CAST(SESSION_CONTEXT(N'#Parameter') AS INT)
GO
CREATE SECURITY POLICY RestrictSchema.ScehmaParameterRequirementForTableX
ADD FILTER PREDICATE RestrictSchema.DenyWithoutParameter (#Parameter) ON dbo.TableX
,ADD BLOCK PREDICATE RestrictSchema.DenyWithoutParameter (#Parameter) ON dbo.TableX
GO
Create a table in the RestrictSchema schema,
SessionID
ParameterKey
ParameterValue
TimeToLive
Add an entry to it for every session/connection as needed, then rewrite RestrictSchema.DenyWithoutParameter to select from there, and occasionally garbage collect.

Codeigniter insert query for SQL Server

Code igniter insert statement automatically includes auto increment id in insert query. I am using the same query for inserting into SQL Server table, which causes issues with identity column as we all know. How can I modify the query to remove auto increment id from query? Or use the same query for inserting to SQL Server tables?
You can edit query as you want manually.
Like as following
$sql= "INSERT INTO <tbl>(`ColumnName`) VALUES('value1')";
create function in your model which insert your query something like this...
Model File
<?php
Model{
function insertQuery($sql)
{
//database object make sure you load database in your model
$this->db->query($sql);
// will insert your query as per you write and pass as argument
}
}
?>

How to insert existing data into auto-incrementing field

I used the SMAA to upsize an Access 2010 database to SQL Server 2005.
During the process a number of records were not imported into SQL Server due to some corrupt or illegal data. I have since cleaned up the data that was not imported and saved it to a temporary table in the database. I now want to insert that data into the original table. However, one of the fields, called Task_ID, is an auto-incrementing field. When I run a standard insert query, the resulting data auto-incremented and does not use the imported Task_ID value. Is there a way to get this data into the field without it being changed?
Enable insertion of existing data for the upload, then turn it off again.
http://technet.microsoft.com/en-us/library/aa259221(v=sql.80).aspx explains how:
Basically it is a SQL commmand. The syntax is:
SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }
Wrap the INSERT statements with the SET IDENTITY_INSERT command:
SET IDENTITY_INSERT [table_name] ON
...
SET IDENTITY_INSERT [table_name] OFF

Sybase Advantage New AutoInc after SQL Insert

Anyone knows how to retrieve a new AutoInc that gets written after an ODBC INSERT?
Is there a variable I have access to just like SQL Server?
Right now, I'm using :
SELECT MAX(myautoincfield) AS mylastkey FROM anytable
in order to retrieve my new key.
The scalar function LastAutoinc can retrieve it efficiently:
select LastAutoinc(statement) as mylastkey from system.iota;
The global variable ##identity identifies the last value inserted into an IDENTITY column by the current session.
You may do the following:
select ##identity
in order to retrieve the new key.

Resources