Can't set auto-increment via SQL Server Express Management Studio? - sql-server

I just tried inserting value in to a database and that work. Now I insert again and I get an error for identical primary key.
I can't find any option to alter it to be auto-increment.
I'm updating the table via Linq-To-Sql.
User u = new User(email.Text, HttpContext.Current.Request.UserHostAddress,
CalculateMD5Hash(password.Text));
db.Users.InsertOnSubmit(g);
db.SubmitChanges();
I didn't fill in the user_id and it worked fine the first time. It became zero.
Trying to add a second user, it wants to make the ID 0 again.
I could query the database and ask for the highest ID, but that's going to far if you know about auto-increment.
How can I turn this on? All I can find are scripts for table creation. I'd like to keep my existing table and simply edit it.

How is your Linq-to-SQL model defined?? Check the properties of the user_id column - what are they set to??
In your Linq-to-SQL model, be sure to have Auto Generated Value set to true, Auto-Sync set to OnInsert, and the server data type should also match your settings (INT IDENTITY),
In SQL Server Management Studio, you need to define the user_id column to be of type INT IDENTITY - in the visual table designer, you need to set this property here:

It is zero because you have a integer for a primary key column type. To use auto-increment, set tables identity column to the ID (selected in the table properties)

Would probably be easier to edit the database using VS if you have a version that will work for, otherwise if you have to edit it in management studio see this article:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx

Or you can increment the user_id manually and pass it to the insert function if you cannot alter the property/table field description

Related

SQL Server syntax for NEWID()

I need some help with some SQL Server 2014 syntax.
I want to use the NEWID() function to create the UUID value in column CareplannersUUID. Each time a record is inserted, I want a new UUID value to appear in column CareplannersUUID.
But I am not sure about the syntax of the command. I think it is something like:
ALTER TABLE CareplannersMembers
ADD CONSTRAINT DF_table_id DEFAULT (NEWID()) FOR CareplannersUUID
I am wondering specifically, is there another value I should add for DF_table_id?
Notes about the table in question:
Table name: CareplannersMembers
UUID column name: CareplannersUUID
Thank you for your help.
Eric
I looked at the similar questions, but am not able to determine, from information presented there, whether or not I am using the correct syntax for my SQL Server statement.

ArcGIS SQL Server ArcSDE: Versioning vs Identity Column

[I am asking here instead of GIS Stackexchange because this maybe more of a SQL Server issue?]
I have SQL Server ArcSDE connection in which data is batch inserted via some scripts. Currently, anytime there is a new row of data then an 'OBJECTID' column, set to INT and Identity Column increases by number 1. So far so good. Except I need to enable "versioning" on the table.
And so I follow this: http://resources.arcgis.com/en/help/main/10.1/index.html#//003n000000v3000000
but get errors because ArcGIS is complaining about the Identity column, per: http://support.esri.com/cn/knowledgebase/techarticles/detail/40329 ; and when I remove the Identity attribute to the column then the column value becomes NULL--not good.
So, in my scenario, how I can I increase the value of OBJECTID by 1 number as auto-increment? I supposed, I can just insert some GUID into the 'OBJECTID' field through the script? Also, if I follow the GUID route then I am not sure if I will be able to add rows manually via ArcGIS Desktop on occasional basis?
Thanks!
Update 1 Okay, so changed the OBJECTID field to a 'uniqueidentifier' one with a default GUID value and now I am able to enable "versioning" using ArcGIS Desktop. However, ArcGIS is expecting GUID to be an INT data type--and so no go?
In light of my "update 1" in the Question above I managed to take care of this by inserting an INT value for OBJECTID during the batch insertions per the following: How to insert an auto_increment key into SQL Server table
so per the above link, I ended doing:
INSERT INTO bo.TABLE (primary_key, field1, fiels2) VALUES ((SELECT ISNULL(MAX(id) + 1, 0) FROM bo.Table), value1, value2)
EXCEPT in my case the IDENTITY remains not 'ON' at all either in the database or, unlike the above link, I didn't have to set Identity On/Off during batch insertions; Works for some reasons anyway!

Database Access Mdb how to set a default value to existing column via SQL

I have a 12 years old mdb database and I was asked to add a new column to a table and set to a default value of "1".
My knowledge of asp/mdb is close to zero. I also have no Access or similar softwares.
I tried with:
ALTER TABLE Members ADD COLUMN Privacy Double Default 1
but generates error:
Error: An action query cannot be used as a row source.
Then I tried with:
ALTER TABLE Members MODIFY COLUMN Privacy VARCHAR(4) NOT NULL DEFAULT 'Yes';
but this also triggers another error:
Microsoft JET Database Engine error '80040e14'
How can I set an existing column to a default value?
Should I use an offline tool? If so which one?
Note: I also used an online tool to create the new column but it has no option to set a default value.
So I can either create the new column with the tool and set a default value with SQL, or do the creation of the column with the default value still with SQL.
I solved using AxBase from SourceForge.net.
I could properly open the *.mdb database and run the above SQL commands and they worked perfectly:
ALTER TABLE Members ALTER COLUMN Privacy SET DEFAULT 1
hi your access database is may be in office 2003 or office xp.
You can try to enable sql syntax and then try to run your query.
Tools -> Options -> Tables/Queries -> (At the bottom right:) Sql Server Compatible Syntax - turn option on for this database.
ALTER TABLE Members ADD COLUMN Privacy number Default 1

how can i change a field in my SQL database from numeric(18) to varchar(10)

I have a zipcode field in a database that I just took over. Previously, it was set as a numeric field with 18 precision but I am trying to convert it over to varchar(10).
I need to make this change because the linq fields are coming in as decimal and are causing issues and i want to change the linq fields to simply be strings.
I tried this in SQL server enterprise manager but i get this error, saying:
that the table will have to be dropped and recreated. you have either made changes to a table that can't be recreated or enable the option to prevent saving changes that require a table recreation
Any suggestions?
to enable that option in SQL management studio uncheck the following option...
Tools / Options / Designers / Table and Database Designers / Prevent saving changes that require table re-creation
You could also run an alter statement to change your datatype (as long as all of your data will fit in a varchar(10) column).
ALTER TABLE MyTable
ALTER COLUMN MyZipCodeColumn VARCHAR(10)
Are you using MS-SQL 2008? Changes that require the table to rebuilt are blocked by default.
Click Tools->Options, then Designers. Uncheck "Prevent saving changes that require table re-creation".
Then you can change your column using the designer.
Screenshots on how to do it:
http://pragmaticworks.com/community/blogs/brianknight/archive/2008/06/04/sql-server-2008-designer-behavior-change-saving-changes-not-permitted.aspx

How can I change a field in a SQL server database that's set to "Read Only Cell"?

I have a SQL database that has a table with a field set to "Read Only" when I look at it through Microsoft SQL Server Management Studio Express.
I need to change some data within that field manually but I can't see any properties that I can change that will let me override this.
Will I need to write a sql script on the table to do this or is there something that I am missing ?
What is the datatype of the field? You may not be able to "type" into it if its of an ntext or image datatype and management studio can't handle the size of it.
In that case you might have no option but to perform an update as follows.
UPDATE TableName SET ColumnName = 'NewValue' WHERE PrimaryKeyId = PrimaryKeyValue
The field is most likely "read-only" because it contains a calculated value.
If that's the case, you would have to change calculation in the table definition to change it's value.
This problem will occur when you set a particular field as Primary Key and you set it into 'Is Identity' is true, that means that field will automatically incremented whenever an insertion is takes placed...So better to check whether it is auto increment or not.. If it is ,then change that property 'Is Idenitity' as false.
In an SQL query I had once, the query I used to generate the table to edit included a join to a table on a "Server Object", specifically a linked server. This marked the cells as read only, even though the table on which I was actually going to change the data wasn't on the linked server.
My resolution: Luckily I was able to adjust the query so I didn't need to do the JOIN with a linked table and then I could edit the cells.
Suggestion: Check your query for linked servers or other odd statements that may lock your table.
Use trigger in order to prevent this column updating:
CREATE TRIGGER UpdateRecord ON my_table
AFTER UPDATE AS UPDATE my_table
SET [CreatedDate] = ((SELECT TOP 1 [CreatedDate] FROM Deleted d where d.[id]=[id]))

Resources