1

I need to create the following query

CREATE TABLE [dbo].[soft] (
[img_pk] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) NULL ,
[img_data] [image] NULL ,
[img_contenttype] [varchar] (50) NULL, 
[file_data] [image] NULL ,
[file_contenttype] [varchar] (50) NULL 
) 

ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[soft] WITH NOCHECK ADD 
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED 
(
[img_pk]
) ON [PRIMARY]
GO

No problem with the first part, it's solved like

string query1 = "CREATE TABLE soft" +
   "(pk int IDENTITY (1, 1) NOT NULL," +
  "name VARCHAR(50) NULL, img_data IMAGE NULL, img_contenttype VARCHAR(50) NULL, file_data IMAGE NULL, file_contenttype VARCHAR(50) NULL)"

But no ideas how to work with the next part. Can anyone help me?

1
  • Could you be more specific about what you're asking or what's tripping you up? Incidentally, the CREATE statement you have is missing the ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] from the original query. Mar 9, 2011 at 13:29

3 Answers 3

4

You can combine the table and primary key definitions:

CREATE TABLE [dbo].[soft] 
( 
   [img_pk] [int] IDENTITY (1, 1) NOT NULL 
       CONSTRAINT PK_Image PRIMARY KEY CLUSTERED, 
   [name] [varchar] (50) NULL , 
   [img_data] [image] NULL , 
   [img_contenttype] [varchar] (50) NULL,  
   [file_data] [image] NULL , 
   [file_contenttype] [varchar] (50) NULL  
)  
GO

You can dispense with the extra 'ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ' if that's your default FileGroup, which it is in many (most?) cases

Also, I have created the PK as Clustered, which is more usual for an auto-inc column.

Also:

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

2
  • I second to Mitch's suggestion
    – this-Me
    Mar 9, 2011 at 13:28
  • There's definitely no reason why you couldn't create PKs during the CREATE query.
    – Jeff LaFay
    Mar 9, 2011 at 13:35
0

You mean just load the query into a string? You can use the @"" multi-line string syntax:

string query2 = @"
ALTER TABLE [dbo].[soft] WITH NOCHECK ADD 
CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED 
(
[img_pk]
) ON [PRIMARY]
";

(The 'ON PRIMARY .. GO' is part of the first query, by the way.)

To execute this you'd use something like

var command2 = dbConnection.CreateCommand();
command2.CommandText = query2;
command2.ExecuteNonQuery();

as usual.

0
0

Any problems you face when you use single line queries ??

string query = "ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO ALTER TABLE [dbo].[soft] WITH NOCHECK ADD CONSTRAINT [PK_image] PRIMARY KEY NONCLUSTERED ( [img_pk] ) ON [PRIMARY] GO";
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.