Can't create tables in SSDT after Filestream enabling - sql

I have problem with creating tables from SSDT for Visual Studio 2012 after enabling Filestream on Sql Server 2012.
To create I use designer from SQL Server Object Explorer.
After click Update, error apper:
Update cannot proceed due to validation errors. Please correct the
following errors and try again.
SQL71565 :: In-lined constraints do not support the FILESTREAM_ON
clause. The FILESTREAM_ON clause must be added to the table or the
constraint must not be in-lined. SQL71565 :: In-lined constraints do
not support the FILESTREAM_ON clause. The FILESTREAM_ON clause must be
added to the table or the constraint must not be in-lined.
I have this error even on very simple table like
CREATE TABLE [dbo].[Table]
(
[Id] INT NOT NULL PRIMARY KEY
)
What i do wrong? On SSMS this code runs without errors.

I was able to fix issue in my own project by adding explicit FILESTREAM_ON clause on table level.
CREATE TABLE [dbo].[Table]
(
[Id] INT NOT NULL PRIMARY KEY
) FILESTREAM_ON [StreamName]
More info there https://msdn.microsoft.com/en-us/library/ms174979.aspx

Consider adding the constraint explicitly within the Create Statement:
CREATE TABLE [dbo].[Table]
(
[Id] INT NOT NULL,
CONSTRAINT [PK_Table] PRIMARY KEY CLUSTERED
(
[Id] ASC
))

Related

How to force Microsoft Database Project generate ALTER statement for primary key constraint instead of creating temp table?

I have following script for LoginLogo table:
CREATE TABLE [LoginLogo] (
[LoginLogoId] INT IDENTITY (1, 1) NOT NULL,
[LoginId] INT NOT NULL,
[LogoNm] NVARCHAR(255) NULL,
CONSTRAINT [PK_LoginLogo_LoginLogoId] PRIMARY KEY CLUSTERED ([LoginId] ASC),
CONSTRAINT [FK_LoginLogo_LoginId] FOREIGN KEY ([LoginId])
REFERENCES [Login] ([LoginId])
);
GO
CREATE NONCLUSTERED INDEX [IF_LoginLogo_LoginId]
ON [LoginLogo]([LoginId] ASC)
ON [INDX];
I need to change Primary Key Constraint, so I've just changed one line, please see below the change:
CONSTRAINT [PK_LoginLogo_LoginLogoId] PRIMARY KEY CLUSTERED ([LoginLogoId] ASC),
Database project perfectly build changed code, but when it generates database update statement it generates temp table instead of simple ALTER statement. See below generated script:
CREATE TABLE [tmp_ms_xx_LoginLogo] (
[LoginLogoId] INT IDENTITY (1, 1) NOT NULL,
[LoginId] INT NOT NULL,
[LogoNm] NVARCHAR (255) NULL,
CONSTRAINT [tmp_ms_xx_constraint_PK_LoginLogo_LoginLogoId1]
PRIMARY KEY CLUSTERED ([LoginLogoId] ASC)
);
IF EXISTS (SELECT TOP 1 1
FROM [apps].[LoginLogo])
BEGIN
SET IDENTITY_INSERT [apps].[tmp_ms_xx_LoginLogo] ON;
INSERT INTO [apps].[tmp_ms_xx_LoginLogo] ([LoginLogoId], [LoginId], [LogoNm])
SELECT [LoginLogoId],
[LoginId],
[LogoNm],
FROM [LoginLogo]
ORDER BY [LoginLogoId] ASC;
SET IDENTITY_INSERT [tmp_ms_xx_LoginLogo] OFF;
END
DROP TABLE [LoginLogo];
EXECUTE sp_rename N'[tmp_ms_xx_LoginLogo]', N'LoginLogo';
EXECUTE sp_rename N'[tmp_ms_xx_constraint_PK_LoginLogo_LoginLogoId1]',
N'PK_LoginLogo_LoginLogoId', N'OBJECT';
Is it possible to tell Database project to generate ALTER statement instead of creating temp table? How can I force Microsoft Database Project to do that?
Bearing in mind that if you change the clustered index of a table, the table will be rebuilt regardless of whether the script does ALTER TABLE or the SSDT-generated stuff with temp tables, the usual way to solve these problems is to do the ALTER ahead of time
Meaning, you need a script, often referred to as a pre-pre-deploy script (pre-deploy won't work, as it is run post-comparison) that makes the expensive change, so that when the comparison is run the change has already occurred, and hence doesn't get repeated by the dacpac deployment.
This script needs to be run as part of your deployment, before you do any of the sqlpackage stuff. You can specify the change as alter table in this script.
In this particular instance, where the table is going to be rebuilt either way, I can't see it making a great deal of difference to the overall deployment time.

Microsoft SQL - Cannot delete a table, "preparing update script" forever

I am using Visual Studio 2015 and have a data connection to an SQL Database on Azure. I can update things just fine but now I need to delete an entire table altogether and it is stuck at "Preparing update script..." forever. How do I remove the table? It does not have any relations with other tables or anything.
Can you please use SQL Server Management Studio for that? Sometimes, i have the same problem with the VS Explorer, and changing to the SSMS almost always is the solution.
If your SQL Azure is not V12 yet, you can do it from the SQL Azure Silverlight portal (clicking on the Manage button on the SQL Azure dasbhoard), but it is not very viable solution.
If that is the helpful answer, please mark it as a helpful or as the answer. Thanks!
This happens when there is a conflict in your database. (I don't have any article reference/s, this is just from my own experience)
For example, you have a stored procedure like this:
INSERT INTO dbo.Employee_Image
(user_image,
file_extension,
employee_id
)
VALUES
(#user_image,
#file_extension,
#FK_Employee_Image_To_Employee_Table
)
And a table like this:
CREATE TABLE [dbo].[Employee_Image] (
[user_image_id] INT IDENTITY (1, 1) NOT NULL,
[employee_id] INT NULL,
[user_image] VARBINARY (MAX) NULL,
[file_extension] VARCHAR (12) NULL,
PRIMARY KEY CLUSTERED ([user_image_id] ASC),
CONSTRAINT [AK_Employee_Image_employee_id] UNIQUE NONCLUSTERED ([employee_id] ASC),
CONSTRAINT [FK_Employee_Image_To_Employee_Table] FOREIGN KEY ([employee_id]) REFERENCES [dbo].[Employee] ([employee_id])
);
If you remove some of the columns (e.g. the file_extension) like this (and try to update):
CREATE TABLE [dbo].[Employee_Image] (
[user_image_id] INT IDENTITY (1, 1) NOT NULL,
[employee_id] INT NULL,
[user_image] VARBINARY (MAX) NULL,
PRIMARY KEY CLUSTERED ([user_image_id] ASC),
CONSTRAINT [AK_Employee_Image_employee_id] UNIQUE NONCLUSTERED ([employee_id] ASC),
CONSTRAINT [FK_Employee_Image_To_Employee_Table] FOREIGN KEY ([employee_id]) REFERENCES [dbo].[Employee] ([employee_id])
);
The "Preparing Update Script" is going to load like FOREVER. But if you also remove the file_extension column in your stored procedure like this (then the "Preparing Update Script" is going to complete fast):
INSERT INTO dbo.Employee_Image
(user_image,
employee_id
)
VALUES
(#user_image,
#FK_Employee_Image_To_Employee_Table
)

Unique constraint excluding NULLs in UDT field

I am using SQL Server 2012 and I have the following User Defined Table Type
CREATE TYPE [dbo].[IdentifierCodeTable] AS TABLE(
[Id] [dbo].[Identifier] NULL,
[Code] [dbo].[Code] NULL
)
I am trying to enforce that Id must be Unique except for NULL values.
I have the following code and it is working fine for NON NULL values but when I try to insert 2 NULL values it does not allow me to do it.
CREATE TYPE [dbo].[IdentifierCodeTable] AS TABLE(
[Id] [dbo].[Identifier] NULL,
[Code] [dbo].[Code] NULL,
UNIQUE(Id)
)
Is there any way to exclude the NULL values from that UNIQUE Constraint like I can do in the regular indexes with the filter?
I think this is all I need to know (It is SQL Server 2008 but i think it applies to SQL Server 2012 either).
A nonclustered index cannot be created on a user-defined table type unless the index is the result of creating a PRIMARY KEY or UNIQUE constraint on the user-defined table type. (SQL Server enforces any UNIQUE or PRIMARY KEY constraint by using an index.)
Source: https://technet.microsoft.com/en-us/library/bb522526%28v=sql.105%29.aspx

SQL Azure not recognizing my clustered Index

I get the following error when I try to insert a row into a SQL Azure table.
Tables without a clustered index are not supported in this version of
SQL Server. Please create a clustered index and try again.
My problem is I do have a clustered index on that table. I used SQL Azure MW to generate the Azure SQL Script.
Here's what I'm using:
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[tblPasswordReset]') AND type in (N'U'))
DROP TABLE [dbo].[tblPasswordReset]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[tblPasswordReset]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblPasswordReset](
[PasswordResetID] [int] IDENTITY(1,1) NOT NULL,
[PasswordResetGUID] [uniqueidentifier] NULL,
[MemberID] [int] NULL,
[RequestDate] [datetime] NULL,
CONSTRAINT [PK_tblPasswordReset] PRIMARY KEY CLUSTERED
(
[PasswordResetID] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF)
)
END
GO
Why doesn't SQL Azure recognize my clustered Key? Is my script wrong?
Your script only creates the table if it did not exist yet. Perhaps there still is an old version of the table without a clustered index? You can check with:
select * from sys.indexes where object_id = object_id('tblPasswordReset')
If the table exists without the clustered index, you can add one like:
alter table tblPasswordReset add constraint
PK_tblPasswordReset primary key clustered
As far as I can see, your statement does conform to the Azure create table spec.
Be careful if you're using SSIS. I ran into this same problem, myself, but was using SSIS instead of manually inserting the data. By default SSIS will drop and recreate the table, so even though I had it properly defined with a clustered index, my SSIS script failed. On the "Edit Mappings" step in the SSIS wizard you can manually define the table creation script. I just deleted the table gen script there and my import worked.
(I'd leave this as a comment but my post count is too anemic)

SQL Server 2005: Nullable Foreign Key Constraint

I have a foreign key constraint between tables Sessions and Users. Specifically, Sessions.UID = Users.ID. Sometimes, I want Sessions.UID to be null. Can this be allowed? Any time I try to do this, I get an FK Constraint Violation.
Specifically, I'm inserting a row into Sessions via LINQ. I set the Session.User = null; and I get this error:
An attempt was made to remove a relationship between a User and a Session. However, one of the relationship's foreign keys (Session.UID) cannot be set to null.
However, when I remove the line that nulls the User property, I get this error on my SubmitChanges line:
Value cannot be null.
Parameter name: cons
None of my tables have a field called 'cons', nor is it in my 5,500-line DataContext.designer.cs file, nor is it in the QuickWatch for any of the related objects, so I have no idea what 'cons' is.
In the Database, Session.UID is a nullable int field and User.ID is a non-nullable int. I want to record sessions that may or may not have a UID, and I'd rather do it without disabling constraint on that FK relationship. Is there a way to do this?
I seemed to remember creating a nullable FK before, so I whipped up a quick test. As you can see below, it is definitely doable (tested on MSSQL 2005).
Script the relevant parts of your tables and constraints and post them so we can troubleshoot further.
CREATE DATABASE [NullableFKTest]
GO
USE [NullableFKTest]
GO
CREATE TABLE OneTable
(
OneId [int] NOT NULL,
CONSTRAINT [PK_OneTable] PRIMARY KEY CLUSTERED
(
[OneId] ASC
)
)
CREATE TABLE ManyTable (ManyId [int] IDENTITY(1,1) NOT NULL, OneId [int] NULL)
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ManyTable_OneTable]') AND parent_object_id = OBJECT_ID(N'[dbo].[ManyTable]') )
ALTER TABLE [dbo].[ManyTable] WITH CHECK ADD CONSTRAINT [FK_ManyTable_OneTable] FOREIGN KEY([OneId])
REFERENCES [dbo].[OneTable] ([OneId])
GO
--let's get a value in here
insert into OneTable(OneId) values(1)
select* from OneTable
--let's try creating a valid relationship to the FK table OneTable
insert into ManyTable(OneId) values (1) --fine
--now, let's try NULL
insert into ManyTable(OneId) values (NULL) --also fine
--how about a non-existent OneTable entry?
insert into ManyTable(OneId) values (5) --BOOM! - FK violation
select* from ManyTable
--1, 1
--2, NULL
--cleanup
ALTER TABLE ManyTable DROP CONSTRAINT FK_ManyTable_OneTable
GO
drop TABLE OneTable
GO
drop TABLE ManyTable
GO
USE [Master]
GO
DROP DATABASE NullableFKTest