January 2, 2015 at 3:47 am
Hi,
How to create table without identity by using * into option.
ex: Select * Into Employee_History From Employee.
I don't want identity column in History table, But columns and datatypes should be same.
Thanks in advanced.
January 2, 2015 at 4:01 am
I'm really not sure what you want to do here but it's best to include the columns you want by name rather than using SELECT * and trying to exclude things.
How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537
January 2, 2015 at 9:33 am
Agreed, just don't include that col in the list.
Watch my free SQL Server Tutorials at:
http://MidnightDBA.com
Blog Author of:
DBA Rant – http://www.MidnightDBA.com/DBARant
January 4, 2015 at 11:02 pm
I am creating history table dynamically, by using INTO clause. After that I would like to purging data from Main table to History table.
Requirement: Create history table , and purge data into history table.
Table Name is same , but different clients having different table structure(columns are different) , so I am not going to create table with hard coded script. Should crate history table dynamically from main table. But History table doesn't want Identity column. If History table having identity column, In Purge script , Always I should maintain Identity ON/OFF and columns in select statement . So that purge script also dynamic. i.e. column building.
So to reduce dynamic in purge script , i want to create history table without IDENTITY(Column name should be there).
Is there any another ways to create table without Identity ?
January 4, 2015 at 11:39 pm
Dear Pramana,
Listed is the script taken from http://www.c-sharpcorner.com/UploadFile/67b45a/how-to-generate-a-create-table-script-for-an-existing-table/
I have modified it to omit the identity property.
DECLARE
@object_name SYSNAME
, @object_id INT
, @sql NVARCHAR(MAX)
SELECT
@object_name = '[' + OBJECT_SCHEMA_NAME(o.[object_id]) + '].[' + OBJECT_NAME([object_id]) + ']'
, @object_id = [object_id]
FROM (SELECT [object_id] = OBJECT_ID('dbo.shafat', 'U')) o
SELECT @sql = 'CREATE TABLE ' + @object_name + CHAR(13) + '(' + CHAR(13) + STUFF((
SELECT CHAR(13) + ' , [' + c.name + '] ' +
CASE WHEN c.is_computed = 1
THEN 'AS ' + OBJECT_DEFINITION(c.[object_id], c.column_id)
ELSE
CASE WHEN c.system_type_id != c.user_type_id
THEN '[' + SCHEMA_NAME(tp.[schema_id]) + '].[' + tp.name + ']'
ELSE '[' + UPPER(tp.name) + ']'
END +
CASE
WHEN tp.name IN ('varchar', 'char', 'varbinary', 'binary')
THEN '(' + CASE WHEN c.max_length = -1
THEN 'MAX'
ELSE CAST(c.max_length AS VARCHAR(5))
END + ')'
WHEN tp.name IN ('nvarchar', 'nchar')
THEN '(' + CASE WHEN c.max_length = -1
THEN 'MAX'
ELSE CAST(c.max_length / 2 AS VARCHAR(5))
END + ')'
WHEN tp.name IN ('datetime2', 'time2', 'datetimeoffset')
THEN '(' + CAST(c.scale AS VARCHAR(5)) + ')'
WHEN tp.name = 'decimal'
THEN '(' + CAST(c.[precision] AS VARCHAR(5)) + ',' + CAST(c.scale AS VARCHAR(5)) + ')'
ELSE ''
END +
CASE WHEN c.collation_name IS NOT NULL AND c.system_type_id = c.user_type_id
THEN ' COLLATE ' + c.collation_name
ELSE ''
END +
CASE WHEN c.is_nullable = 1
THEN ' NULL'
ELSE ' NOT NULL'
END +
CASE WHEN c.default_object_id != 0
THEN ' CONSTRAINT [' + OBJECT_NAME(c.default_object_id) + ']' +
' DEFAULT ' + OBJECT_DEFINITION(c.default_object_id)
ELSE ''
END +
CASE WHEN cc.[object_id] IS NOT NULL
THEN ' CONSTRAINT [' + cc.name + '] CHECK ' + cc.[definition]
ELSE ''
END -- +
--CASE WHEN c.is_identity = 1
-- THEN ' IDENTITY(' + CAST(IDENTITYPROPERTY(c.[object_id], 'SeedValue') AS VARCHAR(5)) + ',' +
-- CAST(IDENTITYPROPERTY(c.[object_id], 'IncrementValue') AS VARCHAR(5)) + ')'
-- ELSE ''
--END
END
FROM sys.columns c WITH(NOLOCK)
JOIN sys.types tp WITH(NOLOCK) ON c.user_type_id = tp.user_type_id
LEFT JOIN sys.check_constraints cc WITH(NOLOCK)
ON c.[object_id] = cc.parent_object_id
AND cc.parent_column_id = c.column_id
WHERE c.[object_id] = @object_id
ORDER BY c.column_id
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 7, ' ') +
ISNULL((SELECT '
, CONSTRAINT [' + i.name + '] PRIMARY KEY ' +
CASE WHEN i.index_id = 1
THEN 'CLUSTERED'
ELSE 'NONCLUSTERED'
END +' (' + (
SELECT STUFF(CAST((
SELECT ', [' + COL_NAME(ic.[object_id], ic.column_id) + ']' +
CASE WHEN ic.is_descending_key = 1
THEN ' DESC'
ELSE ''
END
FROM sys.index_columns ic WITH(NOLOCK)
WHERE i.[object_id] = ic.[object_id]
AND i.index_id = ic.index_id
FOR XML PATH(N''), TYPE) AS NVARCHAR(MAX)), 1, 2, '')) + ')'
FROM sys.indexes i WITH(NOLOCK)
WHERE i.[object_id] = @object_id
AND i.is_primary_key = 1), '') + CHAR(13) + ');'
PRINT @sql
Assuming you have the table name, you can encapsulate the above script in a SP and call the sp with the name of the table. It will provide you with the create script of the table and will omit the identity property of the column if any.
Hope it Helps..!!
Regards,
Shafat Husain
🙂
And Your Lord Never Forgets...!! (64:19 -- Quran)
January 5, 2015 at 12:26 am
PRAMANA.DBA (1/2/2015)
Hi,How to create table without identity by using * into option.
ex: Select * Into Employee_History From Employee.
I don't want identity column in History table, But columns and datatypes should be same.
Thanks in advanced.
The answer is, don't use SELECT * for this. Click on the columns of the table in explorer and drag them into the SELECT. Then, just add "0" to the IDENTITY column and it won't be an IDENTITY column in the new table that SELECT INTO makes.
And I don't mean each individual column, either. When you expand a table in the Object Explorer, you see and entry for "COLUMNS". Just click and drag that into the code window and it'll create a comma delimited list of all the columns for the table.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply