I am trying to test our site on the Windows Azure platform to see if it will work. I've worked through most of the errors, but I haven't been able to get past the following.
Element Column: [dbo].[Subscribers].[ID] has an unsupported property IsRowGuidColumn set and is not supported when used as part of a data package.
I did a lot of searching and can't find anything about this particular error. I did see some stuff about GUIDs and Azure, but nothing that's helped.
I have five tables that use a GUID/Uniqueidentifier as their primary key because they are publicly visible.
Since RowGUID column is not supported in Windows Azure SQL Database recommended alternative is to use uniqueidentifier as the column type and then use NEWID() to generate guids at insert time.
CREATE TABLE MyTable (
MyID UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
Name VARCHAR(10))
INSERT INTO MyTable (name) VALUES ('string1')
INSERT INTO MyTable VALUES (newid(), 'string2')
Related
I'm using JHipster 4. I've create a simple entity just with a "name" property and when I try to create a entity from UI I get the folowing error. (I'm using Microsoft SQL Server).
In think the important part of the error is :
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert explicit value for identity column in table 'tenant' when IDENTITY_INSERT is set to OFF.
Direct answer appears to be that you just need to configure jhipster to have identity_insert on, per this link: https://jhipster.github.io/tips/004_tip_using_ms_sql_server.html
Adding the identityInsertEnabled="true" is the same as wrapping your Inserts with IDENTITY_INSERT ON and IDENTITY_INSERT OFF which will allow you to insert the project autogenerated identities directly. This is why we are using the MS SQL Server Liquibase for.
The error tells you everything you need to know. You are trying to insert a value in to an identity column. This is not allowed, as the column is supposed to be automatically populated unless you explicitly turn it off temporarily.
Documentation on what identity_insert is here: https://msdn.microsoft.com/en-us/library/ms188059.aspx
You can turn it off using the command set identity_insert SchemaName.TableName off though you had better be very confident you are inserting the correct data.
I would recommend you do some investigation with whoever manages your database as to why that column is an identity column and whether or not you should be inserting into it at all.
Assume a table:
create table test(ID int identity(1,1)
,Name nvarchar(100)
)
ALL of these insert statements will throw an error:
insert into test(ID,Name)
select ID
,Name
from OtherTable
insert into test(ID,Name)
select null as ID
,'TestName' as Name
insert into test(ID,Name)
values(99
,'Name'
)
Whereas these will work, automatically generating a new, unique value for the ID column:
insert into test(Name)
select Name
from OtherTable
insert into test(Name)
select 'TestName' as Name
insert into test(Name)
values('Name')
In short, you need to insert nothing into the identity column. Not a blank string, not a null value, but absolutely nothing at all.
I'm application switching databases for my application from MySQL to SQLServer.
Today I implement a custom id generation strategy defined in an abstract class all POJOs use. This works, but I am only able to generate an id via the application.
With this database migration, I'd like, after creating the schema, define somewhere all the 'id' columns for all tables to use a procedure that returns a SELECT NEWID(); query.
Is this possible? How?
I like to define the ID columns with a default constraint:
create table a
(
id
uniqueidentifier not null
constraint [a.id.default.newid]
default( newid() )
constraint [a.id.primarykey]
primary key clustered,
--> other columns...
)
This way, you can either take an app-generated ID or let the database assign it, depending on your needs. Your primary key constraint (or unique constraint) enforces uniqueness...which is important if you allow incoming IDs from the apps.
In the scripts...where you don't necessarily have the need to generate the ID, don't specify a value...and the default kicks in. You can insert newid(), too...
insert a
select newid()
from b
...and SQL Server knows to call newid() for each row
I have 3 tables
Project : ProjectID (primary key)
Bugs: ProjectID, BugID (primary key)
BugLogs: BugID, BugLogID (primary key)
There are:
Multiple bugs on a project
Multiple bug logs on the bugs
How would I insert a project that has a bug (s) and then bug Logs on bugs efficiently into these tables?
Thanks
Well here you need to write individual insert statements. Joins will only be used when querying the data..
You can do the following if bugid is an identity column:
DECLARE #bugid bigint
INSERT INTO Bugs (Projectid,other COLUMNS...)
VALUES (values1,VALUES....)
SELECT #bugid= SCOPE_IDENTITY()
INSERT INTO BugLogs(Bugid,other COLUMNS...)
VALUES(#bugid,....)
Alternatively you can use Output clause to get the bugid. This will work in all scenarios:
DECLARE #bugid bigint
INSERT INTO Bugs (Projectid,other COLUMNS...)
OUTPUT INSERTED.BugId INTO #bugid
VALUES (values1,VALUES....)
INSERT INTO BugLogs(Bugid,other COLUMNS...)
VALUES(#bugid,....)
Joins have nothing to do with Inserts. Joins only come into play when you want to query the data.
If you have your declarative referential integrity in place, then you are going to have to insert records in the following order: Project, Bugs, BugLogs.
If you are working in Microsoft SQL Server, and are using identity columns, after you insert a row, you can use the scope_identity() function to retrieve the primary key assigned and use that to set you foreign keys.
I have a number of tables I need to convert from mySQL to SQL Server.
An Example of a mySQL Table is
CREATE TABLE `required_items` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique Barcode ID',
`fk_load_id` INT( 11 ) NOT NULL COMMENT 'Load ID',
`barcode` VARCHAR( 255 ) NOT NULL COMMENT 'Barcode Value',
`description` VARCHAR( 255 ) NULL DEFAULT NULL COMMENT 'Barcode Description',
`created` TIMESTAMP NULL DEFAULT NULL COMMENT 'Creation Timestamp',
`modified` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modified Timestamp',
FOREIGN KEY (`fk_load_id`) REFERENCES `loads`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET ascii COLLATE ascii_general_ci COMMENT = 'Contains Required Items for the Load';
And a trigger to update the created date
CREATE TRIGGER required_items_before_insert_created_date BEFORE INSERT ON `required_items`
FOR EACH ROW
BEGIN
SET NEW.created = CURRENT_TIMESTAMP;
END
Now I need to create tables similar to this in SQL Server. There seems to be a lot of different data types available so I am unsure which to use.
What data type should I use to the primary key column
(uniqueidentifier, bigint, int)?
What should I use for the timestamps
(timestamp, datatime, datetime2(7))?
How should I enforce the created
and modified timestamps (currently I am using triggers)?
How can I enforce foreign key constraints.
Should I be using Varchar(255) in SQL Server? (Maybe Text, Varchar(MAX) is better)
I am using Visual Studio 2010 to create the tables.
First of all, you can probably use PHPMyAdmin (or something similar) to script out the table creation process to SQL Server. You can take a look at what is automatically created for you to get an idea of what you should be using. After that, you should take a look at SSMS (SQL Server Management Studio) over Visual Studio 2010. Tweaking the tables that your script will create will be easier in SSMS - in fact, most database development tasks will be easier in SSMS.
What data type should I use to the primary key column (uniqueidentifier, bigint, int)?
Depending on how many records you plan to have in your table, use int, or bigint. There are problems with uniqueidentfiers that you will probably want to avoid. INT vs Unique-Identifier for ID field in database
What should I use for the timestamps (timestamp, datatime, datetime2(7))?
timestamps are different in SQL Server than in MySQL. Despite the name, a timestamp is an incrementing number that is used as a mechanism to version rows. http://msdn.microsoft.com/en-us/library/ms182776%28v=sql.90%29.aspx . In short though, datetime is probably your best bet for compatibility purposes.
How should I enforce the created and modified timestamps (currently I am using triggers)?
See above. Also, the SQL Server version of a "Timestamp" is automatically updated by the DBMS. If you need a timestamp similar to your MySQL version, you can use a trigger to do that (but that is generally frowned upon...kind of dogmatic really).
How can I enforce foreign key constraints.
You should treat them as you would using innoDB. See this article for examples of creating foreign key constraints http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/
Should I be using Varchar(255) in SQL Server? (Maybe Text, Varchar(MAX) is better)
That depends on the data you plan to store in the field. Varchar max is equivalent to varchar(8000) and if you don't need varchar(255), you can always set it to a lower value like varchar(50). Using a field size that is too large has performance implications. One thing to note is that if you plan to support unicode (multilingual) data in your field, use nvarchar or nchar.
In all my searching I see that you essentially have to copy the existing table to a new table to chance to identity column for pre-2008, does this apply to 2008 also?
thanks.
most concise solution I have found so far:
CREATE TABLE Test
(
id int identity(1,1),
somecolumn varchar(10)
);
INSERT INTO Test VALUES ('Hello');
INSERT INTO Test VALUES ('World');
-- copy the table. use same schema, but no identity
CREATE TABLE Test2
(
id int NOT NULL,
somecolumn varchar(10)
);
ALTER TABLE Test SWITCH TO Test2;
-- drop the original (now empty) table
DROP TABLE Test;
-- rename new table to old table's name
EXEC sp_rename 'Test2','Test';
-- see same records
SELECT * FROM Test;
we cannot add identity to an existing column using sql command but we can do it using GUI.
Right click on the table - design - select the column on which you want to add identity.
go to the properties available below. find the identity specification and set it to yes.
save the table.
if it is not saved the go to tools from the menu - options - table designer - uncheck the checkbox prevent saving changes. now you can save the table modifications.
now your existing table had identity.
In all of the new feature documents I read about 2008, adding identity to an existing column was not a feature I recall. The solution you've found is correct and I think the process of adding identity increment to a column automatically would be only rarely useful.
Well you can do something like this.
ALTER TABLE my_table ADD ID_COLUMN INT IDENTITY (1,1) NOT NULL
You can add the IDENTITY property to an existing column using the GUI of Enterprise Manager / Management Studio.
In SQL 2005 and earlier, you could not modify an existing column to become an identity column. I deem it very very unlikely that MS changed that in 2008.