Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 How to script Keys

Author  Topic 

rama108
Posting Yak Master

115 Posts

Posted - 2013-11-01 : 15:43:45
Is there a way to script all keys from a table?

Thanks.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-11-01 : 16:05:45
Does this give you what you are looking for?
SELECT  c.NAME AS columnName, i.*
FROM sys.indexes i
INNER JOIN sys.COLUMNS c ON c.OBJECT_ID = i.OBJECT_ID
WHERE
OBJECT_NAME(c.object_id) = 'YourTableName'
If you are trying to generate the scripts to create the keys/indexes, right click on the database name, Tasks-> Generate Scripts. There is an option that allows you to script indexes (along with the script for the table)
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-11-01 : 20:38:43
James,
Thank you for your response. May be you can help me with this. I have too many tables in the database. When I use the Import data wizard to import the data and the table structure in another database, it will not import the keys, column default values and the identity. I select Enable Identity, but it is not for actually importing the column as identity. So how do i use the wizard to import keys, indexes, identity and column defaults.

Thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-02 : 13:16:34
quote:
Originally posted by rama108

James,
Thank you for your response. May be you can help me with this. I have too many tables in the database. When I use the Import data wizard to import the data and the table structure in another database, it will not import the keys, column default values and the identity. I select Enable Identity, but it is not for actually importing the column as identity. So how do i use the wizard to import keys, indexes, identity and column defaults.

Thank you.


you cant use wizard for that. you need to script and apply them separately.


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-02 : 13:45:09
If you've to do this in SSIS, you've task called Transfer SQL Server Objects which can also be used.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-11-02 : 16:02:07
Thanks.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-11-02 : 16:55:50
quote:
Originally posted by rama108

James,
Thank you for your response. May be you can help me with this. I have too many tables in the database. When I use the Import data wizard to import the data and the table structure in another database, it will not import the keys, column default values and the identity. I select Enable Identity, but it is not for actually importing the column as identity. So how do i use the wizard to import keys, indexes, identity and column defaults.

Thank you.

use the second method I described. That is to say, use "generate scripts" to script all your tables with keys, constraints; apply them to your target database. Then use import/export wizard.
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-11-03 : 11:10:40
Thanks James.
Go to Top of Page
   

- Advertisement -