Showing posts with label constraint. Show all posts
Showing posts with label constraint. Show all posts

Friday, March 30, 2012

How to trap the results of constraints in SQL Server 2005from Visual studio C# Code?

Hi all,

Suppose I have set a CHECK constraint to a column where Salary field is not permitted to be less than 1000 or greater than 10000.

In this situation, when I insert a new record with salary as 10, using a stored procedure from Visual Studio, how will I trap the error from C# Code?

Thanks

Tomy

Your best bet would be to have layered constraints. Your business logic should also make sure that the data being entered is between 1000 and 10000, so that the violation would never reach the database. Database constraints should be left in place for people who like to edit the database directly.

While you can get a violated constraint error from the database, I don't believe it would be phrased properly to display to your users, which would mean lots of parsing in order to make the error presentable and user-friendly. It would be better to use a range validator to prevent the user from entering incorrect information.

As for trapping the error, use Try/Catch blocks

try{}catch (System.Data.SqlClient.SqlException ex){// Handle SQL Exceptions here; // all sql exceptions fall under this exception type, // but the errorcode/errors will be different.}catch (Exception ex){// handle all other errors here.}
|||

Thanks a lot.

Wednesday, March 28, 2012

How to Transfer database from mssql2005 to mssql2000 by using generate script.

Currently i need to transfer database (complete with primary key, foreign key ,constraint) from mssql2005 to mssql2000 by using generate script. After getting the script, i execute the script in the sql analyzer 2000, i get a lots of syntax error. Following are some of the error :

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 25
Line 25: Incorrect syntax near '('.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 170, Level 15, State 1, Line 15
Line 15: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 14
Line 14: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 99
Line 99: Incorrect syntax near '('.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Warning: The table 'HACOMP_TMP' has been created but its maximum row size (16427) exceeds the maximum number of bytes per row (8060). INSERT or UPDATE of a row in this table will fail if the resulting row length exceeds 8060 bytes.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 170, Level 15, State 1, Line 16
Line 16: Incorrect syntax near '('.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Warning: The table 'HAAPPRAISE_DEV_TMP' has been created but its maximum row size (9196) exceeds the maximum number of bytes per row (8060). INSERT or UPDATE of a row in this table will fail if the resulting row length exceeds 8060 bytes.

Below are the first part of the script :

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HRMP_Details]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[HRMP_Details](
[Comp_CD] [smallint] NULL,
[MP_ID] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[MP_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Replacement] [bit] NOT NULL,
[Employee_Replaced] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Terminate_Date] [int] NULL,
[Addition] [bit] NULL,
[Approved_Budget] [bit] NULL,
[MP_Permanent] [bit] NULL,
[MP_Temporary] [bit] NULL,
[Time_Needed] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MP_Contract] [bit] NULL,
[Contract_Period] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Full_Time] [int] NULL,
[Part_Time] [int] NULL,
[Hours_Required] [int] NULL,
[Status] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Hire_By] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Headcounts] [int] NULL,
[Approved_Headcounts] [int] NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Applicant_Score]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Applicant_Score](
[COMP_CD] [smallint] NOT NULL,
[Score_ID] [int] IDENTITY(1,1) NOT NULL,
[Applicant_ID] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MP_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Dimension_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Interview_Code] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Interviewer] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Score] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Comment_Number] [int] NULL,
[ID] [int] NULL,
CONSTRAINT [PK_Applicant_Score] PRIMARY KEY CLUSTERED
(
[Score_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Advertisement]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Advertisement](
[COMP_CD] [smallint] NOT NULL,
[Advertisement_ID] [int] IDENTITY(1,1) NOT NULL,
[MP_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_Advertisement_Through_Internet] DEFAULT ((0)),
[Channel_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Languages] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Advertisement_StartDate] [int] NULL,
[Advertisement_EndDate] [int] NULL,
[Monday] [bit] NULL,
[Tuesday] [bit] NULL,
[Wednesday] [bit] NULL,
[Thursday] [bit] NULL,
[Friday] [bit] NULL,
[Saturday] [bit] NULL,
[Sunday] [bit] NULL,
[Daily] [bit] NULL,
[Instruction] [varchar](4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[User_id] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Date_Time] [datetime] NULL,
CONSTRAINT [PK_Advertisement] PRIMARY KEY CLUSTERED
(
[Advertisement_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HAAppraise_COMPETENCY_Approve]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[HAAppraise_COMPETENCY_Approve](
[COMP_CD] [smallint] NOT NULL,
[COMPETENCY_CODE] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[COMPETENCY_ID] [int] IDENTITY(1,1) NOT NULL,
[COMPETENCY_DESC] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EmpNo] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Appraisal_Code] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[App_Form_Code] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[User_Login] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Date_Time] [datetime] NULL,
[Competency_Tracking] [varchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Competency_Type] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
END

Can anyone help me in this?

ps:I cant use the import/export utility because using this method does not include some of the primary key,foreign key, constraint and etc.

You can't script out and transfer the entire database.

There are incompatibilities between SQL 2000 and SQL 2005 schemas. (For example, there is not a [sys.objects] table in SQL 2000, SQL 2005 contains datatypes [such as (varchar(max)] that are not allowed in SQL 2000, etc.

However, you can script out specific data tables and transfer them to a SQL 2000 database as long as the datatypes are supported in SQL 2000, and there is not the use of any CONSTRAINT 'code' that is incompatible.

I suggest that you use the IMPORT/EXPORT wizard, or explore some of the third party products that will create scripts.

|||Using SSMS you can use the Create Script wizard which also support the creation of compatible scripts. Run the script after creating it to create the new database. Afterwards you can transfer the data(base) to the new database.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||Hi, using Sql Server 2005 Developer Edition Service Pack 2, right clicking on an existing database, selecting Generate Script and using the option to generate scripts for Sql 2000 does NOT prevent the generated script from containing "max". Is there a set of guidelines somewhere on what types/objects to avoid if you want to create a schema that is truly Sql Server 2000 compatible please?
|||

The (max) types cannot be converted to SQL Server 200 using the compat script, you will have to tweak the script on your own, changing the max to the appropiate types like text/image.

Jens K. Suessmeyer

http://www.sqlserver2005.de

How to Transfer database from mssql2005 to mssql2000 by using generate script.

Currently i need to transfer database (complete with primary key, foreign key ,constraint) from mssql2005 to mssql2000 by using generate script. After getting the script, i execute the script in the sql analyzer 2000, i get a lots of syntax error. Following are some of the error :

Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 25
Line 25: Incorrect syntax near '('.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 170, Level 15, State 1, Line 15
Line 15: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 17
Line 17: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 14
Line 14: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 99
Line 99: Incorrect syntax near '('.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Warning: The table 'HACOMP_TMP' has been created but its maximum row size (16427) exceeds the maximum number of bytes per row (8060). INSERT or UPDATE of a row in this table will fail if the resulting row length exceeds 8060 bytes.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Server: Msg 170, Level 15, State 1, Line 16
Line 16: Incorrect syntax near '('.
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'sys.objects'.
Warning: The table 'HAAPPRAISE_DEV_TMP' has been created but its maximum row size (9196) exceeds the maximum number of bytes per row (8060). INSERT or UPDATE of a row in this table will fail if the resulting row length exceeds 8060 bytes.

Below are the first part of the script :

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HRMP_Details]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[HRMP_Details](
[Comp_CD] [smallint] NULL,
[MP_ID] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[MP_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Replacement] [bit] NOT NULL,
[Employee_Replaced] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Terminate_Date] [int] NULL,
[Addition] [bit] NULL,
[Approved_Budget] [bit] NULL,
[MP_Permanent] [bit] NULL,
[MP_Temporary] [bit] NULL,
[Time_Needed] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MP_Contract] [bit] NULL,
[Contract_Period] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Full_Time] [int] NULL,
[Part_Time] [int] NULL,
[Hours_Required] [int] NULL,
[Status] [varchar](2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Hire_By] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Headcounts] [int] NULL,
[Approved_Headcounts] [int] NULL
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Applicant_Score]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Applicant_Score](
[COMP_CD] [smallint] NOT NULL,
[Score_ID] [int] IDENTITY(1,1) NOT NULL,
[Applicant_ID] [varchar](20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[MP_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Dimension_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Interview_Code] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Interviewer] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Score] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Comment_Number] [int] NULL,
[ID] [int] NULL,
CONSTRAINT [PK_Applicant_Score] PRIMARY KEY CLUSTERED
(
[Score_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Advertisement]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Advertisement](
[COMP_CD] [smallint] NOT NULL,
[Advertisement_ID] [int] IDENTITY(1,1) NOT NULL,
[MP_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_Advertisement_Through_Internet] DEFAULT ((0)),
[Channel_Code] [varchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Languages] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Advertisement_StartDate] [int] NULL,
[Advertisement_EndDate] [int] NULL,
[Monday] [bit] NULL,
[Tuesday] [bit] NULL,
[Wednesday] [bit] NULL,
[Thursday] [bit] NULL,
[Friday] [bit] NULL,
[Saturday] [bit] NULL,
[Sunday] [bit] NULL,
[Daily] [bit] NULL,
[Instruction] [varchar](4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[User_id] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Date_Time] [datetime] NULL,
CONSTRAINT [PK_Advertisement] PRIMARY KEY CLUSTERED
(
[Advertisement_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[HAAppraise_COMPETENCY_Approve]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[HAAppraise_COMPETENCY_Approve](
[COMP_CD] [smallint] NOT NULL,
[COMPETENCY_CODE] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[COMPETENCY_ID] [int] IDENTITY(1,1) NOT NULL,
[COMPETENCY_DESC] [nvarchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EmpNo] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Appraisal_Code] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[App_Form_Code] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[User_Login] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Date_Time] [datetime] NULL,
[Competency_Tracking] [varchar](500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Competency_Type] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
END

Can anyone help me in this?

ps:I cant use the import/export utility because using this method does not include some of the primary key,foreign key, constraint and etc.

You can't script out and transfer the entire database.

There are incompatibilities between SQL 2000 and SQL 2005 schemas. (For example, there is not a [sys.objects] table in SQL 2000, SQL 2005 contains datatypes [such as (varchar(max)] that are not allowed in SQL 2000, etc.

However, you can script out specific data tables and transfer them to a SQL 2000 database as long as the datatypes are supported in SQL 2000, and there is not the use of any CONSTRAINT 'code' that is incompatible.

I suggest that you use the IMPORT/EXPORT wizard, or explore some of the third party products that will create scripts.

|||Using SSMS you can use the Create Script wizard which also support the creation of compatible scripts. Run the script after creating it to create the new database. Afterwards you can transfer the data(base) to the new database.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||Hi, using Sql Server 2005 Developer Edition Service Pack 2, right clicking on an existing database, selecting Generate Script and using the option to generate scripts for Sql 2000 does NOT prevent the generated script from containing "max". Is there a set of guidelines somewhere on what types/objects to avoid if you want to create a schema that is truly Sql Server 2000 compatible please?
|||

The (max) types cannot be converted to SQL Server 200 using the compat script, you will have to tweak the script on your own, changing the max to the appropiate types like text/image.

Jens K. Suessmeyer

http://www.sqlserver2005.de

Wednesday, March 21, 2012

How to tell SQL Server to use the default constraint name when adding one?

Hi guys,

I have this problem. I want to add a new primary key to a table but i want the name of the contstraint to be generated by the system. I have this TSQL code.

ALTER TABLE TableTest
ADD CONSTRAINT PRIMARY KEY (C1)


Reading the BOL, it says that if you don't supply a name for the constraint it generates one. But I get this error "Incorrect syntax near the keyword 'PRIMARY'".

If I add a name to the constraint, it works fine.
I'm using SQL Server 2000

Thanks
DarkneonAt least you have read and tried. Enclosed is a script that creates a table we use to trace deadlocks. The first created the primary key as it creates the table. The second alters the table after creation to add the primary key. Note that the constraint is named PK_tbl_trace_deadlock:



CREATE TABLE [dbo].[tblm_trace_deadlock] (
[row_id] [int] IDENTITY (1, 1) NOT NULL ,
[import_id] [int] NOT NULL ,
[BinaryData] [image] NULL ,
[DatabaseID] [int] NULL ,
[NTUserName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[NTDomainName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[HostName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ClientProcessID] [int] NULL ,
[ApplicationName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[LoginName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[SPID] [int] NULL ,
[Duration] [bigint] NULL ,
[StartTime] [datetime] NULL ,
[EndTime] [datetime] NULL ,
[EventSubClass] [int] NULL ,
[ObjectID] [int] NULL ,
[IndexID] [int] NULL ,
[IntegerData] [int] NULL ,
[ServerName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[EventClass] [int] NULL ,
[Mode] [int] NULL ,
[DBUserName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [PK_tbl_trace_deadlock] PRIMARY KEY CLUSTERED
(
[row_id]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tblm_trace_deadlock]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[tblm_trace_deadlock]
GO

CREATE TABLE [dbo].[tblm_trace_deadlock] (
[row_id] [int] IDENTITY (1, 1) NOT NULL ,
[import_id] [int] NOT NULL ,
[BinaryData] [image] NULL ,
[DatabaseID] [int] NULL ,
[NTUserName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[NTDomainName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[HostName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[ClientProcessID] [int] NULL ,
[ApplicationName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[LoginName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[SPID] [int] NULL ,
[Duration] [bigint] NULL ,
[StartTime] [datetime] NULL ,
[EndTime] [datetime] NULL ,
[EventSubClass] [int] NULL ,
[ObjectID] [int] NULL ,
[IndexID] [int] NULL ,
[IntegerData] [int] NULL ,
[ServerName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[EventClass] [int] NULL ,
[Mode] [int] NULL ,
[DBUserName] [nvarchar] (512) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[tblm_trace_deadlock] WITH NOCHECK ADD
CONSTRAINT [PK_tbl_trace_deadlock] PRIMARY KEY CLUSTERED
(
[row_id]
) ON [PRIMARY]
GO|||Hey tomh53,

Thanks for your input, but it's not quite what I am looking for. The script you submited creates a primary key with the name you supplied to it. I want to use the name that is automatically generated by the system. For example, in Entreprise Managed, when you set a primary key it generates a name for you. I want it to do the same thing, except the primary key is set programatically.

Here's what the BOL says:

"
constraint_name

Is the new constraint. Constraint names must follow the rules for identifiers, except that the name cannot begin with a number sign (#). If constraint_name is not supplied, a system-generated name is assigned to the constraint."

But like I said, it generates an error instead of a name for me.

Darkneon|||Why would you want to allow abiguity to exist when you have an opportunity to make something meaningful out of the constraint naming process. Allow me to quote from Inside SQL Server 200 by Kalen Delaney regarding system generated constraint names:


The advantage of explicitly naming your constraint rather than using the system-generated name is greater clarity. The constraint name is used in the error message for any constraint violation, so creating a name such as CUSTOMER_PK probably makes more sense to users than a name such as PK__customer__cust_i__0677FF3C. You should choose your own constraint names if such error messages are visible to your users.

The first two characters (PK) show the constraint typePK for PRIMARY KEY, UQ for UNIQUE, FK for FOREIGN KEY, and DF for DEFAULT. Next are two underscore characters, which are used as a separator. (You might be tempted to use one underscore to conserve characters and to avoid having to truncate as much. However, it's common to use a single underscore in a table name or a column name, both of which appear in the constraint name. Using two underscore characters distinguishes the kind of a name it is and where the separation occurs.)
Next comes the table name (customer), which is limited to 116 characters for a PRIMARY KEY constraint and slightly fewer characters for all other constraint names. For all constraints other than PRIMARY KEY, there are then two more underscore characters for separation followed by the next sequence of characters, which is the column name. The column name is truncated to five characters if necessary. If the column name has fewer than five characters in it, the length of the table name portion can be slightly longer.

And finally, the hexadecimal representation of the object ID for the constraint (68E79C55) comes after another separator. (This value is used as the id column of the sysobjects system table and the constid column of the sysconstraints system table.) Object names are limited to 128 characters in SQL Server 2000, so the total length of all portions of the constraint name must also be less than or equal to 128.|||But if you insist in shooting yourself in the foot, here it is:



CREATE TABLE [dbo].[bozo] (
row_id int IDENTITY (1, 1) NOT NULL ,
clownname varchar(20) NOT NULL ,
PRIMARY KEY CLUSTERED (row_id)
)
GO|||You should choose your own constraint names if such error messages are visible to your users.

This was the primary reason we did not mind using system generated names.
But it's ok now, I've talked with me collegue and we decided that we are going to give a name to the constraints.

By the way, the code snippet you provided is not good when you have a already existing table which is missing a primary key. Anyways, if someone knows the answers to my question, I am still interested. Otherwise, I close the discussion to my original question here.

Darkneon|||Look at the last two statements of the large block of code. The first one creates the table without a PK. The second one alters the table to add a PK. To take the small snippet, create the table, and then add the PK is:



CREATE TABLE [dbo].[bozo] (
row_id int IDENTITY (1, 1) NOT NULL ,
clownname varchar(20) NOT NULL
)
GO

alter table bozo
add
PRIMARY KEY CLUSTERED (row_id)
GO|||Thanks,

I tested it and it worked, with the name generated and everything. It is always good to know how to do stuff :D

Darkneon