|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:55 AM
Points: 137,
Visits: 250
|
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, November 11, 2010 6:10 AM
Points: 11,
Visits: 11
|
|
thanks.. very good post.. This for storing all index to table, after storing the informtion to table, how to restore it when i needed? Please let me know the query for restoring the indexes from newly created table data
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:55 AM
Points: 137,
Visits: 250
|
|
declare @cmd varchar (max)
Select @cmd= "Create index "+IndexName + "......From tblDBAIdxMetadt exec (@cmd) You got the idea I will post a better Query tomorrow.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, November 11, 2010 6:10 AM
Points: 11,
Visits: 11
|
|
| Thanks for reply.. can we store the relations to the same table and can we restore it?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:55 AM
Points: 137,
Visits: 250
|
|
| See scripts Truncate all Tables There you will find a script to save and restore the table relationships. Normally you want to keep that kind of data separated from it.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:55 AM
Points: 137,
Visits: 250
|
|
Here is a better example of how to restore the indexes : If Exists (select distinct SchemaOwner ,TableName ,AllColName from tblDBAIdxMetadt where IsClustered = 1 and UniqueType <> 'PRIMARY KEY' ) begin declare @IndexName varchar(255) ,@SchemaOwner varchar (255) ,@TableName varchar (255) ,@ColumnList varchar (1000) ,@Sqlcmd varchar (2000) set @IndexName = ? set @SchemaOwner = ? set @TableName = ? set @ColumnList = ?
select @sqlcmd = 'CREATE CLUSTERED INDEX '+ltrim(rtrim(@IndexName))+' ON '+ltrim(rtrim(@SchemaOwner))+'.'+ltrim(rtrim(@TableName))+' ( '+ltrim(rtrim(@ColumnList))+' ) WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ' exec (@sqlcmd) end You can used a cursor or while loop to generated or execute the scripts by filling the information with the ? marks.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:55 AM
Points: 137,
Visits: 250
|
|
This will be a way to save relations from a table. --Create Table to store constraint information IF NOT EXISTS (Select [name] from sys.tables where [name] = 'T_FK_Xref' and type = 'U') Create table DatabaseName.dbo.T_FK_Xref ( ID int identity (1,1), ConstraintName varchar (255), MasterTable varchar(255), MasterColumn varchar(255), ChildTable varchar(255), ChildColumn varchar(255), FKOrder int ) go --Store Constraints insert into DatabaseName.dbo.T_FK_Xref(ConstraintName,MasterTable,MasterColumn,ChildTable,ChildColumn,FKOrder) SELECT object_name(constid) as ConstraintName,object_name(rkeyid) MasterTable ,sc2.name MasterColumn ,object_name(fkeyid) ChildTable ,sc1.name ChildColumn ,cast (sf.keyno as int) FKOrder FROM sysforeignkeys sf INNER JOIN syscolumns sc1 ON sf.fkeyid = sc1.id AND sf.fkey = sc1.colid INNER JOIN syscolumns sc2 ON sf.rkeyid = sc2.id AND sf.rkey = sc2.colid ORDER BY rkeyid,fkeyid,keyno
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, April 04, 2013 8:56 AM
Points: 6,
Visits: 77
|
|
Running the code as is, and adding a tablename from a SQL Server 2005 database, gives the following error:
Msg 102, Level 15, State 1, Line 108 Incorrect syntax near 'end'.
Any ideas what's wrong. The syntax looks fine. I'm using the SQL Server query editor and selecting the database from the combo box in the menu bar at the top of the editor. Thanks, Ed
|
|
|
|