How to add two columns to a table - sybase

I have a table. I want to add two columns to this table.
I tried it like this:
SELECT *
into
dbo.mytable_Audit
from dbo.mytable
But, I need two colums to mytable_Audit, how do I add them in sybase 15-2 ASE?

You can also add columns to existing tables by using the alter table command. For example, to add two new int columns to mytable_Audit, one with a default value, and the other as NULL:
alter table mytable_Audit add col1 int default 0, col2 int NULL

You get as much columns in your new table, as you have in the existing one. Name, type and order will be inherited from dbo.mytable.
To add an additional column to the output, just add a column to the select:
select 0 as col1, * into dbo.mytable_AUDIT from dbo.mytable;
This adds a column of type integer as the first column in the new table.

Related

Insert from temp table to a table with identity column

I'm grabbing some rows from a table, manipulating them in a temp table, and then looking to insert them as new rows into my original table.
However, I'm running into an issue with the identity column, even when I don't have the identity column on my temp table. The identity column is an auto-incrementing int.
This seems like a simple thing I'm way overthinking.
select top 0 *
into #TestTable
from OriginalTable;
...
--insert and manipulate records
...
ALTER TABLE #TestTable
DROP COLUMN MyIdentityColumn;
DECLARE #InsertedRows TABLE (NewSeqNum INT);
INSERT INTO OriginalTable
OUTPUT MyIdentityColumn INTO #InsertedRows(NewSeqNum)
SELECT * FROM #TestTable
but I get this error:
An explicit value for the identity column in table 'OriginalTable' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I absolutely do not want to set an explicit value, I want it to insert and give me the new identity (via #InsertedRows)
If you don't want to keep the id of inserted records, then you need to specify all your columns but the id column in the select. As general good practice, dont select *, always specify the columns you want to retrieve-insert.
INSERT INTO OriginalTable (col1, col2, col3...)
OUTPUT MyIdentityColumn INTO #InsertedRows(NewSeqNum)
SELECT (col1, col2, col3...) FROM #TestTable
If I'm understanding you, I think your problem is that you're trying to insert '*' into the original table - which means all of your columns from the temp table. Including your ID column (which you don't want to insert, because you're wanting it to auto-generate.)
Instead, I'd suggest doing something like this:
Select [ColumnB],[ColumnC],[ColumnD],[Etc] into your temp table
Select [ColumnB],[ColumnC],[ColumnD],[Etc] into your original table.
... aka, spell out the columns explicitly, and omit the Identity column.

Create permanent table based on temporary table in SQL Server

I have a temp table which aggregates columns from multiple tables.
I would like to convert this temp table into a permanent table without explicit specifying the column names and their types.
Don't know if i have explained this well enough
You can use SELECT ... INTO:
SELECT *
INTO dbo.normal_table
FROM #temp_table
-- WHERE 1 = 2; --add if you only want table structure and not actual data
Things to check after table creation:
IDENTITY column and current value (may need reseeding)
DEFAULT values of column(if temp table has defaults, normal table needs to be ALTERed)
COLLATION tempdb may have different collation than your database
size of columns, precision and scale (VARCHAR,NVARCHAR,CHAR, DECIMAL..) if SELECT contains expressions
If temp table does not contain IDENTITY column you can add one using:
SELECT ID = IDENTITY(INT,1,1)
,col1 = NULL -- NULL by default are treated as INT so you may need casting
,col2 = CAST(NULL AS DECIMAL(38,10))
,t.*
INTO dbo.table_name
FROM #temp t

Insert a FIELD TO A existing table with a condition

I am new to SQL, I am trying to add a column to my table from another table with certain condition
I have Employees2013 table that has StaffNumber column, and I have an Employees table that has StaffNumber and Title columns.
What I am trying to is create a new column called Title in Employees2013 and select title from Employees where Employees2013.StaffNumber = Employees.StaffNumber.
I tried this but it didn't work:
insert into Employees2013(Title)
select e.Title
from LandornetSQL.dbo.Employees e, Employees2013 f
where e.StaffNumber = f.StaffNumber
I get this error:
Cannot insert the value NULL into column 'StaffNumber', table 'xDevProjects.NA\OnderO.Employees2013'; column does not allow nulls. INSERT fails.
Anyone has any idea?
Insert only inserts records, if you want a new column, you must add the column to the table.
ALTER TABLE Employees2013 ADD Title VARCHAR(100)
Then you can update the table, setting the Title column
UPDATE Employees2013 SET Title = Employees.Title
FROM Employees
WHERE Employees.StaffNumber = Employees2013.StaffNumber;

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)

Insert multiple rows of default values into a table

I have a table with a single column, which is an auto-generated identity
create table SingleIdTable (
id int identity(1,1) not null
)
I can insert a single row with an auto-generated id with:
insert into SingleIdTable default values
I want to insert many rows and use the output syntax to get their ids, something like:
insert into SingleIdTable
output inserted.Id into #TableOfIds
select (default values) from SomeOtherTable where Attribute is null
Where the intention is to insert a row into SingleIdTable for each row in SomeOtherTable where Attribute is null using an auto-generated id. The above doesn't work, but how could I do it. I note that if my table had more than just a single column I could do it, but I can't select empty rows which is what I really want to do.
I can't change the definition of SomeOtherTable.
If SQL Server 2008+ you can use MERGE for this. Example syntax below.
MERGE INTO SingleIdTable
USING (SELECT *
FROM SomeOtherTable
WHERE Attribute IS NULL) T
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT
DEFAULT VALUES
OUTPUT INSERTED.id;
I'm not sure what practical use this single column table has though?
you did not specify which version of SQL Server you are on. If you happen to be on SQL 2012 you probably can replace you SingleIdTable with a sequence: http://msdn.microsoft.com/en-us/library/ff878091.aspx

Resources