Sunday, February 19, 2012

Error in an SQL Script

USE master
GO
IF EXISTS(SELECT * FROM sysdatabases
WHERE name = 'Products')
DROP DATABASE Products
GO
CREATE DATABASE Products
ON (NAME=Product,
FILENAME = ‘C:\APPS\Products.mdf’,
SIZE=10 )
GO
USE Products
GO
CREATE TABLE Categories (
catid VARCHAR(10) NOT NULL,
name VARCHAR(50) NOT NULL,
[desc] VARCHAR(MAX) NOT NULL,
PRIMARY KEY(catid) )
GO
CREATE TABLE Products (
productid VARCHAR(10) NOT NULL,
catid VARCHAR(10) NOT NULL,
name VARCHAR(50) NOT NULL,
shorttext VARCHAR(MAX) NOT NULL,
longtext VARCHAR(MAX) NOT NULL,
price MONEY NOT NULL,
thumbnail VARCHAR(40) NOT NULL,
image VARCHAR(40) NOT NULL,
PRIMARY KEY(productid),
FOREIGN KEY(catid) REFERENCES Categories(catid) )
GO
CREATE TABLE FeaturedProducts (
productid VARCHAR(10) NOT NULL,
featuretext VARCHAR(MAX) NOT NULL,
saleprice MONEY NOT NULL,
PRIMARY KEY(productid),
FOREIGN KEY(productid) REFERENCES Products(productid) )
GO

When I run the SQL script above from the command prompt, I get this :

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\HP_Administrator>cd My Documents

C:\Documents and Settings\HP_Administrator\My Documents>sqlcmd -S localhost\SQLE
xpress -i CreateProducts.sql
Changed database context to 'master'.
Msg 102, Level 15, State 1, Server YOUR-C6B5E4EABC\SQLEXPRESS, Line 3
Incorrect syntax near '`'.
Msg 911, Level 16, State 1, Server YOUR-C6B5E4EABC\SQLEXPRESS, Line 1
Could not locate entry in sysdatabases for database 'Products'. No entry found with that name. Make sure that the name is entered correctly.

I am unable to get to the cause of this problem. Thanks for your assistance in advance!

Hello,

The only thing i can see is that your create statement is using "smart quotes" around the data file:

‘C:\APPS\Products.mdf’

I'm not sure if the site just formatted it that way when you copy and pasted, but use single quotes.

Also, just as an aside, it's good practice to explicitly specify the log file in your create statement:

CREATE DATABASE Products

ON (NAME=Product,

FILENAME = 'C:\Products.mdf',

SIZE=10 )

LOG ON (NAME=ProductLOG,

FILENAME = 'C:\Products.ldf',

SIZE=10)

GO

Cheers

Rob

|||

But it is using single quotes right? Or did you mean double quotes?

Thanks for your assistance anyway.

|||No, you are not using single quotes, but smart quotes. You should be using single quotes...

smart quote: `
single quote: '

So instead of writing: `C:\Products.mdf′
write: 'C:\Products.mdf'

No comments:

Post a Comment