I am using sp_rename to switch table name , below is my query
exec sp_rename 'dbo.People_Daily' , 'TEMP'
exec sp_rename 'dbo.People_Future', 'People_Daily'
exec sp_rename 'TEMP', 'dbo.People_Future'
My questions is, what if I execute the same query in parallel with different table name,since im using TEMP word in both query, is there a possiblity that the TEMP in People will be used by Address?, what should I do to avoid this, is there any auto generated word to replace TEMP?
exec sp_rename 'dbo.Address_Daily' , 'TEMP'
exec sp_rename 'dbo.Addresse_Future', 'Address_Daily'
exec sp_rename 'TEMP', 'dbo.Address_Future'
Thanks!!!
Using TEMP can collide if your batches run in parallel. But you can use TEMP as an suffix to existing table name:
exec sp_rename 'dbo.People_Daily' , 'dbo.People_Daily_TEMP'
exec sp_rename 'dbo.People_Future', 'People_Daily'
exec sp_rename 'dbo.People_Daily_TEMP', 'dbo.People_Future'
and
exec sp_rename 'dbo.Address_Daily' , 'dbo.Address_Daily_TEMP'
exec sp_rename 'dbo.Addresse_Future', 'Address_Daily'
exec sp_rename 'dbo.Address_Daily_TEMP', 'dbo.Address_Future'
Then your batches won't collide.
Related
I am trying to delete all data in all tables except system versioned (because we can't). This is part of an integration test that clears data before running each test.
The following statement returns 1 for history tables (the ones that we cannot execute delete from on them.
SELECT OBJECTPROPERTY(OBJECT_ID('table_name'), 'TableTemporalType')
So my attempt was as follows:
-- Remove check constraints
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
-- Delete data
EXEC sp_MSforeachtable 'SET QUOTED_IDENTIFIER ON; IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableTemporalType'') != 1 DELETE FROM ?'
-- Restore check constraints
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL'
However, I am still getting the error:
Cannot delete rows from a temporal history table 'dbo.table_name'.
I am not sure what I am doing wrong!
Any hints are appreciated!
I would do this by generating some dynamic sql using sys.tables. Something like this should be pretty close to what you are trying to do.
declare #sql nvarchar(max) = ''
select #sql = #sql + 'delete ' + name + ';'
from sys.tables
where temporal_type = 0
select #sql
--uncomment the line below when you are ready to blow away all your data
--exec sp_executesql #sql
I found this script which does the job for clearing all the tables in one database.
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'DELETE FROM ?'
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
GO
I'm not familiar with Microsoft SQL Server, at the moment I right click and run the query. I have 100 databases to clear.
How do I clear all tables from all databases programmatically?
You can probably also use the undocumented sp_MSForEachDB, combining it with the sp_MSForEachTable to iterate through each db then through each table. You should be using TRUNCATE instead of DELETE. However, you might be better off just using SSMS to generate scripts for the database. Generate scripts, delete the old DB, generate a new empty one.
The syntax for this undocumented procedure is:
EXEC sp_MSforeachdb #command
Example:
USE master
GO
EXEC sys.sp_MSforeachdb 'USE ?
IF DB_NAME() NOT IN(''master'', ''model'', ''msdb'', ''tempdb'')
BEGIN
EXEC sp_MSForEachTable ''ALTER TABLE ? NOCHECK CONSTRAINT ALL'';
EXEC sp_MSForEachTable ''TRUNCATE ?'';
EXEC sp_MSForEachTable ''ALTER TABLE ? CHECK CONSTRAINT ALL'';
END'
GO
I have 50+ tables in my database and I want to delete all the data in 48 tables.
I tried using
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'
EXEC sp_MSForEachTable 'DELETE FROM ? '
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
EXEC sp_MSForEachTable 'ALTER TABLE ? ENABLE TRIGGER ALL'
EXEC sp_MSFOREACHTABLE 'SELECT * FROM ?'
GO
but it deletes all the data in all the tables.
Can some one suggest me what changes I need to make to delete data from all tables except TABLE1, TABLE2 etc in the query:
EXEC sp_MSForEachTable 'DELETE FROM ? '
I am using SQL Server 2008R2.
Use IF ''?'' NOT IN (''TABLE1'',''TABLE2'') before your DELETE FROM [?]
By the way, I suggest using TRUNCATE TABLE [?] instead of DELETE FROM [?]
ALTER TABLE [TEMP]
SP_RENAME '[TEMP].[Day]', 'GT', 'COLUMN'
I am trying to rename Day to GT and am getting the error
Incorrect syntax near 'SP_RENAME'
SQL Server Management Studio says the error is on SP_RENAME
NOTE: I'm open to other options besides sp_rename
SP_RENAME is not part of the ALTER TABLE statement. It is a system stored procedure and therefore it should be invoked using the EXEC/EXECUTE statement, like this:
exec SP_RENAME '[TEMP].[Day]', 'GT', 'COLUMN'
(without the alter table temp bit)
You need to start each SP_RENAME with an EXEC
Worked for the answer which was quoted by #juergen d.
PRINT N'Modifying Schema for SAMPLE_TABLE started'
DECLARE #COLNAME_DEAL_TERM VARCHAR(200) ;
SET #COLNAME_DEAL_TERM = 'SAMPLE_COL_NAME';
IF COL_LENGTH('SAMPLE', #COLNAME_DEAL_TERM ) IS NULL
BEGIN
exec SP_RENAME 'SAMPLE.portfolio',#COLNAME_DEAL_TERM , 'COLUMN';
END;
GO
I tried the following code. Although I don't get any errors, it did not do it.
SELECT * FROM Categories EXEC sp_rename 'Active', CategoriesActive
EXEC sp_rename 'Categories.Active', 'CategoriesActive', 'COLUMN'
FOR MSSQL :
EXEC sp_rename 'TABLENAME.OLD_COLUMNNAME', 'NEW_COLUMNAME', 'COLUMN';
FOR MYSQL : Use ALTER TABLE to do this
ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name
You can rename a column using a CHANGE old_col_name new_col_name column_definition clause. To do so, specify the old and new column names and the definition that the column currently has. For example, to rename an INTEGER column from a to b, you can do this:
ALTER TABLE t1 CHANGE a b INTEGER;
You don't need to use that select in front, and the syntax should be like:
EXEC sp_rename
#objname = 'Categories.Active',
#newname = 'CategoriesActive',
#objtype = 'Type_of_your_column'