Sybase IQ: load table with autoincrement column - sybase

I need to transfer a table from database A to database B, the table has an autoincrement column. The content of the table in the new database has to be identical to the table in the original database, including the values in the autoincrement column.
The following LOAD TABLE, where col1 is the autoincrement column, doesn't work:
set identity_insert TableName on;
LOAD TABLE TableName
(col1,col2,col3)
FROM 'file.csv';
I get the following error:
Cannot insert or update Column col1: set option 'identity_insert' to the specific table name containing the identity column to be modified.
Still, I'm setting the identity_insert option. What's wrong with this command?

Please try
SET TEMPORARY OPTION IDENTITY_INSERT = 'MyTable';
This has been discussed on SCN

DEFAULTS OFF looks like it should be used as an option if you want to ovverride the default identity (autoincrement) value.
see: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1530/doc/html/san1281564935243.html
If that doesn't work, and this is just a one time thing... the simplest way to accomplish this would be to load table into TableName_work that has no autoincrement, then insert the data into your destination via a select statement with identity_insert on.

Related

SQL Insert Into cannot insert NULL

I have set some script that inserts data from an XML file into a SQL database. I am getting the following error.
Cannot insert the value NULL into column 'fighterID', table 'MMA Database.dbo.FIGHTERStemp'; column does not allow nulls. INSERT fails.
I have fighterID set as the primary key and will not allow NULLS. My intention is to have it number each row as they are inserted. I found one answer that advises to modify the column properties to be the identifier. However it will not let me adjust the columns properties without dropping and adding the table again.
I can do that - but what is the SQL syntax to set the identity specification settings? Am I going about this the right way?
It's pretty simple, just set the field with datatype INT (integer) and follow it with keyword IDENTITY. You can include the seed and increment values; e.g. start at 1, increment by 1, or just use the keyword IDENTITY for a 1,1 default.
CREATE TABLE MMA (FighterID INT IDENTITY (1,1), FighterInfo VARCHAR(MAX))
While inserting data into Primary key you can check the previous max id value and then increment it to next value before you insert a new row.
In SQL, you need to drop table before altering its specification. You can do this by taking backup into temp table then drop your main table and then re insert data from temp table.

How to create a SQL Server table with a column and its values to be generated automatically as a GUID

I need to design a table in SQL Server having some columns, one of these columns (ID column and use sequential uniqueidentifier) should automatically populate its data when inserting other column data.
The values of the ID column should be generated automatically when insertion happens.
Please help me to do this, any help is appreciated.
NB: I am new to this step by step approach will be more helpful
Just create a table with a column ID of datatype uniqueidentifier and set it's default value to newsequentialid():
Then, when you go insert rows into that table, just omit the ID column from the list of columns you're inserted values into:
INSERT INTO dbo.YourTable(ColA, ColB, ....., ColX)
VALUES(.., .. ,. ...)
If you don't explicitly insert a value into ID, the default specification (newsequentialid()) will be used .
As per Marc_s's comment, you should use NEWSEQUENTIALID()
CREATE TABLE myTable (ColumnA uniqueidentifier DEFAULT NEWSEQUENTIALID());
See NEWSEQUENTIALID (Transact-SQL)

SQL Server - Autoincrementing on a generated table

Here's what I'm trying to do:
SELECT * INTO new_table FROM old_table WHERE 1=2;
ALTER TABLE new_table ADD CONSTRAINT pk_new_table_id PRIMARY KEY(id);
SET IDENTITY_INSERT new_table ON
Basically, I want to create a new table based on the structure of an existing one, then turn on autoincrementing for the ID field. However, when I try to insert rows I get this:
Table 'new_table ' does not have the identity property. Cannot perform SET operation.
Can anyone enlighten me as to what I'm doing wrong or how I should actually be going about this?
The table you create doesn't have an identity column. If you inspect the code you posted, you will notice that nowhere is an identity declared. I think you are confusing the concept of PRIMARY KEY and IDENTITY. They are distinct and unrelated. You cannot create a table with identity column using SELECT ... INTO ...
the column needs to be specified with " identity(1,1)" option. First 1 is the starting point and second is the increment.
set identity_insert on does not actually do what you think it does. It disables autoincrement (temporarily) so that you can hard code values in there.
Here's more info on that... http://msdn.microsoft.com/en-us/library/aa259221(v=sql.80).aspx
First, you have to create the new table without the use of "select ... into" and specify the column as an identity column.
Use the "set identity_insert .. on" before you copy the data over. Then, "set identity_insert ... off" when you're done.
If you want to cheat on the first part, you can use the management studio. Right click original table... goto the designer. Add identity option for the desired column. Then save off the script but do not commit. Might want to manipulate the newly generate script to create the new table. There are other more sophisticated solutions if you're going to have to do this repeatedly. But this by far is the simplest.
Here could be the problem:
ALTER TABLE new_table ADD CONSTRAINT pk_new_table_id PRIMARY KEY(id, idColumn);
Specify the id column.

Help needed with "Cannot insert explicit value for identity column" Error

I have a table Table1 on which I have created an insert/update trigger. This trigger insert records of inserts/updates on Table1 into Table1_history. To test whether the trigger is inserting records into Table1_history, I have inserted some test data into Table1 and I am receiving the error:
Cannot insert explicit value for identity column in table 'Table1_history' when IDENTITY_INSERT is set to OFF
Schema of Table1 and Table1_history are identical except an additional 'inserted_on_date' field in Table1_history.
I have set the Identity Insert to ON and the trigger works. But do I have to do this every time I need to insert a new record in Table1?
Put the IDENTITY INSERT ON into the trigger itself, it only needs to be active while you are pushing values into table1_history, because that is where the problem is.
Or, in Table1_history, don't make the pk field an IDENTITY, it does not need to be because you are supplying the values from Table1.
I assume your 'inserted_on_date' field is a datetime. You should be able to turn off the identity insert and set the default value for this column to GETDATE(), which will set any newly inserted records to the datetime the server has when the record is inserted.
You don't mention what database but I assume it's SQL Server 2008.
The set identity on command is session specific. So yes, you need to issue it each time.
Instead, you should remove the identity specification from the history table. You don't need it.

How do I add the identity property to an existing column in SQL Server

In SQL Server (in my case, 2005) how can I add the identity property to an existing table column using T-SQL?
Something like:
alter table tblFoo
alter column bar identity(1,1)
I don't beleive you can do that. Your best bet is to create a new identity column and copy the data over using an identity insert command (if you indeed want to keep the old values).
Here is a decent article describing the process in detail:
http://www.mssqltips.com/tip.asp?tip=1397
The solution posted by Vikash doesn't work; it produces an "Incorrect syntax" error in SQL Management Studio (2005, as the OP specified). The fact that the "Compact Edition" of SQL Server supports this kind of operation is just a shortcut, because the real process is more like what Robert & JohnFX said--creating a duplicate table, populating the data, renaming the original & new tables appropriately.
If you want to keep the values that already exist in the field that needs to be an identity, you could do something like this:
CREATE TABLE tname2 (etc.)
INSERT INTO tname2 FROM tname1
DROP TABLE tname1
CREATE TABLE tname1 (with IDENTITY specified)
SET IDENTITY_INSERT tname1 ON
INSERT INTO tname1 FROM tname2
SET IDENTITY_INSERT tname1 OFF
DROP tname2
Of course, dropping and re-creating a table (tname1) that is used by live code is NOT recommended! :)
Is the table populated? If not drop and recreate the table.
If it is populated what values already exist in the column? If they are values you don't want to keep.
Create a new table as you desire it, load the records from your old table into your new talbe and let the database populate the identity column as normal. Rename your original table and rename the new one to the correct name :).
Finally if the column you wish to make identity currently contains primary key values and is being referenced already by other tables you will need to totally re think if you're sure this is what you want to do :)
There is no direct way of doing this except:
A) through SQL i.e.:
-- make sure you have the correct CREATE TABLE script ready with IDENTITY
SELECT * INTO abcTable_copy FROM abcTable
DROP TABLE abcTable
CREATE TABLE abcTable -- this time with the IDENTITY column
SET IDENTITY_INSERT abcTable ON
INSERT INTO abcTable (..specify all columns!) FROM (..specify all columns!) abcTable_copy
SET INDENTITY_INSERT abcTable OFF
DROP TABLE abcTable_copy
-- I would suggest to verify the contents of both tables
-- before dropping the copy table
B) Through MSSMS which will do exactly the same in the background but will less fat-fingering.
In the MSSMS Object Explorer right click the table you need to modify
Select "design" Select the column you'd like to add IDENTITY to
Change the identity setting from NO -> YES (possibly seed)
Ctr+S the table
This will drop and recreate the table with all original data in it.
If you get a warning:
Go to MSSMS Tools -> Options -> Designers -> Table and database Designers
and uncheck the option "Prevent saving changes that require table re-creation"
Things to be careful about:
your DB has enough disk space before you do this
the DB is not in use (especially the table you are changing)
make sure to backup your DB before doing it
if the table has a lot of data (over 1G) try it somewhere else first
before using in real DB
Create a New Table
SELECT * INTO Table_New FROM Table_Current WHERE 1 = 0;
Drop Column from New Table
Alter table Table_New drop column id;
Add column with identity
Alter table Table_New add id int primary key identity;
Get All Data in New Table
SET IDENTITY_INSERT Table_New ON;
INSERT INTO Table_New (id, Name,CreatedDate,Modified)
SELECT id, Name,CreatedDate,Modified FROM Table_Current;
SET IDENTITY_INSERT Table_New OFF;
Drop old Table
drop table Table_Current;
Rename New Table as old One
EXEC sp_rename 'Table_New', 'Table_Current';
alter table tablename
alter column columnname
add Identity(100,1)

Resources