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 2000 Forums
 SQL Server Development (2000)
 How to find Constraints on a table and....

Author  Topic 

rahul8346
Starting Member

21 Posts

Posted - 2006-08-16 : 05:32:31
Hi SqlTeam,
How can i know all the constraints defined on a table in a database
and how to transfer that to another that table to another database
using Script alone with no data loss.
What are the things i should go through for this, I do want to use
Script alone.
Thanks in Advance

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-16 : 06:38:34
[code]
Select SysObjects.[Name] As [Contraint Name] ,Tab.[Name] as [Table Name],Col.[Name] As [Column Name]
From SysObjects Inner Join (Select [Name],[ID] From SysObjects Where XType = 'U') As Tab
On Tab.[ID] = Sysobjects.[Parent_Obj]
Inner Join sysconstraints On sysconstraints.Constid = Sysobjects.[ID]
Inner Join SysColumns Col On Col.[ColID] = sysconstraints.[ColID] And Col.[ID] = Tab.[ID]
order by Tab.[Name]
[/code]
quote:

how to transfer that to another that table to another database


You need to generate the script using EnterPrise Manager which is the best way to do so.


Chirag
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-08-16 : 09:21:57
also you can use INFORMATION_SCHEMA.TABLE_CONSTRAINTS:

SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS


As to your second question, you have to manually create them in second database...you just can't copy and paste them

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

PHGamer
Starting Member

1 Post

Posted - 2010-06-25 : 12:25:10
Thanks for the constraint scripts. I wanted to mention a problem I observed with Miscrosoft SQL Server Management Studio that led me to this topic. I was receiving constraint errors placing data into SQL Server and went to hunt it down. The Constraint display under the Object Explorer tree showed one constraint in the table in question, but not the constraint that was giving me grief! Chiragkhabaria’s script uncovered both constraints, and I adapted a “Script constraint as” “Drop To” “New Query Editor Window” to delete the invisible constraint, and fixed my problem.

Phil

Phil G
Go to Top of Page
   

- Advertisement -