473,226 Members | 1,601 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

column name change

3
Hi, I'm trying to change the column name - date to Sdate in all the tables in my database. As i have many to change so i tried to search all tables and have it change automatically rather than manually however my query doesn't seem to do the job? requesting assistance from anyone is appreciated thank you!


DECLARE @sSQL AS VarChar(500), -- SQL Statement
@sTableName AS VarChar(100) -- TableName

DECLARE CursorTable CURSOR FOR SELECT [NAME] FROM SYSOBJECTS WHERE XTYPE ='U'


OPEN CursorTable
FETCH NEXT FROM CursorTable INTO @sTableName

WHILE @@FETCH_STATUS = 0
BEGIN
SET @sSQL = 'IF EXISTS(SELECT * FROM SYSOBJECTS OBJ ' +
'INNER JOIN SYSCOLUMNS COL ON OBJ.ID = COL.ID ' +
'WHERE OBJ.XTYPE= ''U'' AND OBJ.NAME = ''' + @sTableName + ''' AND COL.NAME = ''DATE'') ' +
' BEGIN ' +
'ALTER TABLE ' + @sTableName + ' ADD TRANDATE DATETIME' +
'UPDATE ' + @sTableName + ' SET TRANDATE=[DATE]' +
'ALTER TABLE ' + @sTableName + ' DROP COLUMN DATE' +
'PRINT ''' + @sTableName + ' DATE Exist''' +
' END'
EXEC (@sSQL)

FETCH NEXT FROM CursorTable INTO @sTableName
END

CLOSE CursorTable
DEALLOCATE CursorTable

--------------------------------------------------------------------------

SELECT [name] FROM Sysobjects WHERE OBJECTPROPERTY(id, N'IsUserTable') = 1

--create CURSOR to Loop every table
IF (PATINDEX('%date%', [name]) > 0)
Begin
Print [name]
End
Mar 28 '06 #1
3 18719
Banfa
9,065 Expert Mod 8TB
Why not just use the command

ALTER TABLE t1 CHANGE date SDate DATETIME;

to change the name of the column in your table rather than creating a new column copying the data and deleting the old column.

Of course this is a MySql extension but you haven't said what database you are using.
Mar 28 '06 #2
ymcj
3
Thanks. I'm using MS SQL 2000
How do i get that extension converted to SQL 2K?
Not really good at MySql
Mar 29 '06 #3
dbmsql
1
I realize this is new post to an old post, but when you are looking for solutions, it would be nice to see one that works simply.

I believe the simple solution to this problem is the stored procedure called:

sp_rename

to quickly rename a file with script do the following:

sp_rename [ @objname = ] 'object_name' , [ @newname = ] 'new_name'
[ , [ @objtype = ] 'object_type' ]


USE AdventureWorks;
GO
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
GO


You can find more detailed info in books on-line. This works with MSSQL 2000 and 2005.

____________________
Keep it simple!
Feb 15 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Shino | last post by:
Hi, Can anyone help with this error: "ORA-00904: invalid column name"? Thanks! SQL> create view PPFa as 2 SELECT L.UserID AS LecID, U.Name, U.Email, I.IntakeID, S.UserID AS StudID 3 FROM...
4
by: mokles | last post by:
Hi All, I am trying to change column name on an existing table. I am using SQL Server 7. As the table is quite big, it is taking quite long time to do it. By the way I could change the column...
1
by: ibm_97 | last post by:
DB2 8.2 on AIX Hi, I'd like to change a column's name in a table which is part of replication. This column is identity column (generated always). 1. Since DB2 will drop and recreate the...
10
by: Colleyville Alan | last post by:
I am trying to turn a short and fat (63 columns) table into one that is tall and skinny (7 columns). Basically, I am trying to create a "reverse crosstab" using a looping structure in VBA along...
1
by: Will | last post by:
I have a combo box on a form which is based on table tblMachine. On that combo box I have four columns visible MachineNumber, description, location and type. The bound column is the MachineNumber...
10
by: Robert Schuldenfrei | last post by:
Hi NG: I have the following code working. The GetProdStrtList() returns a DataTable called psTable. A Grid control is successfully bound to this table and displays correctly. I would like to...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
6
by: Aaron Smith | last post by:
Ok. I have a dataset that has multiple tables in it. In one of the child tables, I have a column that I added to the DataSet (Not in the DataSource). This column does not need to be stored in the...
7
JustJim
by: JustJim | last post by:
Hi If all you can do with ALTER TABLE....ALTER COLUMN is change the datatype/size then how do I change the name of a field? I have several database files spread all over the state and some of...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.