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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-09-28 : 08:02:04
|
| warren writes "Hi, I have recently started worked as a Junior SQL Server developer. My first task it to create a database catalogue to show all the databases that my team has and all the tables within them, the fields within the tables and the constraints on the fields, etc.I have created the following script that will populate the field table by skipping through all the tables that I have put in my tblTable:--truncate table tblTable--Truncate table tblfieldDECLARE @SQLcmd VarChar (1000)DECLARE @DbName VarChar (50)DECLARE @DbID VarChar (50)DECLARE @TblName VarChar (50)DECLARE @TblID VarChar (50)DECLARE c1 Cursor ForSELECT TableName, TableID, DbNameFROM tblTable T, tblDatabase DWHERE T.DatabaseID = D.DbID ORDER By DatabaseIDOPEN c1FETCH NEXT From c1INTO @TblName, @TblID, @DbNameWHILE @@Fetch_Status = 0BEGIN--PRINT @DbNameSELECT @SQLcmd = 'INSERT INTO tblField(FieldName, TableID)SELECT COLUMN_NAME, '''+@TblID+''' FROM '+@DbName+'.INFORMATION_SCHEMA.COLUMNSWHERE Table_Name = '''+@tblName+''''PRINT @SQLcmdEXEC (@SQLcmd)FETCH NEXT From c1INTO @TblName, @TblID, @DbNameENDCLOSE c1DEAlLOCATE c1However, I want to change this script so that when it adds a Field it then adds the constraints to the constraint table before moving on to the next field it needs to add.I have done the following script that doestn work( i think i need to add another cursor within this cursor, but dont know how):--truncate table tblTable--Truncate table tblfieldDECLARE @SQLcmd VarChar (1000)DECLARE @DbName VarChar (50)DECLARE @DbID VarChar (50)DECLARE @TblName VarChar (50)DECLARE @TblID VarChar (50)DECLARE @FieldID VarChar(50)DECLARE @ColumnName VarChar(50)DECLARE c1 Cursor ForSELECT TableName, TableID, DbNameFROM tblTable T, tblDatabase DWHERE T.DatabaseID = D.DbID ORDER By DatabaseIDOPEN c1FETCH NEXT From c1INTO @TblName, @TblID, @DbNameWHILE @@Fetch_Status = 0BEGIN--PRINT @DbNameSELECT @SQLcmd = 'INSERT INTO tblField(FieldName, TableID)SELECT COLUMN_NAME, '''+@TblID+''' FROM '+@DbName+'.INFORMATION_SCHEMA.COLUMNSWHERE Table_Name = '''+@tblName+''''PRINT @SQLcmdEXEC (@SQLcmd)SELECT @SQLcmd = 'select '''+@ColumnName+''' = COLUMN_NAME FROM '+@DbName+'.INFORMATION_SCHEMA.COLUMNSWHERE Table_Name = '''+@tblName+''''PRINT @SQLcmdEXEC (@SQLcmd)SELECT @SQLcmd = 'INSERT INTO tblConstraint(ConstraintName, ConstraintType, FieldID)SELECT C.CONSTRAINT_NAME, T.CONSTRAINT_TYPE, '''+@FieldID+''' FROM '+@DbName+'.INFORMATION_SCHEMA.TABLE_CONSTRAINTS TINNER JOIN '+@DbName+'.INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CON C.Constraint_Name = T.Constraint_NameWHERE C.Column_Name = '''+@ColumnName+''' AND C.TABLE_NAME = '''+@tblName+''''EXEC(@SQLcmd)FETCH NEXT From c1INTO @TblName, @TblID, @DbNameENDCLOSE c1DEAlLOCATE c1Would really appreciate any help you can give me. " |
|
|
|
|
|
|
|