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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Reserved words

Author  Topic 

rfleming
Starting Member

9 Posts

Posted - 2005-10-07 : 17:56:50
Are the field names YEAR and DATE considered SQL Reserved Words and therefore should not be used as field names in SQL tables?

Would that be a safe statement? I have a table that was created this way and the trigger associated with it doesn't work. When i opened the trigger to troubleshoot, 1st thing i saw were those 2 tables. I, myself, know one shouldn't really use those exactly like that but maybe something like, YEARof and DATEof.

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-10-08 : 04:45:22
its not Recommened to use the Reserved word but if you have any columns with this key words you can access by box brackets [].. somthing like [YEAR]=2004.. which should work fine..

I hope it solves you query ..

Complicated things can be done by simple thinking
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-10 : 02:16:15
or

Instead of YEAR and DATE let them YYEAR and DDATE

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

rfleming
Starting Member

9 Posts

Posted - 2005-10-10 : 12:16:26
what is the "proper" way of changing the field names, using the ALTER statement or opening the table in design view and changing?

I've tried using the following ALTER statement but it doesn't work:


ALTER TABLE bu_allocation CHANGE '[YEAR]' 'YYEAR' DEFAULT ('')

I also have another field in this table that is a % field (float) datatype but I need it to default to blank instead of the 0 it currently defaults to now. I manually changed it's default to ('') and it seems to have worked.

Was this correct also?

Thx!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-10-10 : 12:23:41
I use

sp_rename 'dbo.MyTable.OldName', 'NewName'

You can use the Design Tool too - but that will just generate SQL behind the scenes (use the "View Change Script" button to see the SQL which Enterprise Manager generates)

Kristen
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-10-10 : 17:57:39
In your search for better field names, might I suggest you ask yourself what the date is of and what the year is of? Are they DateCreated? DateLastModified? DateOrdered? YearOfBirth?

Or maybe the table is a calendar table, perhaps to track holidays, in which case I would go with something like CalendarYear.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

HCLollo
Starting Member

49 Posts

Posted - 2005-10-11 : 07:21:25
rfleming,
while using more significant names for your fields, like CalendarYear
or like, YEAR and DATE should not be reserved words, as far as I can
understand from BOL

HCL


"If it works fine, then it doesn't have enough features"
Go to Top of Page

rfleming
Starting Member

9 Posts

Posted - 2005-10-11 : 09:55:43
lessons learned here...

I opened table is design view and manually changed the default value of one of the fields from 0 to blank. Then ran sp_rename and changed the 2 field names YEAR and DATE.

When i opened the application that uses this table and proceed to "reorganize" the table, i got an ODBC SQL error of "syntax error converting datetime from character string". These fields did have data in them before i made the change. I wonder if i should have DTS'd the data out 1st, made the changes, then DTS'd the data back in?

Now I'm going to have to restore the table from a backup.
Go to Top of Page

Sharky
Starting Member

23 Posts

Posted - 2005-10-11 : 10:11:32
select * into dbo.YOURTABLE_backup FROM dbo.YOURTABLE then you won't have to restore the backup to get to the table... ;-)
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-10-11 : 12:34:58
quote:
Originally posted by HCLollo

rfleming,
while using more significant names for your fields, like CalendarYear
or like, YEAR and DATE should not be reserved words, as far as I can
understand from BOL

YEAR and DATE are both ODBC Reserved Keywords and both appear on the Future Keywords list for SQL Server (as listed in BOL). On top of that, YEAR() is also a SQL Server function, so that could really get confusing and messed up. I would steer clear of them as field names.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

rfleming
Starting Member

9 Posts

Posted - 2005-10-11 : 12:41:57
What would be the recommended "fix" then?

1 - Clone the table/create new table
2 - make changes to new table (field names, datatypes, etc)
3 - truncate the new table if cloned
4 - DTS(or select into) data from old table into new table
5 - make updates to associated trigger

Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-10-11 : 12:42:48
quote:
Originally posted by rfleming

lessons learned here...

I opened table is design view and manually changed the default value of one of the fields from 0 to blank. Then ran sp_rename and changed the 2 field names YEAR and DATE.

When i opened the application that uses this table and proceed to "reorganize" the table, i got an ODBC SQL error of "syntax error converting datetime from character string". These fields did have data in them before i made the change. I wonder if i should have DTS'd the data out 1st, made the changes, then DTS'd the data back in?

Now I'm going to have to restore the table from a backup.


First of all, you can't restore just a single table from backup unless it's all alone on a separate file. Doing so in a production environment would risk getting your data out of sync between the table you restored and the others in the system. But you can restore the whole database, which I suspect is what you really meant.

Second, changing the default to an empty space or null, and renaming the fields would not have caused the error you describe. What do you mean by "reorganize the table" from your application? What exactly did you do?

By the way, the most common cause of that error message is that you are attempting to INSERT or UPDATE a datetime or smalldatetime field with a value that is not in a recognizable date-time format.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2005-10-11 : 12:47:46
quote:
Originally posted by rfleming

What would be the recommended "fix" then?

1 - Clone the table/create new table
2 - make changes to new table (field names, datatypes, etc)
3 - truncate the new table if cloned
4 - DTS(or select into) data from old table into new table
5 - make updates to associated trigger


The process that EM would go to is often the long way around, but it goes something like this:

1 - Create a table named Tmp_OriginalTableName with the new fields/structure/etc.
2 - SELECT INTO Tmp_... FROM ...
3 - DROP all Constraints, indexes, triggers, etc. on old table
4 - DROP old table
5 - Rename new table to old tablename
6 - Recreate all the constraints, indexes, triggers, etc.

Did you have triggers, constraints, or indexes already in effect on the fields you renamed? Try dropping and recreating those.


---------------------------
EmeraldCityDomains.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-10-12 : 02:31:21
For Naming Conventions refer this
http://vyaskn.tripod.com/object_naming.htm

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -