Showing posts with label sql2000. Show all posts
Showing posts with label sql2000. Show all posts

Tuesday, March 6, 2012

Adding new columns with check constraints using same batch

I'm using a stored procedure to add fields to an existing table.

These fields must have check constraints and I need to use one T-SQL batch.

In Sql2000 Ok. In Sql2005, if table exists, I get error "invalid column xxxxx" in Add Constraint statement before new column creation.

the code is

Declare @.Setup bit

Set @.Setup = 1

if @.Setup = 1 Begin

--Alter Table MyTable Add MyField Numeric(1, 0) Not Null Default 3

Exec mySp_Add_Column 'MyTable', 'MyField', 'Numeric (1, 0) Not Null', '3'

If IsNull(ObjectProperty(Object_Id('xCK_MyTable_MyField'), 'IsConstraint'), 0) = 0

Alter Table MyTable Add Constraint xCK_MyTable_MyField Check (MyField >= 1 And MyField <= 3)

End Else Begin

-- drop column

End

GO

If MyTable does not exist and, naturally, I add it before of check constraints (using another Sp which add tables) ok.

If I add FK to new fields, ok.

Now I have to split batch in two parts as workaround...

Can anyone tell me if this is a bug or a "fix" for previous versions?

Many thanks,

Giulio

Interesting, it probably is an undocumented "fix" that exists pretty much because it is illogical to add a check constraint when something doesn't exist, so when they parse it (before they execute it) it fails.

I would personally just add the check constraint creation to the ADD column proc, if I were you. It would just require a parameter, and it would be easy enough. The parameter I would add would be @.checkCriteria. Then pass in 'MyField >= 1 and MyField <= 3)'

Then you can formulate the name (or at worst add another parameter as override) and build the statement in the proc. You can add the constraint when you are adding the column too:

create table test
(
testId int primary key
)
go
alter table test
add newColumn varchar(2000) not null
constraint ckTestNewColumn check (newColumn = 1)

|||Another workaround might be this:

If IsNull(ObjectProperty(Object_Id('xCK_MyTable_MyField'), 'IsConstraint'), 0) = 0
exec('
Alter Table MyTable Add Constraint xCK_MyTable_MyField Check (MyField >= 1 And MyField <= 3)')

Steve Kass
Drew University
www.stevekass.com

|||

Thanks Louis and Steve.

I've just resolved problem. Since in Sql 2000 this problem did not exist (using same T-Sql code) I'd like to know if it's a bug...

Really, if I use Alter Table, instead of mySp_Add_Column to add new columns, I get error too... So if I can understand parser can't know what SP wants to do, why doesn't parser understand I'll add new column before of check constraints? Why can I add FKs on new fields while I can't add CKs?

It seems a parser bug...

Bye,

Giulio

|||

Really? When I execute:

create table test
(
testId int primary key
)
alter table test
add newColumn varchar(2000) not null
constraint ckTestNewColumn check (newColumn = 1)

in a single batch, it works just fine. What error are you getting?

|||

ok, in this way it works...

Only when I add a column to an existing table and, after, I add a check constraint, all in one batch, I get error...

And only after I installed SqlSrv 2k5... on SqlSrv 2k it works fine...

Thursday, February 9, 2012

Adding a version 7 DB to SQL2000

I would like to add a version 7 DB to my SQL 2000 Server. I was wondering if there was a way to add and upgrade it to version 2000 without installing version 7 on the machine? If I have to install 7, what is the best procedure with an existing install of 2000? Any help would be greatly appreciated.You can use either BACKUP/RESTORE or sp_attach_db
SQL Server 2K will automatically upgrade your database during restore or
attach process.
After you will restore or attach your database it is suggested to make
update statistic and if it is possible database re-indexation.
Regards
---
All information provided above AS IS.
"TonyHeuft" <theuft@.cha.ab.ca> wrote in message
news:2AE0F362-5E3A-496C-A0B6-D1B8237BF92C@.microsoft.com...
> I would like to add a version 7 DB to my SQL 2000 Server. I was wondering
if there was a way to add and upgrade it to version 2000 without installing
version 7 on the machine? If I have to install 7, what is the best procedure
with an existing install of 2000? Any help would be greatly appreciated.|||Hi Tony
Can you elaborate on what you mean? Do you have backups of a SQL 7 db or the
actual .mdf and .ldf files?
If you have a backup, you can restore a SQL 7 backup to a SQL 2000 server,
if you have the files, you can use sp_attach_db.
Please read about the appropriate topic RESTORE or sp_attach_db in the Books
Online.
You do not need to install SQL 7.
--
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"TonyHeuft" <theuft@.cha.ab.ca> wrote in message
news:2AE0F362-5E3A-496C-A0B6-D1B8237BF92C@.microsoft.com...
> I would like to add a version 7 DB to my SQL 2000 Server. I was wondering
if there was a way to add and upgrade it to version 2000 without installing
version 7 on the machine? If I have to install 7, what is the best procedure
with an existing install of 2000? Any help would be greatly appreciated.|||What is it you have exactly? Is it a backup or just the detached files? In
either case it should not be a problem as the upgrade happens automatically
when you attach or restore it.
--
Andrew J. Kelly
SQL Server MVP
"TonyHeuft" <theuft@.cha.ab.ca> wrote in message
news:2AE0F362-5E3A-496C-A0B6-D1B8237BF92C@.microsoft.com...
> I would like to add a version 7 DB to my SQL 2000 Server. I was wondering
if there was a way to add and upgrade it to version 2000 without installing
version 7 on the machine? If I have to install 7, what is the best procedure
with an existing install of 2000? Any help would be greatly appreciated.

Adding a total column

I have been working on a website in asp.net1.1 in vb.net2003. I am using a sql2000 server. I am attempting to add a column to my datagrid that will add the total number of wins and output the number in that colum. With some help, I have been able to write the code. However, I am not sure where to put it. Is it a sql function I need to call from my code to add to the win column? Thanks for your help.

Hi, maybe you can consider to add a computed column to the table, if all data used for computing the new column is in the same table and you know how to write the function that get the computed result. For example let's say you have create a function to do the computing and return a result:

create function udf_count_KB (@.tx xml)
returns int
begin
declare @.n int
select @.n=@.tx.value('declare namespace x="http://iorijay.com/KBs";
count(/x:KBRec/x:Record)','int')

return @.n
end
go

then you can alter the table like this:

alter table KBs
add KBCount as dbo.udf_count_KB(KBRec)

Then you can access the new added computed column as other columns, and it is possible to create indexes on computed columns (seehttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/createdb/cm_8_des_05_8os3.asp)