473,795 Members | 3,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

column name change

3 New Member
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('%dat e%', [name]) > 0)
Begin
Print [name]
End
Mar 28 '06 #1
3 18743
Banfa
9,065 Recognized Expert Moderator Expert
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 New Member
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 New Member
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.SalesTer ritory.Territor yID', '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
64835
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 User_Lecturer L, User_Student S, TBL_Intake I, Users U 4 WHERE L.UserID=S.InitialSupervisorID And L.UserID=U.UserID And
4
20157
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 name only through the Enterprize Manager. Is it possible to change the column name using SQL script? Why the change of column name will depend on the size of the table?
1
2306
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 table with the changed column name, do I have to do something like 'set integrity off'?
10
6566
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 with SQL. I'd like to take the name of the column and input it into a descritor field. This isn't the table, but will serve as a better illustration than the real deal. If the table looks like this:
1
6178
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 and this is not set to 0 as the machines have its number on which means the user should know the machine number. My problem is if the user doesn't know the number. I have a button which changes the row source of the combo box to order by...
10
3085
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 change the column headings to something other than the name of SQL column. For example I would prefer "Component" to the default "ps_component". I would also like to set the column width property myself. As you can see, I can set the grid's...
19
25471
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 sort columns based on the column header the user has clicked in both Ascending and Descending formats.
1
4239
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
6
10681
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 data on the datasource. It simply gets the first name and last name of an instructor and displays it in the grid. I have two major problems.... One, it doesn't display in the column until the row is saved, (Even after calling a refresh on the...
7
5008
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 the users (Darn users!) have changed a field from "Datum" to "MapDatum" and I need to change it back because it is causing problems with the front end. I suspect that I'm going to have to determine if the field MapDatum exists and if it does, make...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.